Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the Twig loader infrastructure #6936

Merged
merged 3 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion core-bundle/config/commands.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ services:
class: Contao\CoreBundle\Command\DebugContaoTwigCommand
arguments:
- '@contao.twig.filesystem_loader'
- '@contao.twig.filesystem_loader_warmer'
- '@contao.twig.loader.theme_namespace'
- '%kernel.project_dir%'
- '@contao.twig.inspector'
Expand Down
2 changes: 1 addition & 1 deletion core-bundle/config/listener.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ services:
contao.listener.data_container.theme_templates:
class: Contao\CoreBundle\EventListener\DataContainer\ThemeTemplatesListener
arguments:
- '@contao.twig.filesystem_loader_warmer'
- '@contao.twig.filesystem_loader'
- '@contao.twig.loader.theme_namespace'
- '@translator'

Expand Down
2 changes: 0 additions & 2 deletions core-bundle/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1165,8 +1165,6 @@ services:
class: Contao\CoreBundle\Twig\Loader\ContaoFilesystemLoaderWarmer
arguments:
- '@contao.twig.filesystem_loader'
- '@contao.twig.loader.template_locator'
- '%kernel.project_dir%'
- '%kernel.cache_dir%'
- '%kernel.environment%'

Expand Down
10 changes: 4 additions & 6 deletions core-bundle/src/Command/DebugContaoTwigCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@

namespace Contao\CoreBundle\Command;

