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

Commit 77b6cc6

Browse filesBrowse files
committed
feature #24064 [TwigBridge] Show Twig's loader paths on debug:twig command (yceruto)
This PR was squashed before being merged into the 3.4 branch (closes #24064). Discussion ---------- [TwigBridge] Show Twig's loader paths on debug:twig command | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - `bin/console debug:twig`: ![twig-loader-paths](https://user-images.githubusercontent.com/2028198/29986784-ee5a4094-8f32-11e7-86c8-c78183630221.png ) This information is not displayed anywhere ATM and it should be important to know: * The Twig's namespaces availables * The Twig's paths availables * The order that templates will be loaded ( regarding its namespace -> LOAD PRIORITY ! ) So it should help us to debug any issue related to circular templates reference, invalid namespaces, unsuccessful attempt to override a template, etc. WDYT? Commits ------- 3fdcb40 [TwigBridge] Show Twig's loader paths on debug:twig command
2 parents c0b4e56 + 3fdcb40 commit 77b6cc6
Copy full SHA for 77b6cc6

File tree

Expand file treeCollapse file tree

2 files changed

+52
-1
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+52
-1
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Twig/Command/DebugCommand.php
+51-1Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/TwigBundle/Resources/config/console.xml
+1Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.