Skip to content
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: 1 addition & 1 deletion CHANGELOG
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
7 changes: 6 additions & 1 deletion src/ExtensionSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 38 additions & 0 deletions tests/EnvironmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => '']));
Expand Down Expand Up @@ -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(...))];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can add a comment on this line that this must must not be simplified to return (new AttributeExtension(self::class))->getFunctions();, which rely on the runtime instead of the extension, and would not test this behavior.

}

#[AsTwigFunction('cache_test_function')]
public function cacheTestFunction(): string
{
return 'ok';
}
}
Loading