use Contao\CoreBundle\Twig\Inheritance\TemplateHierarchyInterface;
use Contao\CoreBundle\Twig\Inspector\Inspector;
use Contao\CoreBundle\Twig\Loader\ContaoFilesystemLoaderWarmer;
use Contao\CoreBundle\Twig\Loader\ContaoFilesystemLoader;
use Contao\CoreBundle\Twig\Loader\ThemeNamespace;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
Expand All @@ -35,8 +34,7 @@
class DebugContaoTwigCommand extends Command
{
public function __construct(
private readonly TemplateHierarchyInterface $hierarchy,
private readonly ContaoFilesystemLoaderWarmer $cacheWarmer,
private readonly ContaoFilesystemLoader $filesystemLoader,
private readonly ThemeNamespace $themeNamespace,
private readonly string $projectDir,
private readonly Inspector $inspector,
Expand All @@ -56,9 +54,9 @@ protected function configure(): void
protected function execute(InputInterface $input, OutputInterface $output): int
{
// Make sure the template hierarchy is up-to-date
$this->cacheWarmer->refresh();
$this->filesystemLoader->warmUp(true);

$chains = $this->hierarchy->getInheritanceChains($this->getThemeSlug($input));
$chains = $this->filesystemLoader->getInheritanceChains($this->getThemeSlug($input));

if (null !== ($prefix = $input->getArgument('filter'))) {
$chains = array_filter(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use Contao\CoreBundle\DependencyInjection\Compiler\RegisterFragmentsPass;
use Contao\CoreBundle\Framework\ContaoFramework;
use Contao\CoreBundle\Twig\Finder\FinderFactory;
use Contao\CoreBundle\Twig\Inheritance\TemplateHierarchyInterface;
use Contao\CoreBundle\Twig\Loader\ContaoFilesystemLoader;
use Contao\DataContainer;
use Contao\ModuleProxy;
use Doctrine\DBAL\ArrayParameterType;
Expand All @@ -40,7 +40,7 @@ public function __construct(
private readonly Connection $connection,
private readonly ContaoFramework $framework,
private readonly RequestStack $requestStack,
private readonly TemplateHierarchyInterface $hierarchy,
private readonly ContaoFilesystemLoader $filesystemLoader,
) {
}

Expand Down Expand Up @@ -83,7 +83,7 @@ public function __invoke(DataContainer $dc): array
if (!$templateOptions) {
$guessedType = $legacyPrefix.$type;

if (isset($this->hierarchy->getInheritanceChains()[$guessedType])) {
if (isset($this->filesystemLoader->getInheritanceChains()[$guessedType])) {
$help = sprintf('In case you wanted to use the legacy type "%s", define it explicitly in the "template" property of your controller\'s service tag/attribute.', $guessedType);
} else {
$help = 'Did you forget to create the default template?';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@

use Contao\CoreBundle\DependencyInjection\Attribute\AsCallback;
use Contao\CoreBundle\Exception\InvalidThemePathException;
use Contao\CoreBundle\Twig\Loader\ContaoFilesystemLoaderWarmer;
use Contao\CoreBundle\Twig\Loader\ContaoFilesystemLoader;
use Contao\CoreBundle\Twig\Loader\ThemeNamespace;
use Symfony\Contracts\Translation\TranslatorInterface;

#[AsCallback(table: 'tl_theme', target: 'fields.templates.save')]
class ThemeTemplatesListener
{
public function __construct(
private readonly ContaoFilesystemLoaderWarmer $filesystemLoaderWarmer,
private readonly ContaoFilesystemLoader $filesystemLoader,
private readonly ThemeNamespace $themeNamespace,
private readonly TranslatorInterface $translator,
) {
Expand All @@ -37,7 +37,7 @@ public function __invoke(string $value): string
throw new \RuntimeException($this->translator->trans('ERR.invalidThemeTemplatePath', [$e->getPath(), implode('', $e->getInvalidCharacters())], 'contao_default'), 0, $e);
}

$this->filesystemLoaderWarmer->refresh();
$this->filesystemLoader->warmUp(true);

return $value;
}
Expand Down
12 changes: 6 additions & 6 deletions core-bundle/src/Twig/Extension/ContaoExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
use Contao\CoreBundle\Twig\Inheritance\DynamicExtendsTokenParser;
use Contao\CoreBundle\Twig\Inheritance\DynamicIncludeTokenParser;
use Contao\CoreBundle\Twig\Inheritance\DynamicUseTokenParser;
use Contao\CoreBundle\Twig\Inheritance\TemplateHierarchyInterface;
use Contao\CoreBundle\Twig\Interop\ContaoEscaper;
use Contao\CoreBundle\Twig\Interop\ContaoEscaperNodeVisitor;
use Contao\CoreBundle\Twig\Interop\PhpTemplateProxyNode;
use Contao\CoreBundle\Twig\Interop\PhpTemplateProxyNodeVisitor;
use Contao\CoreBundle\Twig\Loader\ContaoFilesystemLoader;
use Contao\CoreBundle\Twig\ResponseContext\AddTokenParser;
use Contao\CoreBundle\Twig\ResponseContext\DocumentLocation;
use Contao\CoreBundle\Twig\Runtime\ContentUrlRuntime;
Expand Down Expand Up @@ -63,7 +63,7 @@ final class ContaoExtension extends AbstractExtension implements GlobalsInterfac

public function __construct(
private readonly Environment $environment,
private readonly TemplateHierarchyInterface $hierarchy,
private readonly ContaoFilesystemLoader $filesystemLoader,
ContaoCsrfTokenManager $tokenManager,
private readonly ContaoVariable $contaoVariable,
) {
Expand Down Expand Up @@ -140,9 +140,9 @@ public function getTokenParsers(): array
return [
// Overwrite the parsers for the "extends", "include" and "use" tags to
// additionally support the Contao template hierarchy
new DynamicExtendsTokenParser($this->hierarchy),
new DynamicIncludeTokenParser($this->hierarchy),
new DynamicUseTokenParser($this->hierarchy),
new DynamicExtendsTokenParser($this->filesystemLoader),
new DynamicIncludeTokenParser($this->filesystemLoader),
new DynamicUseTokenParser($this->filesystemLoader),
// Add a parser for the Contao specific "add" tag
new AddTokenParser(self::class),
];
Expand All @@ -159,7 +159,7 @@ public function getFunctions(): array
'include',
function (Environment $env, $context, $template, $variables = [], $withContext = true, $ignoreMissing = false, $sandboxed = false /* we need named arguments here */) use ($includeFunctionCallable) {
$args = \func_get_args();
$args[2] = DynamicIncludeTokenParser::adjustTemplateName($template, $this->hierarchy);
$args[2] = DynamicIncludeTokenParser::adjustTemplateName($template, $this->filesystemLoader);

return $includeFunctionCallable(...$args);
},
Expand Down
6 changes: 3 additions & 3 deletions core-bundle/src/Twig/Finder/Finder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
namespace Contao\CoreBundle\Twig\Finder;

use Contao\CoreBundle\Twig\ContaoTwigUtil;
use Contao\CoreBundle\Twig\Inheritance\TemplateHierarchyInterface;
use Contao\CoreBundle\Twig\Loader\ContaoFilesystemLoader;
use Contao\CoreBundle\Twig\Loader\ThemeNamespace;
use Symfony\Component\Filesystem\Path;
use Symfony\Component\Translation\TranslatorBagInterface;
Expand Down Expand Up @@ -45,7 +45,7 @@ final class Finder implements \IteratorAggregate, \Countable
* @internal
*/
public function __construct(
private readonly TemplateHierarchyInterface $hierarchy,
private readonly ContaoFilesystemLoader $filesystem,
private readonly ThemeNamespace $themeNamespace,
private readonly TranslatorBagInterface|TranslatorInterface $translator,
) {
Expand Down Expand Up @@ -167,7 +167,7 @@ public function getIterator(): \Generator
{
// Only include chains that contain at least one non-legacy template
$chains = array_filter(
$this->hierarchy->getInheritanceChains($this->themeSlug),
$this->filesystem->getInheritanceChains($this->themeSlug),
static function (array $chain) {
foreach (array_keys($chain) as $path) {
if ('html5' !== Path::getExtension($path, true)) {
Expand Down
6 changes: 3 additions & 3 deletions core-bundle/src/Twig/Finder/FinderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

namespace Contao\CoreBundle\Twig\Finder;

use Contao\CoreBundle\Twig\Inheritance\TemplateHierarchyInterface;
use Contao\CoreBundle\Twig\Loader\ContaoFilesystemLoader;
use Contao\CoreBundle\Twig\Loader\ThemeNamespace;
use Symfony\Component\Translation\TranslatorBagInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
Expand All @@ -23,7 +23,7 @@ class FinderFactory
* @internal
*/
public function __construct(
private readonly TemplateHierarchyInterface $hierarchy,
private readonly ContaoFilesystemLoader $filesystemLoader,
private readonly ThemeNamespace $themeNamespace,
private readonly TranslatorBagInterface|TranslatorInterface $translator,
) {
Expand All @@ -34,6 +34,6 @@ public function __construct(
*/
public function create(): Finder
{
return new Finder($this->hierarchy, $this->themeNamespace, $this->translator);
return new Finder($this->filesystemLoader, $this->themeNamespace, $this->translator);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
namespace Contao\CoreBundle\Twig\Inheritance;

use Contao\CoreBundle\Twig\ContaoTwigUtil;
use Contao\CoreBundle\Twig\Loader\ContaoFilesystemLoader;
use Twig\Error\SyntaxError;
use Twig\Node\Expression\ArrayExpression;
use Twig\Node\Expression\ConstantExpression;
Expand All @@ -31,7 +32,7 @@
*/
final class DynamicExtendsTokenParser extends AbstractTokenParser
{
public function __construct(private readonly TemplateHierarchyInterface $hierarchy)
public function __construct(private readonly ContaoFilesystemLoader $filesystemLoader)
{
}

Expand Down Expand Up @@ -93,7 +94,7 @@ private function traverseAndAdjustTemplateNames(string $sourcePath, Node $node):
return;
}

$parentName = $this->hierarchy->getDynamicParent($parts[1] ?? '', $sourcePath);
$parentName = $this->filesystemLoader->getDynamicParent($parts[1] ?? '', $sourcePath);

// Adjust parent template according to the template hierarchy
$node->setAttribute('value', $parentName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
namespace Contao\CoreBundle\Twig\Inheritance;

use Contao\CoreBundle\Twig\ContaoTwigUtil;
use Contao\CoreBundle\Twig\Loader\ContaoFilesystemLoader;
use Twig\Node\Expression\ArrayExpression;
use Twig\Node\Expression\ConstantExpression;
use Twig\Node\IncludeNode;
Expand All @@ -32,7 +33,7 @@
*/
final class DynamicIncludeTokenParser extends AbstractTokenParser
{
public function __construct(private readonly TemplateHierarchyInterface $hierarchy)
public function __construct(private readonly ContaoFilesystemLoader $filesystemLoader)
{
}

Expand All @@ -56,7 +57,7 @@ public function getTag(): string
* Return the adjusted logical name or the unchanged input if it does not
* match the Contao Twig namespace.
*/
public static function adjustTemplateName(TemplateWrapper|string $name, TemplateHierarchyInterface $hierarchy): TemplateWrapper|string
public static function adjustTemplateName(TemplateWrapper|string $name, ContaoFilesystemLoader $filesystemLoader): TemplateWrapper|string
{
if ($name instanceof TemplateWrapper) {
return $name;
Expand All @@ -69,7 +70,7 @@ public static function adjustTemplateName(TemplateWrapper|string $name, Template
}

try {
return $hierarchy->getFirst($parts[1] ?? '');
return $filesystemLoader->getFirst($parts[1] ?? '');
} catch (\LogicException $e) {
throw new \LogicException($e->getMessage().' Did you try to include a non-existent template or a template from a theme directory?', 0, $e);
}
Expand Down Expand Up @@ -123,7 +124,7 @@ private function traverseAndAdjustTemplateNames(Node $node): void
}

$name = (string) $node->getAttribute('value');
$adjustedName = self::adjustTemplateName($name, $this->hierarchy);
$adjustedName = self::adjustTemplateName($name, $this->filesystemLoader);

if ($name !== $adjustedName) {
$node->setAttribute('value', $adjustedName);
Expand Down
5 changes: 3 additions & 2 deletions core-bundle/src/Twig/Inheritance/DynamicUseTokenParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
namespace Contao\CoreBundle\Twig\Inheritance;

use Contao\CoreBundle\Twig\ContaoTwigUtil;
use Contao\CoreBundle\Twig\Loader\ContaoFilesystemLoader;
use Twig\Error\SyntaxError;
use Twig\Node\Expression\ConstantExpression;
use Twig\Node\Node;
Expand All @@ -30,7 +31,7 @@
*/
final class DynamicUseTokenParser extends AbstractTokenParser
{
public function __construct(private readonly TemplateHierarchyInterface $hierarchy)
public function __construct(private readonly ContaoFilesystemLoader $filesystemLoader)
{
}

Expand Down Expand Up @@ -84,7 +85,7 @@ private function adjustTemplateName(string $sourcePath, ConstantExpression $node
return;
}

$nextOrFirst = $this->hierarchy->getDynamicParent($parts[1] ?? '', $sourcePath);
$nextOrFirst = $this->filesystemLoader->getDynamicParent($parts[1] ?? '', $sourcePath);

// Adjust parent template according to the template hierarchy
$node->setAttribute('value', $nextOrFirst);
Expand Down
50 changes: 0 additions & 50 deletions core-bundle/src/Twig/Inheritance/TemplateHierarchyInterface.php

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

/*
* This file is part of Contao.
*
* (c) Leo Feyer
*
* @license LGPL-3.0-or-later
*/

namespace Contao\CoreBundle\Twig\Loader;

use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
use Symfony\Component\HttpKernel\Event\RequestEvent;

/**
* @internal
*/
#[AsEventListener]
class AutoRefreshTemplateHierarchyListener
{
public function __construct(
private readonly ContaoFilesystemLoader $loader,
private readonly string $environment,
) {
}

/**
* Auto refresh template hierarchy in dev mode, so that added/removed files
* are immediately recognized.
*/
public function __invoke(RequestEvent $event): void
{
if ('dev' === $this->environment && $event->isMainRequest()) {
$this->loader->warmUp(true);
}
}
}