8000 [TwigBridge] Add template file link to debug:twig command by yceruto · Pull Request #30827 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[TwigBridge] Add template file link to debug:twig command #30827

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
8000 Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 32 additions & 8 deletions src/Symfony/Bridge/Twig/Command/DebugCommand.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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