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:
commit
aa5b6f95b9
@ -19,6 +19,7 @@ use Symfony\Component\Console\Input\InputOption;
|
|||||||
use Symfony\Component\Console\Output\OutputInterface;
|
use Symfony\Component\Console\Output\OutputInterface;
|
||||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||||
use Symfony\Component\Finder\Finder;
|
use Symfony\Component\Finder\Finder;
|
||||||
|
use Symfony\Component\HttpKernel\Debug\FileLinkFormatter;
|
||||||
use Twig\Environment;
|
use Twig\Environment;
|
||||||
use Twig\Loader\ChainLoader;
|
use Twig\Loader\ChainLoader;
|
||||||
use Twig\Loader\FilesystemLoader;
|
use Twig\Loader\FilesystemLoader;
|
||||||
@ -38,8 +39,9 @@ class DebugCommand extends Command
|
|||||||
private $twigDefaultPath;
|
private $twigDefaultPath;
|
||||||
private $rootDir;
|
private $rootDir;
|
||||||
private $filesystemLoaders;
|
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();
|
parent::__construct();
|
||||||
|
|
||||||
@ -48,6 +50,7 @@ class DebugCommand extends Command
|
|||||||
$this->bundlesMetadata = $bundlesMetadata;
|
$this->bundlesMetadata = $bundlesMetadata;
|
||||||
$this->twigDefaultPath = $twigDefaultPath;
|
$this->twigDefaultPath = $twigDefaultPath;
|
||||||
$this->rootDir = $rootDir;
|
$this->rootDir = $rootDir;
|
||||||
|
$this->fileLinkFormatter = $fileLinkFormatter;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function configure()
|
protected function configure()
|
||||||
@ -105,16 +108,28 @@ EOF
|
|||||||
|
|
||||||
private function displayPathsText(SymfonyStyle $io, string $name)
|
private function displayPathsText(SymfonyStyle $io, string $name)
|
||||||
{
|
{
|
||||||
$files = $this->findTemplateFiles($name);
|
$file = new \ArrayIterator($this->findTemplateFiles($name));
|
||||||
$paths = $this->getLoaderPaths($name);
|
$paths = $this->getLoaderPaths($name);
|
||||||
|
|
||||||
$io->section('Matched File');
|
$io->section('Matched File');
|
||||||
if ($files) {
|
if ($file->valid()) {
|
||||||
$io->success(array_shift($files));
|
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->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 {
|
} else {
|
||||||
$alternatives = [];
|
$alternatives = [];
|
||||||
@ -453,9 +468,9 @@ EOF
|
|||||||
|
|
||||||
if (is_file($filename)) {
|
if (is_file($filename)) {
|
||||||
if (false !== $realpath = realpath($filename)) {
|
if (false !== $realpath = realpath($filename)) {
|
||||||
$files[] = $this->getRelativePath($realpath);
|
$files[$realpath] = $this->getRelativePath($realpath);
|
||||||
} else {
|
} else {
|
||||||
$files[] = $this->getRelativePath($filename);
|
$files[$filename] = $this->getRelativePath($filename);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -563,4 +578,13 @@ EOF
|
|||||||
|
|
||||||
return $this->filesystemLoaders;
|
return $this->filesystemLoaders;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function getFileLink(string $absolutePath): string
|
||||||
|
{
|
||||||
|
if (null === $this->fileLinkFormatter) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
|
return (string) $this->fileLinkFormatter->format($absolutePath, 1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -13,6 +13,7 @@
|
|||||||
<argument>%kernel.bundles_metadata%</argument>
|
<argument>%kernel.bundles_metadata%</argument>
|
||||||
<argument>%twig.default_path%</argument>
|
<argument>%twig.default_path%</argument>
|
||||||
<argument>%kernel.root_dir%</argument>
|
<argument>%kernel.root_dir%</argument>
|
||||||
|
<argument type="service" id="debug.file_link_formatter" on-invalid="null" />
|
||||||
<tag name="console.command" command="debug:twig" />
|
<tag name="console.command" command="debug:twig" />
|
||||||
</service>
|
</service>
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user