From 77a6053c67b6184918a37545d8947d1734af1d62 Mon Sep 17 00:00:00 2001 From: "a.bolonin" Date: Sun, 29 Nov 2026 08:20:10 +0300 Subject: [PATCH] Fix the template cache signature for attribute extension wrappers --- CHANGELOG | 2 +- src/ExtensionSet.php | 7 ++++++- tests/EnvironmentTest.php | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 868a802124d..74b80289b2d 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,6 @@ # 3.31.2 (2026-XX-XX) - * n/a + * Fix compiled template cache collisions between regular extensions and their attribute wrappers # 3.31.1 (2026-11-24) diff --git a/src/ExtensionSet.php b/src/ExtensionSet.php index 7ec378b7a46..1d6da208f53 100644 --- a/src/ExtensionSet.php +++ b/src/ExtensionSet.php @@ -116,7 +116,12 @@ public function getExtensions(): array public function getSignature(): string { - return serialize(array_keys($this->extensions)); + $signature = []; + foreach ($this->extensions as $class => $extension) { + $signature[] = $class === $extension::class ? $class : [$class, $extension::class]; + } + + return serialize($signature); } public function getSandboxSignature(): string diff --git a/tests/EnvironmentTest.php b/tests/EnvironmentTest.php index 0b52f5fc2f4..785f8b73d95 100644 --- a/tests/EnvironmentTest.php +++ b/tests/EnvironmentTest.php @@ -735,6 +735,30 @@ public function testTemplateClassFollowsExtensionsAddedAfterItWasComputed(): voi $this->assertSame($cls, $other->getTemplateClass('index')); } + public function testTemplateCacheDistinguishesAnExtensionFromItsAttributeWrapper(): void + { + $dir = sys_get_temp_dir().'/twig-extension-cache-'.bin2hex(random_bytes(8)); + $loader = new ArrayLoader(['index' => '{{ cache_test_function() }}']); + $options = ['cache' => $dir, 'auto_reload' => false]; + + try { + $eager = new Environment($loader, $options); + $eager->addExtension(new EnvironmentTest_CacheTestExtension()); + $this->assertSame('ok', $eager->render('index')); + + $lazy = new Environment($loader, $options); + $lazy->addExtension(new AttributeExtension(EnvironmentTest_CacheTestExtension::class)); + $lazy->addRuntimeLoader(new FactoryRuntimeLoader([ + EnvironmentTest_CacheTestExtension::class => static fn () => new EnvironmentTest_CacheTestExtension(), + ])); + + $this->assertSame('ok', $lazy->render('index')); + $this->assertNotSame($eager->getTemplateClass('index'), $lazy->getTemplateClass('index')); + } finally { + FilesystemHelper::removeDir($dir); + } + } + public function testTemplateClassFollowsDebugAndStrictVariablesChanges(): void { $twig = new Environment(new ArrayLoader(['index' => ''])); @@ -1120,3 +1144,17 @@ public function attributeFunction(string $value): string return $value.$this->suffix; } } + +class EnvironmentTest_CacheTestExtension extends AbstractExtension +{ + public function getFunctions(): array + { + return [new TwigFunction('cache_test_function', $this->cacheTestFunction(...))]; + } + + #[AsTwigFunction('cache_test_function')] + public function cacheTestFunction(): string + { + return 'ok'; + } +}