Skip to content

Commit

Permalink
Fix the Twig loader infrastructure (see #6949)
Browse files Browse the repository at this point in the history
Description
-----------

Backport for #6936 

This one needs manual testing as well. 

/cc @bytehead

Commits
-------

d0ec8b4 backport #6936
feda1a1 fix php syntax for 7.4
2c7e526 fix service arguments
9c74e47 re-add type annotation
  • Loading branch information
m-vo committed Mar 5, 2024
1 parent 8d30b96 commit fa9ec09
Show file tree
Hide file tree
Showing 36 changed files with 810 additions and 909 deletions.
15 changes: 6 additions & 9 deletions core-bundle/src/Command/DebugContaoTwigCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@

namespace Contao\CoreBundle\Command;

use Contao\CoreBundle\Twig\Inheritance\TemplateHierarchyInterface;
use Contao\CoreBundle\Twig\Loader\ContaoFilesystemLoaderWarmer;
use Contao\CoreBundle\Twig\Loader\ContaoFilesystemLoader;
use Contao\CoreBundle\Twig\Loader\ThemeNamespace;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\TableSeparator;
Expand All @@ -32,15 +31,13 @@ class DebugContaoTwigCommand extends Command
protected static $defaultName = 'debug:contao-twig';
protected static $defaultDescription = 'Displays the Contao template hierarchy.';

private TemplateHierarchyInterface $hierarchy;
private ContaoFilesystemLoaderWarmer $cacheWarmer;
private ContaoFilesystemLoader $filesystemLoader;
private ThemeNamespace $themeNamespace;
private string $projectDir;

public function __construct(TemplateHierarchyInterface $hierarchy, ContaoFilesystemLoaderWarmer $cacheWarmer, ThemeNamespace $themeNamespace, string $projectDir)
public function __construct(ContaoFilesystemLoader $filesystemLoader, ThemeNamespace $themeNamespace, string $projectDir)
{
$this->hierarchy = $hierarchy;
$this->cacheWarmer = $cacheWarmer;
$this->filesystemLoader = $filesystemLoader;
$this->themeNamespace = $themeNamespace;
$this->projectDir = $projectDir;

Expand All @@ -58,10 +55,10 @@ 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);

$rows = [];
$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 @@ -14,7 +14,7 @@

use Contao\CoreBundle\Exception\InvalidThemePathException;
use Contao\CoreBundle\ServiceAnnotation\Callback;
use Contao\CoreBundle\Twig\Loader\ContaoFilesystemLoaderWarmer;
use Contao\CoreBundle\Twig\Loader\ContaoFilesystemLoader;
use Contao\CoreBundle\Twig\Loader\ThemeNamespace;
use Symfony\Contracts\Translation\TranslatorInterface;

Expand All @@ -23,13 +23,13 @@
*/
class ThemeTemplatesListener
{
private ContaoFilesystemLoaderWarmer $filesystemLoaderWarmer;
private ContaoFilesystemLoader $filesystemLoader;
private ThemeNamespace $themeNamespace;
private TranslatorInterface $translator;

public function __construct(ContaoFilesystemLoaderWarmer $filesystemLoaderWarmer, ThemeNamespace $themeNamespace, TranslatorInterface $translator)
public function __construct(ContaoFilesystemLoader $filesystemLoader, ThemeNamespace $themeNamespace, TranslatorInterface $translator)
{
$this->filesystemLoaderWarmer = $filesystemLoaderWarmer;
$this->filesystemLoader = $filesystemLoader;
$this->themeNamespace = $themeNamespace;
$this->translator = $translator;
}
Expand All @@ -43,7 +43,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
1 change: 0 additions & 1 deletion core-bundle/src/Resources/config/commands.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,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%'

Expand Down
10 changes: 9 additions & 1 deletion core-bundle/src/Resources/config/listener.yml
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,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 Expand Up @@ -557,6 +557,14 @@ services:
- '@translator'
- '@contao.csrf.token_manager'

contao.twig.loader.auto_refresh_template_hierarchy_listener:
class: Contao\CoreBundle\Twig\Loader\AutoRefreshTemplateHierarchyListener
arguments:
- '@contao.twig.filesystem_loader'
- '%kernel.environment%'
tags:
- { name: kernel.event_listener, event: kernel.request }

# Backwards compatibility
Contao\CoreBundle\EventListener\DataContainer\ContentCompositionListener:
alias: contao.listener.data_container.content_composition
Expand Down
3 changes: 0 additions & 3 deletions core-bundle/src/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -975,13 +975,10 @@ 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%'
tags:
- { name: kernel.cache_warmer }
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }

contao.twig.finder_factory:
class: Contao\CoreBundle\Twig\Finder\FinderFactory
Expand Down
14 changes: 7 additions & 7 deletions core-bundle/src/Twig/Extension/ContaoExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
use Contao\CoreBundle\InsertTag\ChunkedText;
use Contao\CoreBundle\Twig\Inheritance\DynamicExtendsTokenParser;
use Contao\CoreBundle\Twig\Inheritance\DynamicIncludeTokenParser;
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\Runtime\FigureRendererRuntime;
use Contao\CoreBundle\Twig\Runtime\InsertTagRuntime;
use Contao\CoreBundle\Twig\Runtime\LegacyTemplateFunctionsRuntime;
Expand All @@ -44,13 +44,13 @@
final class ContaoExtension extends AbstractExtension
{
private Environment $environment;
private TemplateHierarchyInterface $hierarchy;
private ContaoFilesystemLoader $filesystemLoader;
private array $contaoEscaperFilterRules = [];

public function __construct(Environment $environment, TemplateHierarchyInterface $hierarchy)
public function __construct(Environment $environment, ContaoFilesystemLoader $filesystemLoader)
{
$this->environment = $environment;
$this->hierarchy = $hierarchy;
$this->filesystemLoader = $filesystemLoader;

$contaoEscaper = new ContaoEscaper();

Expand Down Expand Up @@ -104,8 +104,8 @@ public function getTokenParsers(): array
return [
// Overwrite the parsers for the "extends" and "include" tags to
// additionally support the Contao template hierarchy
new DynamicExtendsTokenParser($this->hierarchy),
new DynamicIncludeTokenParser($this->hierarchy),
new DynamicExtendsTokenParser($this->filesystemLoader),
new DynamicIncludeTokenParser($this->filesystemLoader),
];
}

Expand All @@ -120,7 +120,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
10 changes: 5 additions & 5 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\Contracts\Translation\TranslatorInterface;
Expand All @@ -24,7 +24,7 @@
*/
final class Finder implements \IteratorAggregate, \Countable
{
private TemplateHierarchyInterface $hierarchy;
private ContaoFilesystemLoader $filesystemLoader;
private ThemeNamespace $themeNamespace;
private TranslatorInterface $translator;

Expand All @@ -42,9 +42,9 @@ final class Finder implements \IteratorAggregate, \Countable
/**
* @internal
*/
public function __construct(TemplateHierarchyInterface $hierarchy, ThemeNamespace $themeNamespace, TranslatorInterface $translator)
public function __construct(ContaoFilesystemLoader $filesystemLoader, ThemeNamespace $themeNamespace, TranslatorInterface $translator)
{
$this->hierarchy = $hierarchy;
$this->filesystemLoader = $filesystemLoader;
$this->themeNamespace = $themeNamespace;
$this->translator = $translator;
}
Expand Down Expand Up @@ -146,7 +146,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->filesystemLoader->getInheritanceChains($this->themeSlug),
static function (array $chain) {
foreach (array_keys($chain) as $path) {
if ('html5' !== Path::getExtension($path, true)) {
Expand Down
10 changes: 5 additions & 5 deletions core-bundle/src/Twig/Finder/FinderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@

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\Contracts\Translation\TranslatorInterface;

class FinderFactory
{
private TemplateHierarchyInterface $hierarchy;
private ContaoFilesystemLoader $filesystemLoader;
private ThemeNamespace $themeNamespace;
private TranslatorInterface $translator;

/**
* @internal
*/
public function __construct(TemplateHierarchyInterface $hierarchy, ThemeNamespace $themeNamespace, TranslatorInterface $translator)
public function __construct(ContaoFilesystemLoader $filesystemLoader, ThemeNamespace $themeNamespace, TranslatorInterface $translator)
{
$this->hierarchy = $hierarchy;
$this->filesystemLoader = $filesystemLoader;
$this->themeNamespace = $themeNamespace;
$this->translator = $translator;
}
Expand All @@ -37,6 +37,6 @@ public function __construct(TemplateHierarchyInterface $hierarchy, ThemeNamespac
*/
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 @@ -28,11 +29,11 @@
*/
final class DynamicExtendsTokenParser extends AbstractTokenParser
{
private TemplateHierarchyInterface $hierarchy;
private ContaoFilesystemLoader $filesystemLoader;

public function __construct(TemplateHierarchyInterface $hierarchy)
public function __construct(ContaoFilesystemLoader $filesystemLoader)
{
$this->hierarchy = $hierarchy;
$this->filesystemLoader = $filesystemLoader;
}

public function parse(Token $token): Node
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
15 changes: 7 additions & 8 deletions core-bundle/src/Twig/Inheritance/DynamicIncludeTokenParser.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\Node\Expression\ArrayExpression;
use Twig\Node\Expression\ConstantExpression;
use Twig\Node\IncludeNode;
Expand All @@ -29,11 +30,11 @@
*/
final class DynamicIncludeTokenParser extends AbstractTokenParser
{
private TemplateHierarchyInterface $hierarchy;
private ContaoFilesystemLoader $filesystemLoader;

public function __construct(TemplateHierarchyInterface $hierarchy)
public function __construct(ContaoFilesystemLoader $filesystemLoader)
{
$this->hierarchy = $hierarchy;
$this->filesystemLoader = $filesystemLoader;
}

public function parse(Token $token): IncludeNode
Expand All @@ -56,13 +57,11 @@ public function getTag(): string
* Return the adjusted logical name or the unchanged input if it does not
* match the Contao Twig namespace.
*
* TODO change signature to public static function adjustTemplateName(string|TemplateWrapper $name, TemplateHierarchyInterface $hierarchy): string|TemplateWrapper in Contao 5.1
*
* @param string|TemplateWrapper $name
*
* @return string|TemplateWrapper
*/
public static function adjustTemplateName($name, TemplateHierarchyInterface $hierarchy)
public static function adjustTemplateName($name, ContaoFilesystemLoader $filesystemLoader)
{
if (!\is_string($name)) {
if ($name instanceof TemplateWrapper) {
Expand All @@ -79,7 +78,7 @@ public static function adjustTemplateName($name, TemplateHierarchyInterface $hie
}

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 @@ -133,7 +132,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
50 changes: 0 additions & 50 deletions core-bundle/src/Twig/Inheritance/TemplateHierarchyInterface.php

This file was deleted.

0 comments on commit fa9ec09

Please sign in to comment.