Skip to content

Commit aa5b6f9

Browse files
committed
feature #30827 [TwigBridge] Add template file link to debug:twig command (yceruto)
This PR was merged into the 4.3-dev branch. Discussion ---------- [TwigBridge] Add template file link to debug:twig command | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT ![debug_twig_file_link](https://user-images.githubusercontent.com/2028198/55365428-8c85c680-54b2-11e9-9d4e-e4845fc7d411.png) Commits ------- 05e2e1e Add template file link
2 parents a63496b + 05e2e1e commit aa5b6f9

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed

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

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Component\Console\Output\OutputInterface;
2020
use Symfony\Component\Console\Style\SymfonyStyle;
2121
use Symfony\Component\Finder\Finder;
22+
use Symfony\Component\HttpKernel\Debug\FileLinkFormatter;
2223
use Twig\Environment;
2324
use Twig\Loader\ChainLoader;
2425
use Twig\Loader\FilesystemLoader;
@@ -38,8 +39,9 @@ class DebugCommand extends Command
3839
private $twigDefaultPath;
3940
private $rootDir;
4041
private $filesystemLoaders;
42+
private $fileLinkFormatter;
4143

42-
public function __construct(Environment $twig, string $projectDir = null, array $bundlesMetadata = [], string $twigDefaultPath = null, string $rootDir = null)
44+
public function __construct(Environment $twig, string $projectDir = null, array $bundlesMetadata = [], string $twigDefaultPath = null, string $rootDir = null, FileLinkFormatter $fileLinkFormatter = null)
4345
{
4446
parent::__construct();
4547

@@ -48,6 +50,7 @@ public function __construct(Environment $twig, string $projectDir = null, array
4850
$this->bundlesMetadata = $bundlesMetadata;
4951
$this->twigDefaultPath = $twigDefaultPath;
5052
$this->rootDir = $rootDir;
53+
$this->fileLinkFormatter = $fileLinkFormatter;
5154
}
5255

5356
protected function configure()
@@ -105,16 +108,28 @@ protected function execute(InputInterface $input, OutputInterface $output)
105108

106109
private function displayPathsText(SymfonyStyle $io, string $name)
107110
{
108-
$files = $this->findTemplateFiles($name);
111+
$file = new \ArrayIterator($this->findTemplateFiles($name));
109112
$paths = $this->getLoaderPaths($name);
110113

111114
$io->section('Matched File');
112-
if ($files) {
113-
$io->success(array_shift($files));
115+
if ($file->valid()) {
116+
if ($fileLink = $this->getFileLink($file->key())) {
117+
$io->block($file->current(), 'OK', sprintf('fg=black;bg=green;href=%s', $fileLink), ' ', true);
118+
} else {
119+
$io->success($file->current());
120+
}
121+
$file->next();
114122

115-
if ($files) {
123+
if ($file->valid()) {
116124
$io->section('Overridden Files');
117-
$io->listing($files);
125+
do {
126+
if ($fileLink = $this->getFileLink($file->key())) {
127+
$io->text(sprintf('* <href=%s>%s</>', $fileLink, $file->current()));
128+
} else {
129+
$io->text(sprintf('* %s', $file->current()));
130+
}
131+
$file->next();
132+
} while ($file->valid());
118133
}
119134
} else {
120135
$alternatives = [];
@@ -453,9 +468,9 @@ private function findTemplateFiles(string $name): array
453468

454469
if (is_file($filename)) {
455470
if (false !== $realpath = realpath($filename)) {
456-
$files[] = $this->getRelativePath($realpath);
471+
$files[$realpath] = $this->getRelativePath($realpath);
457472
} else {
458-
$files[] = $this->getRelativePath($filename);
473+
$files[$filename] = $this->getRelativePath($filename);
459474
}
460475
}
461476
}
@@ -563,4 +578,13 @@ private function getFilesystemLoaders(): array
563578

564579
return $this->filesystemLoaders;
565580
}
581+
582+
private function getFileLink(string $absolutePath): string
583+
{
584+
if (null === $this->fileLinkFormatter) {
585+
return '';
586+
}
587+
588+
return (string) $this->fileLinkFormatter->format($absolutePath, 1);
589+
}
566590
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<argument>%kernel.bundles_metadata%</argument>
1414
<argument>%twig.default_path%</argument>
1515
<argument>%kernel.root_dir%</argument>
16+
<argument type="service" id="debug.file_link_formatter" on-invalid="null" />
1617
<tag name="console.command" command="debug:twig" />
1718
</service>
1819

0 commit comments

Comments
 (0)