From 40a4bb389b1ffbf3a34c8ccd2712774627b9d0a9 Mon Sep 17 00:00:00 2001 From: Yonel Ceruto Date: Fri, 1 Sep 2017 10:02:44 -0400 Subject: [PATCH 1/3] Show Twig loader paths on debug:twig command --- .../Bridge/Twig/Command/DebugCommand.php | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/Symfony/Bridge/Twig/Command/DebugCommand.php b/src/Symfony/Bridge/Twig/Command/DebugCommand.php index da98feb7632bb..48e207fdf2692 100644 --- a/src/Symfony/Bridge/Twig/Command/DebugCommand.php +++ b/src/Symfony/Bridge/Twig/Command/DebugCommand.php @@ -18,6 +18,7 @@ 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. @@ -120,6 +121,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; @@ -145,9 +147,36 @@ protected function execute(InputInterface $input, OutputInterface $output) $io->listing($items); } + $list = array(); + foreach ($this->getLoaderPaths() as $namespace => $paths) { + $list = array_merge($list, array_map(function ($path) use ($namespace) { + return $namespace.($namespace ? ': ' : '').$path; + }, $paths)); + } + $io->section('Loader Paths'); + $io->listing($list); + return 0; } + private function getLoaderPaths() + { + if (!($loader = $this->twig->getLoader()) instanceof FilesystemLoader) { + return array(); + } + + $paths = array(); + foreach ($loader->getNamespaces() as $namespace) { + if (FilesystemLoader::MAIN_NAMESPACE === $namespace) { + $paths[''] = $loader->getPaths($namespace); + } else { + $paths['@'.$namespace] = $loader->getPaths($namespace); + } + } + + return $paths; + } + private function getMetadata($type, $entity) { if ($type === 'globals') { From 7cd2b2942a4888c40e68b0a1350bff5da8155242 Mon Sep 17 00:00:00 2001 From: Yonel Ceruto Date: Fri, 1 Sep 2017 15:09:45 -0400 Subject: [PATCH 2/3] Show short path (from project root) and display table --- .../Bridge/Twig/Command/DebugCommand.php | 37 ++++++++++++++----- .../TwigBundle/Resources/config/console.xml | 1 + 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/src/Symfony/Bridge/Twig/Command/DebugCommand.php b/src/Symfony/Bridge/Twig/Command/DebugCommand.php index 48e207fdf2692..e0f2efaec036c 100644 --- a/src/Symfony/Bridge/Twig/Command/DebugCommand.php +++ b/src/Symfony/Bridge/Twig/Command/DebugCommand.php @@ -12,6 +12,7 @@ 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; @@ -30,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); @@ -47,6 +50,7 @@ public function __construct($twig = null) parent::__construct(); $this->twig = $twig; + $this->projectDir = $projectDir; } public function setTwigEnvironment(Environment $twig) @@ -147,14 +151,17 @@ protected function execute(InputInterface $input, OutputInterface $output) $io->listing($items); } - $list = array(); + $rows = array(); foreach ($this->getLoaderPaths() as $namespace => $paths) { - $list = array_merge($list, array_map(function ($path) use ($namespace) { - return $namespace.($namespace ? ': ' : '').$path; - }, $paths)); + foreach ($paths as $path) { + $rows[] = array($namespace, '* '.$path); + $namespace = ''; + } + $rows[] = new TableSeparator(); } + array_pop($rows); $io->section('Loader Paths'); - $io->listing($list); + $io->table(array('Namespace', 'Paths'), $rows); return 0; } @@ -165,16 +172,26 @@ private function getLoaderPaths() return array(); } - $paths = 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) { - $paths[''] = $loader->getPaths($namespace); + $namespace = '(None)'; } else { - $paths['@'.$namespace] = $loader->getPaths($namespace); + $namespace = '@'.$namespace; } + + $loaderPaths[$namespace] = $paths; } - return $paths; + return $loaderPaths; } private function getMetadata($type, $entity) diff --git a/src/Symfony/Bundle/TwigBundle/Resources/config/console.xml b/src/Symfony/Bundle/TwigBundle/Resources/config/console.xml index a8997823749fe..d716f309f451d 100644 --- a/src/Symfony/Bundle/TwigBundle/Resources/config/console.xml +++ b/src/Symfony/Bundle/TwigBundle/Resources/config/console.xml @@ -9,6 +9,7 @@ + %kernel.project_dir% From ddde74f6e11cf910b1b7f4be09566b82f9c5c1d9 Mon Sep 17 00:00:00 2001 From: Yonel Ceruto Date: Fri, 1 Sep 2017 17:03:51 -0400 Subject: [PATCH 3/3] Seven wins the fight ... for now ... :) --- src/Symfony/Bridge/Twig/Command/DebugCommand.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Bridge/Twig/Command/DebugCommand.php b/src/Symfony/Bridge/Twig/Command/DebugCommand.php index e0f2efaec036c..258387173a540 100644 --- a/src/Symfony/Bridge/Twig/Command/DebugCommand.php +++ b/src/Symfony/Bridge/Twig/Command/DebugCommand.php @@ -12,7 +12,6 @@ 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; @@ -153,11 +152,16 @@ protected function execute(InputInterface $input, OutputInterface $output) $rows = array(); foreach ($this->getLoaderPaths() as $namespace => $paths) { + if (count($paths) > 1) { + $rows[] = array('', ''); + } foreach ($paths as $path) { - $rows[] = array($namespace, '* '.$path); + $rows[] = array($namespace, '- '.$path); $namespace = ''; } - $rows[] = new TableSeparator(); + if (count($paths) > 1) { + $rows[] = array('', ''); + } } array_pop($rows); $io->section('Loader Paths');