mirror of
https://github.com/twigphp/Twig.git
synced 2026-10-02 09:56:49 +00:00
Reject a Template instance created by another environment when resolving templates
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user