Skip to content

Commit b4057b5

Browse files
bug #27728 [TwigBridge] Fix missing path and separators in loader paths list on debug:twig output (yceruto)
This PR was squashed before being merged into the 3.4 branch (closes #27728). Discussion ---------- [TwigBridge] Fix missing path and separators in loader paths list on debug:twig output | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - **Reproducer** ```bash $ composer create-project symfony/skeleton repr $ cd repr $ composer req twig # remove duplicated path on `twig.paths: ['%kernel.project_dir%/templates']` config. $ bin/console debug:twig ``` See "Loader Paths" section at the end of the output and compare it with `--format=json`. Note that the root `templates` path is present in Twig `loader_paths` because `twig.default_path` config, but it isn't visible on `debug:twig` output. **Missing path:** | Before | After | | --- | --- | | ![debug_twig_before1](https://user-images.githubusercontent.com/2028198/41908937-9af78274-7913-11e8-91d1-bb48f81031dd.png) | ![debug_twig_after1](https://user-images.githubusercontent.com/2028198/41908969-b5fd3140-7913-11e8-8129-8215f3c09b24.png) | **Extra Lines separator:** | Before | After | | --- | --- | | ![debug_twig_before2](https://user-images.githubusercontent.com/2028198/41909029-e867fcdc-7913-11e8-91f1-bba0d873495c.png) | ![debug_twig_after2](https://user-images.githubusercontent.com/2028198/41915586-12a6426e-7924-11e8-899f-9ecc7d95b74f.png) | Commits ------- ba7a4ca863 [TwigBridge] Fix missing path and separators in loader paths list on debug:twig output
2 parents 782ac8d + 2308822 commit b4057b5

File tree

2 files changed

+92
-3
lines changed

2 files changed

+92
-3
lines changed

Command/DebugCommand.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,19 +151,27 @@ protected function execute(InputInterface $input, OutputInterface $output)
151151
}
152152

153153
$rows = array();
154+
$firstNamespace = true;
155+
$prevHasSeparator = false;
154156
foreach ($this->getLoaderPaths() as $namespace => $paths) {
155-
if (count($paths) > 1) {
157+
if (!$firstNamespace && !$prevHasSeparator && count($paths) > 1) {
156158
$rows[] = array('', '');
157159
}
160+
$firstNamespace = false;
158161
foreach ($paths as $path) {
159-
$rows[] = array($namespace, '- '.$path);
162+
$rows[] = array($namespace, $path.DIRECTORY_SEPARATOR);
160163
$namespace = '';
161164
}
162165
if (count($paths) > 1) {
163166
$rows[] = array('', '');
167+
$prevHasSeparator = true;
168+
} else {
169+
$prevHasSeparator = false;
164170
}
165171
}
166-
array_pop($rows);
172+
if ($prevHasSeparator) {
173+
array_pop($rows);
174+
}
167175
$io->section('Loader Paths');
168176
$io->table(array('Namespace', 'Paths'), $rows);
169177

Tests/Command/DebugCommandTest.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Twig\Tests\Command;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Bridge\Twig\Command\DebugCommand;
16+
use Symfony\Component\Console\Application;
17+
use Symfony\Component\Console\Tester\CommandTester;
18+
use Twig\Loader\FilesystemLoader;
19+
use Twig\Environment;
20+
21+
class DebugCommandTest extends TestCase
22+
{
23+
public function testDebugCommand()
24+
{
25+
$tester = $this->createCommandTester();
26+
$ret = $tester->execute(array(), array('decorated' => false));
27+
28+
$this->assertEquals(0, $ret, 'Returns 0 in case of success');
29+
$this->assertContains('Functions', trim($tester->getDisplay()));
30+
}
31+
32+
public function testLineSeparatorInLoaderPaths()
33+
{
34+
// these paths aren't realistic,
35+
// they're configured to force the line separator
36+
$tester = $this->createCommandTester(array(
37+
'Acme' => array('extractor', 'extractor'),
38+
'!Acme' => array('extractor', 'extractor'),
39+
FilesystemLoader::MAIN_NAMESPACE => array('extractor', 'extractor'),
40+
));
41+
$ret = $tester->execute(array(), array('decorated' => false));
42+
$ds = DIRECTORY_SEPARATOR;
43+
$loaderPaths = <<<TXT
44+
Loader Paths
45+
------------
46+
47+
----------- ------------
48+
Namespace Paths
49+
----------- ------------
50+
@Acme extractor$ds
51+
extractor$ds
52+
53+
@!Acme extractor$ds
54+
extractor$ds
55+
56+
(None) extractor$ds
57+
extractor$ds
58+
----------- ------------
59+
TXT;
60+
61+
$this->assertEquals(0, $ret, 'Returns 0 in case of success');
62+
$this->assertContains($loaderPaths, trim($tester->getDisplay(true)));
63+
}
64+
65+
private function createCommandTester(array $paths = array())
66+
{
67+
$filesystemLoader = new FilesystemLoader(array(), dirname(__DIR__).'/Fixtures');
68+
foreach ($paths as $namespace => $relDirs) {
69+
foreach ($relDirs as $relDir) {
70+
$filesystemLoader->addPath($relDir, $namespace);
71+
}
72+
}
73+
$command = new DebugCommand(new Environment($filesystemLoader));
74+
75+
$application = new Application();
76+
$application->add($command);
77+
$command = $application->find('debug:twig');
78+
79+
return new CommandTester($command);
80+
}
81+
}

0 commit comments

Comments
 (0)