Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3fdcb40

Browse files
ycerutofabpot
authored andcommitted
[TwigBridge] Show Twig's loader paths on debug:twig command
1 parent 042328d commit 3fdcb40

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

src/Symfony/Bridge/Twig/Command/DebugCommand.php

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Component\Console\Output\OutputInterface;
1919
use Symfony\Component\Console\Style\SymfonyStyle;
2020
use Twig\Environment;
21+
use Twig\Loader\FilesystemLoader;
2122

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

3132
private $twig;
33+
private $projectDir;
3234

3335
/**
3436
* @param Environment $twig
37+
* @param string|null $projectDir
3538
*/
36-
public function __construct($twig = null)
39+
public function __construct($twig = null, $projectDir = null)
3740
{
3841
if (!$twig instanceof Environment) {
3942
@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);
@@ -46,6 +49,7 @@ public function __construct($twig = null)
4649
parent::__construct();
4750

4851
$this->twig = $twig;
52+
$this->projectDir = $projectDir;
4953
}
5054

5155
public function setTwigEnvironment(Environment $twig)
@@ -120,6 +124,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
120124
}
121125
}
122126
$data['tests'] = array_keys($data['tests']);
127+
$data['loader_paths'] = $this->getLoaderPaths();
123128
$io->writeln(json_encode($data));
124129

125130
return 0;
@@ -145,9 +150,54 @@ protected function execute(InputInterface $input, OutputInterface $output)
145150
$io->listing($items);
146151
}
147152

153+
$rows = array();
154+
foreach ($this->getLoaderPaths() as $namespace => $paths) {
155+
if (count($paths) > 1) {
156+
$rows[] = array('', '');
157+
}
158+
foreach ($paths as $path) {
159+
$rows[] = array($namespace, '- '.$path);
160+
$namespace = '';
161+
}
162+
if (count($paths) > 1) {
163+
$rows[] = array('', '');
164+
}
165+
}
166+
array_pop($rows);
167+
$io->section('Loader Paths');
168+
$io->table(array('Namespace', 'Paths'), $rows);
169+
148170
return 0;
149171
}
150172

173+
private function getLoaderPaths()
174+
{
175+
if (!($loader = $this->twig->getLoader()) instanceof FilesystemLoader) {
176+
return array();
177+
}
178+
179+
$loaderPaths = array();
180+
foreach ($loader->getNamespaces() as $namespace) {
181+
$paths = array_map(function ($path) use ($namespace) {
182+
if (null !== $this->projectDir && 0 === strpos($path, $this->projectDir)) {
183+
$path = ltrim(substr($path, strlen($this->projectDir)), DIRECTORY_SEPARATOR);
184+
}
185+
186+
return $path;
187+
}, $loader->getPaths($namespace));
188+
189+
if (FilesystemLoader::MAIN_NAMESPACE === $namespace) {
190+
$namespace = '(None)';
191+
} else {
192+
$namespace = '@'.$namespace;
193+
}
194+
195+
$loaderPaths[$namespace] = $paths;
196+
}
197+
198+
return $loaderPaths;
199+
}
200+
151201
private function getMetadata($type, $entity)
152202
{
153203
if ($type === 'globals') {

src/Symfony/Bundle/TwigBundle/Resources/config/console.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
<service id="Symfony\Bridge\Twig\Command\DebugCommand">
1111
<argument type="service" id="twig" />
12+
<argument>%kernel.project_dir%</argument>
1213
<tag name="console.command" command="debug:twig" />
1314
</service>
1415

0 commit comments

Comments
 (0)