Skip to content

Commit f02b5d4

Browse files
committed
support legacy naming
1 parent 43320a0 commit f02b5d4

File tree

1 file changed

+27
-67
lines changed

1 file changed

+27
-67
lines changed

src/Filesystem/TwigTemplateLocator.php

+27-67
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,20 @@
99
namespace HeimrichHannot\TwigSupportBundle\Filesystem;
1010

1111
use Contao\CoreBundle\Config\ResourceFinderInterface;
12-
use Contao\CoreBundle\ContaoCoreBundle;
1312
use Contao\CoreBundle\Framework\ContaoFramework;
1413
use Contao\CoreBundle\Routing\ScopeMatcher;
15-
use Contao\CoreBundle\Twig\ContaoTwigUtil;
1614
use Contao\CoreBundle\Twig\Finder\FinderFactory;
1715
use Contao\CoreBundle\Twig\Loader\TemplateLocator;
1816
use Contao\PageModel;
1917
use Contao\ThemeModel;
2018
use Contao\Validator;
2119
use HeimrichHannot\TwigSupportBundle\Cache\TemplateCache;
2220
use HeimrichHannot\TwigSupportBundle\Exception\TemplateNotFoundException;
23-
use http\Encoding\Stream\Inflate;
2421
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
2522
use Symfony\Component\Finder\Exception\DirectoryNotFoundException;
2623
use Symfony\Component\Finder\Finder;
2724
use Symfony\Component\Finder\SplFileInfo;
2825
use Symfony\Component\HttpFoundation\RequestStack;
29-
use Symfony\Component\HttpKernel\Bundle\Bundle;
3026
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
3127
use Symfony\Component\HttpKernel\KernelInterface;
3228
use Symfony\Component\Stopwatch\Stopwatch;
@@ -367,6 +363,8 @@ public function getTwigTemplatesInPath($dir, ?string $twigKey = null, bool $exte
367363
* - extension: (bool) Add extension to filename (array key)
368364
*
369365
* @param iterable|string $dir
366+
*
367+
* @deprecated Use Contao\CoreBundle\Twig\Loader\TemplateLocator::findTemplates()
370368
*/
371369
public function getTemplatesInPath($dir, ?BundleInterface $bundle = null, array $options = []): array
372370
{
@@ -396,8 +394,6 @@ public function getTemplatesInPath($dir, ?BundleInterface $bundle = null, array
396394

397395
$twigFiles = [];
398396

399-
$finder = $this->finderFactory->create();
400-
401397
foreach ($files as $file) {
402398
/** @var SplFileInfo $file */
403399
$name = $file->getBasename();
@@ -414,28 +410,14 @@ public function getTemplatesInPath($dir, ?BundleInterface $bundle = null, array
414410
}
415411
elseif ('Contao' === $twigKey) {
416412
$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();
433413
$twigFiles[$name]['paths'][] = $path;
434414
$twigFiles[$name]['pathInfo'][$path]['bundle'] = null;
435415
$twigFiles[$name]['pathInfo'][$path]['pathname'] = $file->getPathname();
436416
} else {
437417
$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();
439421
}
440422
}
441423
$this->stopwatch->stop($stopwatchname);
@@ -448,6 +430,10 @@ public function getTemplatesInPath($dir, ?BundleInterface $bundle = null, array
448430
*/
449431
protected function generateContaoTwigTemplatePaths(bool $extension = false): array
450432
{
433+
$stopwatchname = 'TwigTemplateLocator::generateContaoTwigTemplatePaths()';
434+
$this->stopwatch->start($stopwatchname);
435+
436+
451437
$contaoResourcePaths = $this->templateLocator->findResourcesPaths();
452438
$bundles = $this->kernel->getBundles();
453439

@@ -492,7 +478,7 @@ protected function generateContaoTwigTemplatePaths(bool $extension = false): arr
492478
if (str_contains($path, '/contao/templates')) {
493479
$namespace = 'Contao_'.$bundle;
494480
} else {
495-
$namespace = $bundle;
481+
$namespace = preg_replace('/Bundle$/', '', $bundle);
496482
}
497483
}
498484

@@ -503,65 +489,39 @@ protected function generateContaoTwigTemplatePaths(bool $extension = false): arr
503489

504490
$twigPath = ($namespace ? "@$namespace/" : '').$name;
505491

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+
}
508497

509498
$this->addPath($twigFiles, $name, $twigPath, $bundle, $path);
510-
}
511-
}
512-
}
513-
514-
return $twigFiles;
515499

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, '/')) {
524502
continue;
525503
}
526504

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+
}
531510

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);
538512
}
539-
};
513+
}
540514
}
541515

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);
558517
return $twigFiles;
559518
}
560519

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
562521
{
563522
$pathData[$name]['paths'][] = $twigPath;
564523
$pathData[$name]['pathInfo'][$twigPath]['bundle'] = $bundleName;
565524
$pathData[$name]['pathInfo'][$twigPath]['pathname'] = $absolutePath;
525+
$pathData[$name]['pathInfo'][$twigPath]['deprecatedPath'] = $deprecated;
566526
}
567527
}

0 commit comments

Comments
 (0)