Skip to content

Commit

Permalink
feature #30827 [TwigBridge] Add template file link to debug:twig comm…
Browse files Browse the repository at this point in the history
…and (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
  • Loading branch information
fabpot committed Apr 2, 2019
2 parents a63496b + 05e2e1e commit aa5b6f9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
40 changes: 32 additions & 8 deletions src/Symfony/Bridge/Twig/Command/DebugCommand.php
Expand Up @@ -19,6 +19,7 @@
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Finder\Finder;
use Symfony\Component\HttpKernel\Debug\FileLinkFormatter;
use Twig\Environment;
use Twig\Loader\ChainLoader;
use Twig\Loader\FilesystemLoader;
Expand All @@ -38,8 +39,9 @@ class DebugCommand extends Command
private $twigDefaultPath;
private $rootDir;
private $filesystemLoaders;
private $fileLinkFormatter;

public function __construct(Environment $twig, string $projectDir = null, array $bundlesMetadata = [], string $twigDefaultPath = null, string $rootDir = null)
public function __construct(Environment $twig, string $projectDir = null, array $bundlesMetadata = [], string $twigDefaultPath = null, string $rootDir = null, FileLinkFormatter $fileLinkFormatter = null)
{
parent::__construct();

Expand All @@ -48,6 +50,7 @@ public function __construct(Environment $twig, string $projectDir = null, array
$this->bundlesMetadata = $bundlesMetadata;
$this->twigDefaultPath = $twigDefaultPath;
$this->rootDir = $rootDir;
$this->fileLinkFormatter = $fileLinkFormatter;
}

protected function configure()
Expand Down Expand Up @@ -105,16 +108,28 @@ protected function execute(InputInterface $input, OutputInterface $output)

private function displayPathsText(SymfonyStyle $io, string $name)
{
$files = $this->findTemplateFiles($name);
$file = new \ArrayIterator($this->findTemplateFiles($name));
$paths = $this->getLoaderPaths($name);

$io->section('Matched File');
if ($files) {
$io->success(array_shift($files));
if ($file->valid()) {
if ($fileLink = $this->getFileLink($file->key())) {
$io->block($file->current(), 'OK', sprintf('fg=black;bg=green;href=%s', $fileLink), ' ', true);
} else {
$io->success($file->current());
}
$file->next();

if ($files) {
if ($file->valid()) {
$io->section('Overridden Files');
$io->listing($files);
do {
if ($fileLink = $this->getFileLink($file->key())) {
$io->text(sprintf('* <href=%s>%s</>', $fileLink, $file->current()));
} else {
$io->text(sprintf('* %s', $file->current()));
}
$file->next();
} while ($file->valid());
}
} else {
$alternatives = [];
Expand Down Expand Up @@ -453,9 +468,9 @@ private function findTemplateFiles(string $name): array

if (is_file($filename)) {
if (false !== $realpath = realpath($filename)) {
$files[] = $this->getRelativePath($realpath);
$files[$realpath] = $this->getRelativePath($realpath);
} else {
$files[] = $this->getRelativePath($filename);
$files[$filename] = $this->getRelativePath($filename);
}
}
}
Expand Down Expand Up @@ -563,4 +578,13 @@ private function getFilesystemLoaders(): array

return $this->filesystemLoaders;
}

private function getFileLink(string $absolutePath): string
{
if (null === $this->fileLinkFormatter) {
return '';
}

return (string) $this->fileLinkFormatter->format($absolutePath, 1);
}
}
1 change: 1 addition & 0 deletions src/Symfony/Bundle/TwigBundle/Resources/config/console.xml
Expand Up @@ -13,6 +13,7 @@
<argument>%kernel.bundles_metadata%</argument>
<argument>%twig.default_path%</argument>
<argument>%kernel.root_dir%</argument>
<argument type="service" id="debug.file_link_formatter" on-invalid="null" />
<tag name="console.command" command="debug:twig" />
</service>

Expand Down

0 comments on commit aa5b6f9

Please sign in to comment.