Reject a Template instance created by another environment when resolving templates

This commit is contained in:
Fabien Potencier
2026-09-25 23:34:59 +02:00
parent 2904612341
commit 605b1278f7
3 changed files with 21 additions and 0 deletions
+1
View File
@@ -1,5 +1,6 @@
# 3.30.1 (2026-XX-XX)
* Reject a deprecated `Template` instance created by another environment in `Environment::resolveTemplate()`
* Speed up reading object attributes backed by getters or class constants
# 3.30.0 (2026-09-25)
+4
View File
@@ -514,6 +514,10 @@ class Environment
if ($name instanceof Template) {
trigger_deprecation('twig/twig', '3.9', 'Passing a "%s" instance to "%s" is deprecated.', Template::class, __METHOD__);
if (!$name->isOwnedBy($this)) {
throw new RuntimeError(\sprintf('A "%s" can only be used with the "%s" that created it.', Template::class, self::class));
}
return new TemplateWrapper($this, $name);
}
if ($name instanceof TemplateWrapper) {
+16
View File
@@ -96,6 +96,22 @@ class EnvironmentTest extends TestCase
$twig->load($foreign->load('index'));
}
/**
* @group legacy
*/
#[Group('legacy')]
public function testResolveTemplateRejectsTemplateFromAnotherEnvironment(): void
{
$foreign = new Environment(new ArrayLoader(['index' => 'foreign']));
$twig = new Environment(new ArrayLoader());
$this->expectDeprecation('Since twig/twig 3.9: Passing a "Twig\Template" instance to "Twig\Environment::resolveTemplate" is deprecated.');
$this->expectException(RuntimeError::class);
$this->expectExceptionMessage('A "Twig\Template" can only be used with the "Twig\Environment" that created it.');
$twig->resolveTemplate([$foreign->load('index')->unwrap($foreign)]);
}
/**
* @group legacy
*/