From 43320a04853e6cc3e727fdbc1106cc63d8aa504f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20K=C3=B6rner?= Date: Tue, 24 Sep 2024 13:45:44 +0200 Subject: [PATCH] working on using contao template loader --- config/services.yaml | 2 + src/EventListener/RenderListener.php | 5 + src/Filesystem/TwigTemplateLocator.php | 122 ++++++++++++++++++++++++- 3 files changed, 124 insertions(+), 5 deletions(-) diff --git a/config/services.yaml b/config/services.yaml index 703bdcf..aa26d4c 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -14,6 +14,8 @@ services: HeimrichHannot\TwigSupportBundle\Cache\TemplateCache: ~ HeimrichHannot\TwigSupportBundle\Filesystem\TwigTemplateLocator: public: true + bind: + $finderFactory: '@contao.twig.finder_factory' HeimrichHannot\TwigSupportBundle\Helper\NormalizerHelper: ~ HeimrichHannot\TwigSupportBundle\Renderer\TwigTemplateRenderer: public: true diff --git a/src/EventListener/RenderListener.php b/src/EventListener/RenderListener.php index e9b3ba8..bcedb43 100644 --- a/src/EventListener/RenderListener.php +++ b/src/EventListener/RenderListener.php @@ -192,6 +192,11 @@ public function render($contaoTemplate): string $twigTemplatePath = $this->templateLocator->getTemplatePath($twigTemplateName); + if ('@Contao/news_stage.html.twig' === $twigTemplatePath) { + $twigTemplatePath = '@Contao/news/news_stage.html.twig'; + } + + if ($contaoTemplate instanceof Widget) { $twigTemplateData['widget'] = $contaoTemplate; } diff --git a/src/Filesystem/TwigTemplateLocator.php b/src/Filesystem/TwigTemplateLocator.php index 112aedc..366e803 100644 --- a/src/Filesystem/TwigTemplateLocator.php +++ b/src/Filesystem/TwigTemplateLocator.php @@ -12,11 +12,15 @@ use Contao\CoreBundle\ContaoCoreBundle; use Contao\CoreBundle\Framework\ContaoFramework; use Contao\CoreBundle\Routing\ScopeMatcher; +use Contao\CoreBundle\Twig\ContaoTwigUtil; +use Contao\CoreBundle\Twig\Finder\FinderFactory; +use Contao\CoreBundle\Twig\Loader\TemplateLocator; use Contao\PageModel; use Contao\ThemeModel; use Contao\Validator; use HeimrichHannot\TwigSupportBundle\Cache\TemplateCache; use HeimrichHannot\TwigSupportBundle\Exception\TemplateNotFoundException; +use http\Encoding\Stream\Inflate; use Symfony\Component\Cache\Adapter\FilesystemAdapter; use Symfony\Component\Finder\Exception\DirectoryNotFoundException; use Symfony\Component\Finder\Finder; @@ -26,6 +30,7 @@ use Symfony\Component\HttpKernel\Bundle\BundleInterface; use Symfony\Component\HttpKernel\KernelInterface; use Symfony\Component\Stopwatch\Stopwatch; +use Twig\Environment; use Webmozart\PathUtil\Path; class TwigTemplateLocator @@ -39,8 +44,25 @@ class TwigTemplateLocator protected Stopwatch $stopwatch; protected FilesystemAdapter $templateCache; private ContaoFramework $contaoFramework; - - public function __construct(KernelInterface $kernel, ResourceFinderInterface $contaoResourceFinder, RequestStack $requestStack, ScopeMatcher $scopeMatcher, Stopwatch $stopwatch, FilesystemAdapter $templateCache, ContaoFramework $contaoFramework) + private Environment $twig; + private FinderFactory $finderFactory; + private TemplateLocator $templateLocator; + + /** @var array|string[] */ + private array $resourcePaths; + + public function __construct( + KernelInterface $kernel, + ResourceFinderInterface $contaoResourceFinder, + RequestStack $requestStack, + ScopeMatcher $scopeMatcher, + Stopwatch $stopwatch, + FilesystemAdapter $templateCache, + ContaoFramework $contaoFramework, + Environment $twig, + FinderFactory $finderFactory, + TemplateLocator $templateLocator + ) { $this->kernel = $kernel; $this->contaoResourceFinder = $contaoResourceFinder; @@ -49,6 +71,9 @@ public function __construct(KernelInterface $kernel, ResourceFinderInterface $co $this->stopwatch = $stopwatch; $this->templateCache = $templateCache; $this->contaoFramework = $contaoFramework; + $this->twig = $twig; + $this->finderFactory = $finderFactory; + $this->templateLocator = $templateLocator; } /** @@ -371,6 +396,8 @@ public function getTemplatesInPath($dir, ?BundleInterface $bundle = null, array $twigFiles = []; + $finder = $this->finderFactory->create(); + foreach ($files as $file) { /** @var SplFileInfo $file */ $name = $file->getBasename(); @@ -387,14 +414,28 @@ public function getTemplatesInPath($dir, ?BundleInterface $bundle = null, array } elseif ('Contao' === $twigKey) { $path = "@$twigKey/".$file->getBasename(); + + + + if (!$this->twig->getLoader()->exists($path)) { + foreach ($finder->name($file->getRelativePathname())->getIterator() as $identifier => $extension) { + + } + } + + + foreach ($finder->name($file->getRelativePathname())->getIterator() as $identifier => $extension) { + continue; + } + + + $path = "@$twigKey/".$file->getBasename(); $twigFiles[$name]['paths'][] = $path; $twigFiles[$name]['pathInfo'][$path]['bundle'] = null; $twigFiles[$name]['pathInfo'][$path]['pathname'] = $file->getPathname(); } else { $path = "@$twigKey/".$file->getRelativePathname(); - $twigFiles[$name]['paths'][] = $path; - $twigFiles[$name]['pathInfo'][$path]['bundle'] = $bundle->getName(); - $twigFiles[$name]['pathInfo'][$path]['pathname'] = $file->getPathname(); + $this->addPath($twigFiles, $name, $path, $bundle->getName(), $file->getPathname()); } } $this->stopwatch->stop($stopwatchname); @@ -407,7 +448,71 @@ public function getTemplatesInPath($dir, ?BundleInterface $bundle = null, array */ protected function generateContaoTwigTemplatePaths(bool $extension = false): array { + $contaoResourcePaths = $this->templateLocator->findResourcesPaths(); $bundles = $this->kernel->getBundles(); + + $resourcePaths = []; + if (\is_array($bundles)) { + foreach ($bundles as $key => $bundle) { + $path = $bundle->getPath(); + + foreach (['/templates', '/Resources/views',] as $subpath) { + if (!is_dir($dir = rtrim($path, '/').$subpath)) { + continue; + } + + $resourcePaths[$key][] = $dir; + } + if (isset($contaoResourcePaths[$key])) { + $resourcePaths[$key] = array_merge(($resourcePaths[$key] ?? []), $contaoResourcePaths[$key]); + } + } + } + + if (isset($contaoResourcePaths['App'])) { + $resourcePaths['App'] = $contaoResourcePaths['App']; + } + + $twigFiles = []; + $projectDir = $this->kernel->getProjectDir(); + foreach ($resourcePaths as $bundle => $paths) { + foreach ($paths as $path) { + $templates = $this->templateLocator->findTemplates($path); + if (empty($templates)) { + continue; + } + + if ('App' === $bundle) { + if (str_contains($path, '/contao/templates')) { + $namespace = 'Contao_App'; + } else { + $namespace = ''; + } + } else { + if (str_contains($path, '/contao/templates')) { + $namespace = 'Contao_'.$bundle; + } else { + $namespace = $bundle; + } + } + + foreach ($templates as $name => $templatePath) { + if (str_ends_with($name, '.html5')) { + continue; + } + + $twigPath = ($namespace ? "@$namespace/" : '').$name; + + +// Path::makeRelative($file->getPathname(), $this->kernel->getProjectDir().'/templates'); + + $this->addPath($twigFiles, $name, $twigPath, $bundle, $path); + } + } + } + + return $twigFiles; + $twigFiles = []; if (\is_array($bundles)) { @@ -452,4 +557,11 @@ public function __construct() return $twigFiles; } + + private function addPath(array &$pathData, string $name, string $twigPath, ?string $bundleName, string $absolutePath): void + { + $pathData[$name]['paths'][] = $twigPath; + $pathData[$name]['pathInfo'][$twigPath]['bundle'] = $bundleName; + $pathData[$name]['pathInfo'][$twigPath]['pathname'] = $absolutePath; + } }