9
9
namespace HeimrichHannot \TwigSupportBundle \Filesystem ;
10
10
11
11
use Contao \CoreBundle \Config \ResourceFinderInterface ;
12
- use Contao \CoreBundle \ContaoCoreBundle ;
13
12
use Contao \CoreBundle \Framework \ContaoFramework ;
14
13
use Contao \CoreBundle \Routing \ScopeMatcher ;
15
- use Contao \CoreBundle \Twig \ContaoTwigUtil ;
16
14
use Contao \CoreBundle \Twig \Finder \FinderFactory ;
17
15
use Contao \CoreBundle \Twig \Loader \TemplateLocator ;
18
16
use Contao \PageModel ;
19
17
use Contao \ThemeModel ;
20
18
use Contao \Validator ;
21
19
use HeimrichHannot \TwigSupportBundle \Cache \TemplateCache ;
22
20
use HeimrichHannot \TwigSupportBundle \Exception \TemplateNotFoundException ;
23
- use http \Encoding \Stream \Inflate ;
24
21
use Symfony \Component \Cache \Adapter \FilesystemAdapter ;
25
22
use Symfony \Component \Finder \Exception \DirectoryNotFoundException ;
26
23
use Symfony \Component \Finder \Finder ;
27
24
use Symfony \Component \Finder \SplFileInfo ;
28
25
use Symfony \Component \HttpFoundation \RequestStack ;
29
- use Symfony \Component \HttpKernel \Bundle \Bundle ;
30
26
use Symfony \Component \HttpKernel \Bundle \BundleInterface ;
31
27
use Symfony \Component \HttpKernel \KernelInterface ;
32
28
use Symfony \Component \Stopwatch \Stopwatch ;
@@ -367,6 +363,8 @@ public function getTwigTemplatesInPath($dir, ?string $twigKey = null, bool $exte
367
363
* - extension: (bool) Add extension to filename (array key)
368
364
*
369
365
* @param iterable|string $dir
366
+ *
367
+ * @deprecated Use Contao\CoreBundle\Twig\Loader\TemplateLocator::findTemplates()
370
368
*/
371
369
public function getTemplatesInPath ($ dir , ?BundleInterface $ bundle = null , array $ options = []): array
372
370
{
@@ -396,8 +394,6 @@ public function getTemplatesInPath($dir, ?BundleInterface $bundle = null, array
396
394
397
395
$ twigFiles = [];
398
396
399
- $ finder = $ this ->finderFactory ->create ();
400
-
401
397
foreach ($ files as $ file ) {
402
398
/** @var SplFileInfo $file */
403
399
$ name = $ file ->getBasename ();
@@ -414,28 +410,14 @@ public function getTemplatesInPath($dir, ?BundleInterface $bundle = null, array
414
410
}
415
411
elseif ('Contao ' === $ twigKey ) {
416
412
$ path = "@ $ twigKey/ " .$ file ->getBasename ();
417
-
418
-
419
-
420
- if (!$ this ->twig ->getLoader ()->exists ($ path )) {
421
- foreach ($ finder ->name ($ file ->getRelativePathname ())->getIterator () as $ identifier => $ extension ) {
422
-
423
- }
424
- }
425
-
426
-
427
- foreach ($ finder ->name ($ file ->getRelativePathname ())->getIterator () as $ identifier => $ extension ) {
428
- continue ;
429
- }
430
-
431
-
432
- $ path = "@ $ twigKey/ " .$ file ->getBasename ();
433
413
$ twigFiles [$ name ]['paths ' ][] = $ path ;
434
414
$ twigFiles [$ name ]['pathInfo ' ][$ path ]['bundle ' ] = null ;
435
415
$ twigFiles [$ name ]['pathInfo ' ][$ path ]['pathname ' ] = $ file ->getPathname ();
436
416
} else {
437
417
$ path = "@ $ twigKey/ " .$ file ->getRelativePathname ();
438
- $ this ->addPath ($ twigFiles , $ name , $ path , $ bundle ->getName (), $ file ->getPathname ());
418
+ $ twigFiles [$ name ]['paths ' ][] = $ path ;
419
+ $ twigFiles [$ name ]['pathInfo ' ][$ path ]['bundle ' ] = $ bundle ->getName ();
420
+ $ twigFiles [$ name ]['pathInfo ' ][$ path ]['pathname ' ] = $ file ->getPathname ();
439
421
}
440
422
}
441
423
$ this ->stopwatch ->stop ($ stopwatchname );
@@ -448,6 +430,10 @@ public function getTemplatesInPath($dir, ?BundleInterface $bundle = null, array
448
430
*/
449
431
protected function generateContaoTwigTemplatePaths (bool $ extension = false ): array
450
432
{
433
+ $ stopwatchname = 'TwigTemplateLocator::generateContaoTwigTemplatePaths() ' ;
434
+ $ this ->stopwatch ->start ($ stopwatchname );
435
+
436
+
451
437
$ contaoResourcePaths = $ this ->templateLocator ->findResourcesPaths ();
452
438
$ bundles = $ this ->kernel ->getBundles ();
453
439
@@ -492,7 +478,7 @@ protected function generateContaoTwigTemplatePaths(bool $extension = false): arr
492
478
if (str_contains ($ path , '/contao/templates ' )) {
493
479
$ namespace = 'Contao_ ' .$ bundle ;
494
480
} else {
495
- $ namespace = $ bundle ;
481
+ $ namespace = preg_replace ( ' /Bundle$/ ' , '' , $ bundle) ;
496
482
}
497
483
}
498
484
@@ -503,65 +489,39 @@ protected function generateContaoTwigTemplatePaths(bool $extension = false): arr
503
489
504
490
$ twigPath = ($ namespace ? "@ $ namespace/ " : '' ).$ name ;
505
491
506
-
507
- // Path::makeRelative($file->getPathname(), $this->kernel->getProjectDir().'/templates');
492
+ if (!$ extension ) {
493
+ if (str_ends_with ($ name , '.html.twig ' )) {
494
+ $ name = substr ($ name , 0 , -10 );
495
+ }
496
+ }
508
497
509
498
$ this ->addPath ($ twigFiles , $ name , $ twigPath , $ bundle , $ path );
510
- }
511
- }
512
- }
513
-
514
- return $ twigFiles ;
515
499
516
- $ twigFiles = [];
517
-
518
- if (\is_array ($ bundles )) {
519
- foreach ($ bundles as $ key => $ bundle ) {
520
- $ path = $ bundle ->getPath ();
521
-
522
- foreach (['/templates ' , '/Resources/views ' ,] as $ subpath ) {
523
- if (!is_dir ($ dir = rtrim ($ path , '/ ' ).$ subpath )) {
500
+ // check for modern contao template paths and legacy fallback
501
+ if (!str_contains ($ name , '/ ' )) {
524
502
continue ;
525
503
}
526
504
527
- $ twigFiles = array_merge_recursive ($ twigFiles , $ this ->getTemplatesInPath ($ dir , $ bundle , ['extension ' => $ extension ]));
528
- }
529
- }
530
- }
505
+ $ file = new \SplFileInfo ($ templatePath );
506
+ $ name = $ file ->getBasename ();
507
+ if (str_ends_with ($ name , '.html.twig ' )) {
508
+ $ name = substr ($ name , 0 , -10 );
509
+ }
531
510
532
- $ bundle = null ;
533
- if (version_compare (\VERSION , '4.12 ' , '>= ' )) {
534
- $ bundle = new class extends Bundle {
535
- public function __construct ()
536
- {
537
- $ this ->name = 'Contao ' ;
511
+ $ this ->addPath ($ twigFiles , $ name , $ twigPath , $ bundle , $ path , true );
538
512
}
539
- };
513
+ }
540
514
}
541
515
542
- // Bundle template folders
543
- $ twigFiles = array_merge_recursive ($ twigFiles , $ this ->getTemplatesInPath (
544
- $ this ->contaoResourceFinder ->findIn ('templates ' )->name ('*.twig ' )->getIterator (),
545
- $ bundle ,
546
- ['extension ' => $ extension ]));
547
-
548
- // Project template folders
549
- $ twigFiles = array_merge_recursive (
550
- $ twigFiles ,
551
- $ this ->getTemplatesInPath ($ this ->kernel ->getProjectDir ().'/contao/templates ' , $ bundle , ['extension ' => $ extension ])
552
- );
553
- $ twigFiles = array_merge_recursive (
554
- $ twigFiles ,
555
- $ this ->getTemplatesInPath ($ this ->kernel ->getProjectDir ().'/templates ' , null , ['extension ' => $ extension ])
556
- );
557
-
516
+ $ this ->stopwatch ->stop ($ stopwatchname );
558
517
return $ twigFiles ;
559
518
}
560
519
561
- private function addPath (array &$ pathData , string $ name , string $ twigPath , ?string $ bundleName , string $ absolutePath ): void
520
+ private function addPath (array &$ pathData , string $ name , string $ twigPath , ?string $ bundleName , string $ absolutePath, bool $ deprecated = false ): void
562
521
{
563
522
$ pathData [$ name ]['paths ' ][] = $ twigPath ;
564
523
$ pathData [$ name ]['pathInfo ' ][$ twigPath ]['bundle ' ] = $ bundleName ;
565
524
$ pathData [$ name ]['pathInfo ' ][$ twigPath ]['pathname ' ] = $ absolutePath ;
525
+ $ pathData [$ name ]['pathInfo ' ][$ twigPath ]['deprecatedPath ' ] = $ deprecated ;
566
526
}
567
527
}
0 commit comments