Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[TwigBridge] Show Twig's loader paths on debug:twig command #24064

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 47 additions & 1 deletion 48 src/Symfony/Bridge/Twig/Command/DebugCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@
namespace Symfony\Bridge\Twig\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\TableSeparator;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Twig\Environment;
use Twig\Loader\FilesystemLoader;

/**
* Lists twig functions, filters, globals and tests present in the current project.
Expand All @@ -29,11 +31,13 @@ class DebugCommand extends Command
protected static $defaultName = 'debug:twig';

private $twig;
private $projectDir;

/**
* @param Environment $twig
* @param string|null $projectDir
*/
public function __construct($twig = null)
public function __construct($twig = null, $projectDir = null)
{
if (!$twig instanceof Environment) {
@trigger_error(sprintf('Passing a command name as the first argument of "%s" is deprecated since version 3.4 and will be removed in 4.0. If the command was registered by convention, make it a service instead.', __METHOD__), E_USER_DEPRECATED);
Expand All @@ -46,6 +50,7 @@ public function __construct($twig = null)
parent::__construct();

$this->twig = $twig;
$this->projectDir = $projectDir;
}

public function setTwigEnvironment(Environment $twig)
Expand Down Expand Up @@ -120,6 +125,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
}
}
$data['tests'] = array_keys($data['tests']);
$data['loader_paths'] = $this->getLoaderPaths();
$io->writeln(json_encode($data));

return 0;
Expand All @@ -145,9 +151,49 @@ protected function execute(InputInterface $input, OutputInterface $output)
$io->listing($items);
}

$rows = array();
foreach ($this->getLoaderPaths() as $namespace => $paths) {
foreach ($paths as $path) {
$rows[] = array($namespace, '* '.$path);
$namespace = '';
}
$rows[] = new TableSeparator();
}
array_pop($rows);
$io->section('Loader Paths');
$io->table(array('Namespace', 'Paths'), $rows);

return 0;
}

private function getLoaderPaths()
{
if (!($loader = $this->twig->getLoader()) instanceof FilesystemLoader) {
return array();
}

$loaderPaths = array();
foreach ($loader->getNamespaces() as $namespace) {
$paths = array_map(function ($path) use ($namespace) {
if (null !== $this->projectDir && 0 === strpos($path, $this->projectDir)) {
$path = ltrim(substr($path, strlen($this->projectDir)), DIRECTORY_SEPARATOR);
}

return $path;
}, $loader->getPaths($namespace));

if (FilesystemLoader::MAIN_NAMESPACE === $namespace) {
$namespace = '(None)';
} else {
$namespace = '@'.$namespace;
}

$loaderPaths[$namespace] = $paths;
}

return $loaderPaths;
}

private function getMetadata($type, $entity)
{
if ($type === 'globals') {
Expand Down
1 change: 1 addition & 0 deletions 1 src/Symfony/Bundle/TwigBundle/Resources/config/console.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

<service id="Symfony\Bridge\Twig\Command\DebugCommand">
<argument type="service" id="twig" />
<argument>%kernel.project_dir%</argument>
<tag name="console.command" command="debug:twig" />
</service>

Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.