Skip to content

[TwigBundle] Use kernel.build_dir to store the templates known at build time #54384

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

Open
wants to merge 1 commit into
base: 7.3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Symfony/Bundle/TwigBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ CHANGELOG
* Enable `#[AsTwigFilter]`, `#[AsTwigFunction]` and `#[AsTwigTest]` attributes
to configure extensions on runtime classes
* Add support for a `twig` validator
* Use `ChainCache` to store warmed-up cache in `kernel.build_dir` and runtime cache in `kernel.cache_dir`
* Make `TemplateCacheWarmer` use `kernel.build_dir` instead of `kernel.cache_dir`

7.1
---
Expand Down
44 changes: 34 additions & 10 deletions src/Symfony/Bundle/TwigBundle/CacheWarmer/TemplateCacheWarmer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use Psr\Container\ContainerInterface;
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
use Symfony\Contracts\Service\ServiceSubscriberInterface;
use Twig\Cache\CacheInterface;
use Twig\Cache\NullCache;
use Twig\Environment;
use Twig\Error\Error;

Expand All @@ -34,26 +36,48 @@ class TemplateCacheWarmer implements CacheWarmerInterface, ServiceSubscriberInte
public function __construct(
private ContainerInterface $container,
private iterable $iterator,
private ?CacheInterface $cache = null,
) {
}

