From fff62b9eb10b28e40c0d0de238799c482b46cf28 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Tue, 5 Jan 2021 10:39:31 +0100 Subject: [PATCH] Improve wiring --- doc/tags/cache.rst | 3 ++- extra/cache-extra/Node/CacheNode.php | 12 ------------ .../src/Resources/config/cache.php | 18 ++++++++++++++++-- 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/doc/tags/cache.rst b/doc/tags/cache.rst index b0356afba..61b3c0a3e 100644 --- a/doc/tags/cache.rst +++ b/doc/tags/cache.rst @@ -100,13 +100,14 @@ are local to the template fragment: If you are not using Symfony, you must also register the extension runtime:: use Symfony\Component\Cache\Adapter\FilesystemAdapter; + use Symfony\Component\Cache\Adapter\TagAwareAdapter; use Twig\Extra\Cache\CacheRuntime; use Twig\RuntimeLoader\RuntimeLoaderInterface; $twig->addRuntimeLoader(new class implements RuntimeLoaderInterface { public function load($class) { if (CacheRuntime::class === $class) { - return new CacheRuntime(new FilesystemAdapter()); + return new CacheRuntime(new TagAwareAdapter(new FilesystemAdapter())); } } }); diff --git a/extra/cache-extra/Node/CacheNode.php b/extra/cache-extra/Node/CacheNode.php index 9f949c7ef..d6b438934 100644 --- a/extra/cache-extra/Node/CacheNode.php +++ b/extra/cache-extra/Node/CacheNode.php @@ -38,9 +38,6 @@ class CacheNode extends Node ->subcompile($this->getNode('key')) ->raw(", function (\Symfony\Contracts\Cache\ItemInterface \$item) use (\$context) {\n") ->indent() - ->write("try {\n") - ->indent() - ->write("\$item->tag('twig');\n") ; if ($this->hasNode('tags')) { @@ -51,15 +48,6 @@ class CacheNode extends Node ; } - $compiler - ->outdent() - ->write("} catch (\Psr\Cache\CacheException \$e) {\n") - ->indent() - ->write("// cache doesn't support tags\n") - ->outdent() - ->write("}\n") - ; - if ($this->hasNode('ttl')) { $compiler ->write('$item->expiresAfter(') diff --git a/extra/twig-extra-bundle/src/Resources/config/cache.php b/extra/twig-extra-bundle/src/Resources/config/cache.php index 38f6d7f4c..bf4064d33 100644 --- a/extra/twig-extra-bundle/src/Resources/config/cache.php +++ b/extra/twig-extra-bundle/src/Resources/config/cache.php @@ -11,6 +11,9 @@ namespace Symfony\Component\DependencyInjection\Loader\Configurator; +use Psr\Cache\CacheItemPoolInterface; +use Symfony\Component\Cache\Adapter\TagAwareAdapter; +use Symfony\Contracts\Cache\CacheInterface; use Symfony\Contracts\Cache\TagAwareCacheInterface; use Twig\Extra\Cache\CacheExtension; use Twig\Extra\Cache\CacheRuntime; @@ -22,10 +25,21 @@ return static function (ContainerConfigurator $container) { ->set('twig.runtime.cache', CacheRuntime::class) ->args([ - service('twig.cache.default'), + service('twig.cache'), ]) ->tag('twig.runtime') - ->alias('twig.cache.default', TagAwareCacheInterface::class) + ->set('twig.cache', TagAwareAdapter::class) + ->args([ + service('.twig.cache.inner') + ]) + + ->set('.twig.cache.inner') + ->parent('cache.app') + ->tag('cache.pool', ['name' => 'twig.cache']) + + ->alias(TagAwareCacheInterface::class.' $twigCache', 'twig.cache') + ->alias(CacheInterface::class.' $twigCache', '.twig.cache.inner') + ->alias(CacheItemPoolInterface::class.' $twigCache', '.twig.cache.inner') ; };