20
20
use Symfony \Component \Console \Style \SymfonyStyle ;
21
21
use Symfony \Component \Finder \Finder ;
22
22
use Twig \Environment ;
23
+ use Twig \Loader \ChainLoader ;
23
24
use Twig \Loader \FilesystemLoader ;
24
25
25
26
/**
@@ -36,6 +37,7 @@ class DebugCommand extends Command
36
37
private $ bundlesMetadata ;
37
38
private $ twigDefaultPath ;
38
39
private $ rootDir ;
40
+ private $ filesystemLoaders ;
39
41
40
42
public function __construct (Environment $ twig , string $ projectDir = null , array $ bundlesMetadata = [], string $ twigDefaultPath = null , string $ rootDir = null )
41
43
{
@@ -87,7 +89,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
87
89
$ name = $ input ->getArgument ('name ' );
88
90
$ filter = $ input ->getOption ('filter ' );
89
91
90
- if (null !== $ name && ! $ this ->twig -> getLoader () instanceof FilesystemLoader ) {
92
+ if (null !== $ name && [] === $ this ->getFilesystemLoaders () ) {
91
93
throw new InvalidArgumentException (sprintf ('Argument "name" not supported, it requires the Twig loader "%s" ' , FilesystemLoader::class));
92
94
}
93
95
@@ -150,9 +152,11 @@ private function displayPathsText(SymfonyStyle $io, string $name)
150
152
$ message = 'No template paths configured for your application ' ;
151
153
} else {
152
154
$ 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
+ }
156
160
}
157
161
}
158
162
@@ -243,25 +247,25 @@ private function displayGeneralJson(SymfonyStyle $io, $filter)
243
247
244
248
private function getLoaderPaths (string $ name = null ): array
245
249
{
246
- /** @var FilesystemLoader $loader */
247
- $ loader = $ this ->twig ->getLoader ();
248
250
$ 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
+ }
254
257
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 ));
257
260
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
+ }
263
266
264
- $ loaderPaths [$ namespace ] = $ paths ;
267
+ $ loaderPaths [$ namespace ] = $ paths ;
268
+ }
265
269
}
266
270
267
271
return $ loaderPaths ;
@@ -437,22 +441,22 @@ private function error(SymfonyStyle $io, string $message, array $alternatives =
437
441
438
442
private function findTemplateFiles (string $ name ): array
439
443
{
440
- /** @var FilesystemLoader $loader */
441
- $ loader = $ this ->twig ->getLoader ();
442
- $ files = [];
443
444
list ($ namespace , $ shortname ) = $ this ->parseTemplateName ($ name );
444
445
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 ;
450
453
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
+ }
456
460
}
457
461
}
458
462
}
@@ -535,4 +539,28 @@ private function isAbsolutePath(string $file): bool
535
539
{
536
540
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 );
537
541
}
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
+ }
538
566
}
0 commit comments