Skip to content

Commit

Permalink
working on using contao template loader
Browse files Browse the repository at this point in the history
  • Loading branch information
koertho committed Sep 24, 2024
1 parent ca5fa02 commit 43320a0
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 5 deletions.
2 changes: 2 additions & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions src/EventListener/RenderListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
122 changes: 117 additions & 5 deletions src/Filesystem/TwigTemplateLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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;
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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();
Expand All @@ -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);
Expand All @@ -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)) {
Expand Down Expand Up @@ -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;
}
}

0 comments on commit 43320a0

Please sign in to comment.