8000 Fix DebugCommand when chain loader is involved · symfony/symfony@cc3c90d · GitHub
[go: up one dir, main page]

Skip to content

Commit cc3c90d

Browse files
committed
Fix DebugCommand when chain loader is involved
1 parent 675c458 commit cc3c90d

File tree

2 files changed

+61
-33
lines changed

2 files changed

+61
-33
lines changed

src/Symfony/Bridge/Twig/Command/DebugCommand.php

Lines changed: 60 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Symfony\Component\Console\Style\SymfonyStyle;
2121
use Symfony\Component\Finder\Finder;
2222
use Twig\Environment;
23+
use Twig\Loader\ChainLoader;
2324
use Twig\Loader\FilesystemLoader;
2425

2526
/**
@@ -36,6 +37,7 @@ class DebugCommand extends Command
3637
private $bundlesMetadata;
3738
private $twigDefaultPath;
3839
private $rootDir;
40+
private $filesystemLoaders;
3941

4042
public function __construct(Environment $twig, string $projectDir = null, array $bundlesMetadata = [], string $twigDefaultPath = null, string $rootDir = null)
4143
{
@@ -87,7 +89,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
8789
$name = $input->getArgument('name');
8890
$filter = $input->getOption('filter');
8991

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

@@ -150,9 +152,11 @@ private function displayPathsText(SymfonyStyle $io, string $name)
150152
$message = 'No template paths configured for your application';
151153
} else {
152154
$message = sprintf('No template paths configured for "@%s" namespace', $namespace);
153-
$namespaces = $this->twig->getLoader()->getNamespaces();
154-
foreach ($this->findAlternatives($namespace, $namespaces) as $namespace) {
155-
$alternatives[] = '@'.$namespace;
155+
foreach ($this->getFilesystemLoaders() as $loader) {
156+
$namespaces = $loader->getNamespaces();
157+
foreach ($this->findAlternatives($namespace, $namespaces) as $namespace) {
158+
$alternatives[] = '@'.$namespace;
159+
}
156160
}
157161
}
158162

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

244248
private function getLoaderPaths(string $name = null): array
245249
{
246-
/** @var FilesystemLoader $loader */
247-
$loader = $this->twig->getLoader();
248250
$loaderPaths = [];
249-
$namespaces = $loader->getNamespaces();
250-
if (null !== $name) {
251-
$namespace = $this->parseTemplateName($name)[0];
252-
$namespaces = array_intersect([$namespace], $namespaces);
253-
}
251+
foreach ($this->getFilesystemLoaders() as $loader) {
252+
$namespaces = $loader->getNamespaces();
253+
if (null !== $name) {
254+
$namespace = $this->parseTemplateName($name)[0];
255+
$namespaces = array_intersect([$namespace], $namespaces);
256+
}
254257

255-
foreach ($namespaces as $namespace) {
256-
$paths = array_map([$this, 'getRelativePath'], $loader->getPaths($namespace));
258+
foreach ($namespaces as $namespace) {
259+
$paths = array_map([$this, 'getRelativePath'], $loader->getPaths($namespace));
257260

258-
if (FilesystemLoader::MAIN_NAMESPACE === $namespace) {
259-
$namespace = '(None)';
260-
} else {
261-
$namespace = '@'.$namespace;
262-
}
261+
if (FilesystemLoader::MAIN_NAMESPACE === $namespace) {
262+
$namespace = '(None)';
263+
} else {
264+
$namespace = '@'.$namespace;
265+
}
263266

264-
$loaderPaths[$namespace] = $paths;
267+
$loaderPaths[$namespace] = $paths;
268+
}
265269
}
266270

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

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

445-
foreach ($loader->getPaths($namespace) as $path) {
446-
if (!$this->isAbsolutePath($path)) {
447-
$path = $this->projectDir.'/'.$path;
448-
}
449-
$filename = $path.'/'.$shortname;
446+
$files = [];
447+
foreach ($this->getFilesystemLoaders() as $loader) {
448+
foreach ($loader->getPaths($namespace) as $path) {
449+
if (!$this->isAbsolutePath($path)) {
450+
$path = $this->projectDir.'/'.$path;
451+
}
452+
$filename = $path.'/'.$shortname;
450453

451-
if (is_file($filename)) {
452-
if (false !== $realpath = realpath($filename)) {
453-
$files[] = $this->getRelativePath($realpath);
454-
} else {
455-
$files[] = $this->getRelativePath($filename);
454+
if (is_file($filename)) {
455+
if (false !== $realpath = realpath($filename)) {
456+
$files[] = $this->getRelativePath($realpath);
457+
} else {
458+
$files[] = $this->getRelativePath($filename);
459+
}
456460
}
457461
}
458462
}
@@ -535,4 +539,28 @@ private function isAbsolutePath(string $file): bool
535539
{
536540
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);
537541
}
542+
543+
/**
544+
* @return FilesystemLoader[]
545+
*/
546+
private function getFilesystemLoaders(): array
547+
{
548+
if (null !== $this->filesystemLoaders) {
549+
return $this->filesystemLoaders;
550+
}
551+
$this->filesystemLoaders = [];
552+
553+
$loader = $this->twig->getLoader();
554+
if ($loader instanceof FilesystemLoader) {
555+
$this->filesystemLoaders[] = $loader;
556+
} elseif ($loader instanceof ChainLoader) {
557+
foreach ($loader->getLoaders() as $l) {
558+
if ($l instanceof FilesystemLoader) {
559+
$this->filesystemLoaders[] = $l;
560+
}
561+
}
562+
}
563+
564+
return $this->filesystemLoaders;
565+
}
538566
}

src/Symfony/Bridge/Twig/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"require": {
1919
"php": "^7.1.3",
2020
"symfony/contracts": "^1.0.2",
21-
"twig/twig": "^1.37.1|^2.6.2"
21+
"twig/twig": "dev-master"
2222
},
2323
"require-dev": {
2424
"symfony/asset": "~3.4|~4.0",

0 commit comments

Comments
 (0)
0