Allow to call TemplateWrapper::unwrap() without arguments

This commit is contained in:
Alexander M. Turek
2026-09-21 18:10:25 +02:00
parent 4b7c937485
commit bfbe89e3a3
2 changed files with 22 additions and 2 deletions
+4 -2
View File
@@ -121,9 +121,11 @@ final class TemplateWrapper
/**
* @internal
*/
public function unwrap(Environment $env): Template
public function unwrap(?Environment $env = null): Template
{
if (!$this->isOwnedBy($env)) {
if (null === $env) {
trigger_deprecation('twig/twig', '3.29', 'Calling "%s()" without arguments is deprecated, pass the Twig environment instead.', __METHOD__);
} elseif (!$this->isOwnedBy($env)) {
throw new RuntimeError(\sprintf('A "%s" can only be used with the "%s" that created it.', self::class, Environment::class));
}
+18
View File
@@ -21,7 +21,9 @@ namespace Twig\Tests;
*/
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\TestCase;
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
use Twig\Environment;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
@@ -31,6 +33,8 @@ use Twig\TwigFunction;
class TemplateWrapperTest extends TestCase
{
use ExpectDeprecationTrait;
public function testUnwrapChecksTheEnvironment(): void
{
$twig = new Environment(new ArrayLoader(['index' => 'content']));
@@ -43,6 +47,20 @@ class TemplateWrapperTest extends TestCase
$wrapper->unwrap(new Environment(new ArrayLoader()));
}
/**
* @group legacy
*/
#[Group('legacy')]
public function testUnwrapCanBeCalledWithoutArguments(): void
{
$twig = new Environment(new ArrayLoader(['index' => 'content']));
$wrapper = $twig->load('index');
$this->expectDeprecation('Since twig/twig 3.29: Calling "Twig\TemplateWrapper::unwrap()" without arguments is deprecated, pass the Twig environment instead.');
$this->assertInstanceOf(Template::class, $wrapper->unwrap());
}
public function testGetDefaultEscapeStrategy(): void
{
$twig = new Environment(new ArrayLoader(['index.js.twig' => 'content']), ['autoescape' => 'name']);