public function warmUp(string $cacheDir, ?string $buildDir = null): array
{
$this->twig ??= $this->container->get('twig');

foreach ($this->iterator as $template) {
try {
$this->twig->load($template);
} catch (Error) {
$originalCache = $this->twig->getCache();
if ($originalCache instanceof NullCache) {
// There's no point to warm up a cache that won't be used afterward
return [];
}

if (null !== $this->cache) {
if (!$buildDir) {
/*
* Problem during compilation, give up for this template (e.g. syntax errors).
* Failing silently here allows to ignore templates that rely on functions that aren't available in
* the current environment. For example, the WebProfilerBundle shouldn't be available in the prod
* environment, but some templates that are never used in prod might rely on functions the bundle provides.
* As we can't detect which templates are "really" important, we try to load all of them and ignore
* errors. Error checks may be performed by calling the lint:twig command.
* The cache has already been warmup during the build of the container, when $buildDir was set.
*/
return [];
}
// Swap the cache for the warmup as the Twig Environment has the ChainCache injected
$this->twig->setCache($this->cache);
}

try {
foreach ($this->iterator as $template) {
try {
$this->twig->load($template);
} catch (Error) {
/*
* Problem during compilation, give up for this template (e.g. syntax errors).
* Failing silently here allows to ignore templates that rely on functions that aren't available in
* the current environment. For example, the WebProfilerBundle shouldn't be available in the prod
* environment, but some templates that are never used in prod might rely on functions the bundle provides.
* As we can't detect which templates are "really" important, we try to load all of them and ignore
* errors. Error checks may be performed by calling the lint:twig command.
*/
}
}
} finally {
$this->twig->setCache($originalCache);
}

return [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ private function addTwigOptions(ArrayNodeDefinition $rootNode): void
->example('Twig\Template')
->cannotBeEmpty()
->end()
->scalarNode('cache')->defaultValue('%kernel.cache_dir%/twig')->end()
->scalarNode('cache')->defaultTrue()->end()
->scalarNode('charset')->defaultValue('%kernel.charset%')->end()
->booleanNode('debug')->defaultValue('%kernel.debug%')->end()
->booleanNode('strict_variables')->defaultValue('%kernel.debug%')->end()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use Twig\Attribute\AsTwigFilter;
use Twig\Attribute\AsTwigFunction;
use Twig\Attribute\AsTwigTest;
use Twig\Cache\FilesystemCache;
use Twig\Environment;
use Twig\Extension\ExtensionInterface;
use Twig\Extension\RuntimeExtensionInterface;
Expand Down Expand Up @@ -167,6 +168,31 @@ public function load(array $configs, ContainerBuilder $container): void
}
}

if (true === $config['cache']) {
$autoReloadOrDefault = $container->getParameterBag()->resolveValue($config['auto_reload'] ?? $config['debug']);
$buildDir = $container->getParameter('kernel.build_dir');
$cacheDir = $container->getParameter('kernel.cache_dir');

if ($autoReloadOrDefault || $cacheDir === $buildDir) {
$config['cache'] = '%kernel.cache_dir%/twig';
}
}

if (true === $config['cache']) {
$config['cache'] = new Reference('twig.template_cache.chain');
} else {
$container->removeDefinition('twig.template_cache.chain');
$container->removeDefinition('twig.template_cache.runtime_cache');
$container->removeDefinition('twig.template_cache.readonly_cache');
$container->removeDefinition('twig.template_cache.warmup_cache');

if (false === $config['cache']) {
$container->removeDefinition('twig.template_cache_warmer');
} else {
$container->getDefinition('twig.template_cache_warmer')->replaceArgument(2, null);
}
}

if (isset($config['autoescape_service'])) {
$config['autoescape'] = [new Reference($config['autoescape_service']), $config['autoescape_service_method'] ?? '__invoke'];
} else {
Expand All @@ -191,10 +217,6 @@ public function load(array $configs, ContainerBuilder $container): void
$container->registerAttributeForAutoconfiguration(AsTwigFilter::class, AttributeExtensionPass::autoconfigureFromAttribute(...));
$container->registerAttributeForAutoconfiguration(AsTwigFunction::class, AttributeExtensionPass::autoconfigureFromAttribute(...));
$container->registerAttributeForAutoconfiguration(AsTwigTest::class, AttributeExtensionPass::autoconfigureFromAttribute(...));

if (false === $config['cache']) {
$container->removeDefinition('twig.template_cache_warmer');
}
}

private function getBundleTemplatePaths(ContainerBuilder $container, array $config): array
Expand Down
20 changes: 19 additions & 1 deletion src/Symfony/Bundle/TwigBundle/Resources/config/twig.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@
use Symfony\Bundle\TwigBundle\CacheWarmer\TemplateCacheWarmer;
use Symfony\Bundle\TwigBundle\DependencyInjection\Configurator\EnvironmentConfigurator;
use Symfony\Bundle\TwigBundle\TemplateIterator;
use Twig\Cache\ChainCache;
use Twig\Cache\FilesystemCache;
use Twig\Cache\ReadOnlyFilesystemCache;
use Twig\Environment;
use Twig\Extension\CoreExtension;
use Twig\Extension\DebugExtension;
Expand Down Expand Up @@ -79,8 +81,24 @@
->set('twig.template_iterator', TemplateIterator::class)
->args([service('kernel'), abstract_arg('Twig paths'), param('twig.default_path'), abstract_arg('File name pattern')])

->set('twig.template_cache.runtime_cache', FilesystemCache::class)
->args([param('kernel.cache_dir').'/twig'])

->set('twig.template_cache.readonly_cache', ReadOnlyFilesystemCache::class)
->args([param('kernel.build_dir').'/twig'])

->set('twig.template_cache.warmup_cache', FilesystemCache::class)
->args([param('kernel.build_dir').'/twig'])

->set('twig.template_cache.chain', ChainCache::class)
->args([[service('twig.template_cache.readonly_cache'), service('twig.template_cache.runtime_cache')]])

->set('twig.template_cache_warmer', TemplateCacheWarmer::class)
->args([service(ContainerInterface::class), service('twig.template_iterator')])
->args([
service(ContainerInterface::class),
service('twig.template_iterator'),
service('twig.template_cache.warmup_cache'),
])
->tag('kernel.cache_warmer')
->tag('container.service_subscriber', ['id' => 'twig'])

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
'pi' => 3.14,
'bad' => ['key' => 'foo'],
],
'auto_reload' => true,
'cache' => '/tmp',
'auto_reload' => false,
'charset' => 'ISO-8859-1',
'debug' => true,
'strict_variables' => true,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

$container->loadFromExtension('twig', [
'cache' => false,
]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

$container->loadFromExtension('twig', [
'cache' => 'random-path',
]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

$container->loadFromExtension('twig', [
'cache' => true,
'auto_reload' => false,
]);
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/twig https://symfony.com/schema/dic/twig/twig-1.0.xsd">

<twig:config auto-reload="true" cache="/tmp" charset="ISO-8859-1" debug="true" strict-variables="true">
<twig:config auto-reload="true" charset="ISO-8859-1" debug="true" strict-variables="true">
<twig:path namespace="namespace3">namespaced_path3</twig:path>
</twig:config>
</container>
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/twig https://symfony.com/schema/dic/twig/twig-1.0.xsd">

<twig:config auto-reload="true" cache="/tmp" charset="ISO-8859-1" debug="true" strict-variables="true" default-path="%kernel.project_dir%/Fixtures/templates">
<twig:config auto-reload="false" charset="ISO-8859-1" debug="true" strict-variables="true" default-path="%kernel.project_dir%/Fixtures/templates">
<twig:form-theme>MyBundle::form.html.twig</twig:form-theme>
<twig:global key="foo" id="bar" type="service" />
<twig:global key="baz">@@qux</twig:global>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:twig="http://symfony.com/schema/dic/twig"
xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/twig https://symfony.com/schema/dic/twig/twig-1.0.xsd">

<twig:config cache="false" />
</container>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:twig="http://symfony.com/schema/dic/twig"
xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/twig https://symfony.com/schema/dic/twig/twig-1.0.xsd">

<twig:config cache="random-path" />
</container>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:twig="http://symfony.com/schema/dic/twig"
xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/twig https://symfony.com/schema/dic/twig/twig-1.0.xsd">

<twig:config cache="true" auto-reload="false" />
</container>
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ twig:
baz: "@@qux"
pi: 3.14
bad: {key: foo}
auto_reload: true
cache: /tmp
auto_reload: false
charset: ISO-8859-1
debug: true
strict_variables: true
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
twig:
cache: false
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
twig:
cache: random-path
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
twig:
cache: true
auto_reload: false
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ public function testLoadEmptyConfiguration()
}

/**
* @dataProvider getFormats
* @dataProvider getFormatsAndBuildDir
*/
public function testLoadFullConfiguration(string $format)
public function testLoadFullConfiguration(string $format, ?string $buildDir)
{
$container = $this->createContainer();
$container = $this->createContainer($buildDir);
$container->registerExtension(new TwigExtension());
$this->loadFromFile($container, 'full', $format);
$this->compileContainer($container);
Expand Down Expand Up @@ -92,13 +92,64 @@ public function testLoadFullConfiguration(string $format)

// Twig options
$options = $container->getDefinition('twig')->getArgument(1);
$this->assertTrue($options['auto_reload'], '->load() sets the auto_reload option');
$this->assertFalse($options['auto_reload'], '->load() sets the auto_reload option');
$this->assertSame('name', $options['autoescape'], '->load() sets the autoescape option');
$this->assertArrayNotHasKey('base_template_class', $options, '->load() does not set the base_template_class if none is provided');
$this->assertEquals('/tmp', $options['cache'], '->load() sets the cache option');
$this->assertEquals('ISO-8859-1', $options['charset'], '->load() sets the charset option');
$this->assertTrue($options['debug'], '->load() sets the debug option');
$this->assertTrue($options['strict_variables'], '->load() sets the strict_variables option');
$this->assertEquals($buildDir !== null ? new Reference('twig.template_cache.chain') : '%kernel.cache_dir%/twig', $options['cache'], '->load() sets the cache option');
}

/**
* @dataProvider getFormatsAndBuildDir
*/
public function testLoadNoCacheConfiguration(string $format, ?string $buildDir)
{
$container = $this->createContainer($buildDir);
$container->registerExtension(new TwigExtension());
$this->loadFromFile($container, 'no-cache', $format);
$this->compileContainer($container);

$this->assertEquals(Environment::class, $container->getDefinition('twig')->getClass(), '->load() loads the twig.xml file');

// Twig options
$options = $container->getDefinition('twig')->getArgument(1);
$this->assertFalse($options['cache'], '->load() sets cache option to false');
}

/**
* @dataProvider getFormatsAndBuildDir
*/
public function testLoadPathCacheConfiguration(string $format, ?string $buildDir)
{
$container = $this->createContainer($buildDir);
$container->registerExtension(new TwigExtension());
$this->loadFromFile($container, 'path-cache', $format);
$this->compileContainer($container);

$this->assertEquals(Environment::class, $container->getDefinition('twig')->getClass(), '->load() loads the twig.xml file');

// Twig options
$options = $container->getDefinition('twig')->getArgument(1);
$this->assertSame('random-path', $options['cache'], '->load() sets cache option to string path');
}

/**
* @dataProvider getFormatsAndBuildDir
*/
public function testLoadProdCacheConfiguration(string $format, ?string $buildDir)
{
$container = $this->createContainer($buildDir);
$container->registerExtension(new TwigExtension());
$this->loadFromFile($container, 'prod-cache', $format);
$this->compileContainer($container);

$this->assertEquals(Environment::class, $container->getDefinition('twig')->getClass(), '->load() loads the twig.xml file');

// Twig options
$options = $container->getDefinition('twig')->getArgument(1);
$this->assertEquals($buildDir !== null ? new Reference('twig.template_cache.chain') : '%kernel.cache_dir%/twig', $options['cache'], '->load() sets cache option to CacheChain reference');
}

/**
Expand Down Expand Up @@ -238,6 +289,19 @@ public static function getFormats(): array
];
}

public static function getFormatsAndBuildDir(): array
{
return [
['php', null],
['php', __DIR__.'/build'],
['yml', null],
['yml', __DIR__.'/build'],
['xml', null],
['xml', __DIR__.'/build'],
];
}


/**
* @dataProvider stopwatchExtensionAvailabilityProvider
*/
Expand Down Expand Up @@ -312,10 +376,11 @@ public function testCustomHtmlToTextConverterService(string $format)
$this->assertEquals(new Reference('my_converter'), $bodyRenderer->getArgument('$converter'));
}

private function createContainer(): ContainerBuilder
private function createContainer(?string $buildDir = null): ContainerBuilder
{
$container = new ContainerBuilder(new ParameterBag([
'kernel.cache_dir' => __DIR__,
'kernel.build_dir' => $buildDir ?? __DIR__,
'kernel.project_dir' => __DIR__,
'kernel.charset' => 'UTF-8',
'kernel.debug' => false,
Expand Down
Loading