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
-------

05e2e1e088 Add template file link
This commit is contained in:
Fabien Potencier 2019-04-02 09:00:23 +02:00
commit aa5b6f95b9
2 changed files with 33 additions and 8 deletions

View File

@ -19,6 +19,7 @@ use Symfony\Component\Console\Input\InputOption;
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;
@ -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();
@ -48,6 +50,7 @@ class DebugCommand extends Command
$this->bundlesMetadata = $bundlesMetadata;
$this->twigDefaultPath = $twigDefaultPath;
$this->rootDir = $rootDir;
$this->fileLinkFormatter = $fileLinkFormatter;
}
protected function configure()
@ -105,16 +108,28 @@ EOF
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 = [];
@ -453,9 +468,9 @@ EOF
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);
}
}
}
@ -563,4 +578,13 @@ EOF
return $this->filesystemLoaders;
}
private function getFileLink(string $absolutePath): string
{
if (null === $this->fileLinkFormatter) {
return '';
}
return (string) $this->fileLinkFormatter->format($absolutePath, 1);
}
}

View File

@ -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>