10000 [TwigBridge] Fix DebugCommand when chain loader is involved by yceruto · Pull Request #30440 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[TwigBridge] Fix DebugCommand when chain loader is involved #30440

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
Mar 20, 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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 60 additions & 32 deletions src/Symfony/Bridge/Twig/Command/DebugCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Finder\Finder;
use Twig\Environment;
use Twig\Loader\ChainLoader;
use Twig\Loader\FilesystemLoader;

/**
Expand All @@ -36,6 +37,7 @@ class DebugCommand extends Command
private $bundlesMetadata;
private $twigDefaultPath;
private $rootDir;
private $filesystemLoaders;

public function __construct(Environment $twig, string $projectDir = null, array $bundlesMetadata = [], string $twigDefaultPath = null, string $rootDir = null)
{
Expand Down< 10000 /tool-tip> Expand Up @@ -87,7 +89,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$name = $input->getArgument('name');
$filter = $input->getOption('filter');

if (null !== $name && !$this->twig->getLoader() instanceof FilesystemLoader) {
if (null !== $name && [] === $this->getFilesystemLoaders()) {
throw new InvalidArgumentException(sprintf('Argument "name" not supported, it requires the Twig loader "%s"', FilesystemLoader::class));
}

Expand Down Expand Up @@ -150,9 +152,11 @@ private function displayPathsText(SymfonyStyle $io, string $name)
$message = 'No template paths configured for your application';
} else {
$message = sprintf('No template paths configured for "@%s" namespace', $namespace);
$namespaces = $this->twig->getLoader()->getNamespaces();
foreach ($this->findAlternatives($namespace, $namespaces) as $namespace) {
$alternatives[] = '@'.$namespace;
foreach ($this->getFilesystemLoaders() as $loader) {
$namespaces = $loader->getNamespaces();
foreach ($this->findAlternatives($namespace, $namespaces) as $namespace) {
$alternatives[] = '@'.$namespace;
}
}
}

Expand Down Expand Up @@ -243,25 +247,25 @@ private function displayGeneralJson(SymfonyStyle $io, $filter)

private function getLoaderPaths(string $name = null): array
{
/** @var FilesystemLoader $loader */
$loader = $this->twig->getLoader();
$loaderPaths = [];
$namespaces = $loader->getNamespaces();
if (null !== $name) {
$namespace = $this->parseTemplateName($name)[0];
$namespaces = array_intersect([$namespace], $namespaces);
}
foreach ($this->getFilesystemLoaders() as $loader) {
$namespaces = $loader->getNamespaces();
if (null !== $name) {
$namespace = $this->parseTemplateName($name)[0];
$namespaces = array_intersect([$namespace], $namespaces);
}

foreach ($namespaces as $namespace) {
$paths = array_map([$this, 'getRelativePath'], $loader->getPaths($namespace));
foreach ($namespaces as $namespace) {
$paths = array_map([$this, 'getRelativePath'], $loader->getPaths($namespace));

if (FilesystemLoader::MAIN_NAMESPACE === $namespace) {
$namespace = '(None)';
} else {
$namespace = '@'.$namespace;
}
if (FilesystemLoader::MAIN_NAMESPACE === $namespace) {
$namespace = '(None)';
} else {
$namespace = '@'.$namespace;
}

$loaderPaths[$namespace] = $paths;
$loaderPaths[$namespace] = $paths;
}
}

return $loaderPaths;
Expand Down Expand Up @@ -437,22 +441,22 @@ private function error(SymfonyStyle $io, string $message, array $alternatives =

private function findTemplateFiles(string $name): array
{
/** @var FilesystemLoader $loader */
$loader = $this->twig->getLoader();
$files = [];
list($namespace, $shortname) = $this->parseTemplateName($name);

foreach ($loader->getPaths($namespace) as $path) {
if (!$this->isAbsolutePath($path)) {
$path = $this->projectDir.'/'.$path;
}
$filename = $path.'/'.$shortname;
$files = [];
foreach ($this->getFilesystemLoaders() as $loader) {
foreach ($loader->getPaths($namespace) as $path) {
if (!$this->isAbsolutePath($path)) {
$path = $this->projectDir.'/'.$path;
}
$filename = $path.'/'.$shortname;

if (is_file($filename)) {
if (false !== $realpath = realpath($filename)) {
$files[] = $this->getRelativePath($realpath);
} else {
$files[] = $this->getRelativePath($filename);
if (is_file($filename)) {
if (false !== $realpath = realpath($filename)) {
$files[] = $this->getRelativePath($realpath);
} else {
$files[] = $this->getRelativePath($filename);
}
}
}
}
Expand Down Expand Up @@ -535,4 +539,28 @@ private function isAbsolutePath(string $file): bool
{
return strspn($file, '/\\', 0, 1) || (\strlen($file) > 3 && ctype_alpha($file[0]) && ':' === $file[1] && strspn($file, '/\\', 2, 1)) || null !== parse_url($file, PHP_URL_SCHEME);
}

/**
* @return FilesystemLoader[]
*/
private function getFilesystemLoaders(): array
{
if (null !== $this->filesystemLoaders) {
return $this->filesystemLoaders;
}
$this->filesystemLoaders = [];

$loader = $this->twig->getLoader();
if ($loader instanceof FilesystemLoader) {
$this->filesystemLoaders[] = $loader;
} elseif ($loader instanceof ChainLoader) {
foreach ($loader->getLoaders() as $l) {
if ($l instanceof FilesystemLoader) {
$this->filesystemLoaders[] = $l;
}
}
}

return $this->filesystemLoaders;
}
}
16 changes: 15 additions & 1 deletion src/Symfony/Bridge/Twig/Tests/Command/DebugCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;
use Twig\Environment;
use Twig\Loader\ChainLoader;
use Twig\Loader\FilesystemLoader;

class DebugCommandTest extends TestCase
Expand Down Expand Up @@ -279,7 +280,16 @@ public function getDebugTemplateNameTestData()
];
}

private function createCommandTester(array $paths = [], array $bundleMetadata = [], string $defaultPath = null, string $rootDir = null): CommandTester
public function testDebugTemplateNameWithChainLoader()
{
$tester = $this->createCommandTester(['templates/' => null], [], null, null, true);
$ret = $tester->execute(['name' => 'base.html.twig'], ['decorated' => false]);

$this CAC9 ->assertEquals(0, $ret, 'Returns 0 in case of success');
$this->assertContains('[OK]', $tester->getDisplay());
}

private function createCommandTester(array $paths = [], array $bundleMetadata = [], string $defaultPath = null, string $rootDir = null, bool $useChainLoader = false): CommandTester
{
$projectDir = \dirname(__DIR__).\DIRECTORY_SEPARATOR.'Fixtures';
$loader = new FilesystemLoader([], $projectDir);
Expand All @@ -291,6 +301,10 @@ private function createCommandTester(array $paths = [], array $bundleMetadata =
}
}

if ($useChainLoader) {
$loader = new ChainLoader([$loader]);
}

$application = new Application();
$application->add(new DebugCommand(new Environment($loader), $projectDir, $bundleMetadata, $defaultPath, $rootDir));
$command = $application->find('debug:twig');
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Bridge/Twig/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"require": {
"php": "^7.1.3",
"symfony/contracts": "^1.0.2",
"twig/twig": "^1.37.1|^2.6.2"
"twig/twig": "^1.38.1|^2.7.1"
},
"require-dev": {
"symfony/asset": "~3.4|~4.0",
Expand Down
0