Skip to content

Commit

Permalink
bug #4083 Fix BC break on escaper extension (fabpot)
Browse files Browse the repository at this point in the history
This PR was merged into the 3.x branch.

Discussion
----------

Fix BC break on escaper extension

Closes #4082

`@tacman` Can you test that this fixes your issue?

Commits
-------

1953f77 Fix BC break on escaper extension
  • Loading branch information
fabpot committed May 11, 2024
2 parents 4313162 + 1953f77 commit 1de5736
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# 3.10.1 (2024-XX-XX)

* Fix BC break on escaper extension
* Fix constant return type

# 3.10.0 (2024-05-11)
Expand Down
4 changes: 3 additions & 1 deletion src/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,9 @@ public function __construct(LoaderInterface $loader, $options = [])
]);

$this->addExtension(new CoreExtension());
$this->addExtension(new EscaperExtension($options['autoescape']));
$escaperExt = new EscaperExtension($options['autoescape']);
$escaperExt->setEnvironment($this, false);
$this->addExtension($escaperExt);
if (\PHP_VERSION_ID >= 80000) {
$this->addExtension(new YieldNotReadyExtension($this->useYield));
}
Expand Down
14 changes: 7 additions & 7 deletions src/Extension/EscaperExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,14 @@ public function getFilters(): array
/**
* @deprecated since Twig 3.10
*/
public function setEnvironment(Environment $environment): void
public function setEnvironment(Environment $environment, bool $triggerDeprecation = true): void
{
trigger_deprecation('twig/twig', '3.10', 'The "%s()" method is deprecated and not needed if you are using methods from "Twig\Runtime\EscaperRuntime".', __METHOD__);
if ($triggerDeprecation) {
trigger_deprecation('twig/twig', '3.10', 'The "%s()" method is deprecated and not needed if you are using methods from "Twig\Runtime\EscaperRuntime".', __METHOD__);
}

$this->environment = $environment;
$this->escaper = $environment->getRuntime(EscaperRuntime::class);
}

/**
Expand Down Expand Up @@ -126,9 +129,6 @@ public function setEscaper($strategy, callable $callable)
if (!isset($this->environment)) {
throw new \LogicException(sprintf('You must call "setEnvironment()" before calling "%s()".', __METHOD__));
}
if (!isset($this->escaper)) {
throw new \LogicException(sprintf('You must call "setEscaperRuntime()" before calling "%s()".', __METHOD__));
}

$this->escapers[$strategy] = $callable;
$callable = function ($string, $charset) use ($callable) {
Expand Down Expand Up @@ -160,7 +160,7 @@ public function setSafeClasses(array $safeClasses = [])
trigger_deprecation('twig/twig', '3.10', 'The "%s()" method is deprecated, use the "Twig\Runtime\EscaperRuntime::setSafeClasses()" method instead.', __METHOD__);

if (!isset($this->escaper)) {
throw new \LogicException(sprintf('You must call "setEscaperRuntime()" before calling %s().', __METHOD__));
throw new \LogicException(sprintf('You must call "setEnvironment()" before calling "%s()".', __METHOD__));
}

$this->escaper->setSafeClasses($safeClasses);
Expand All @@ -174,7 +174,7 @@ public function addSafeClass(string $class, array $strategies)
trigger_deprecation('twig/twig', '3.10', 'The "%s()" method is deprecated, use the "Twig\Runtime\EscaperRuntime::addSafeClass()" method instead.', __METHOD__);

if (!isset($this->escaper)) {
throw new \LogicException(sprintf('You must call setEscaperRuntime() before calling %s().', __METHOD__));
throw new \LogicException(sprintf('You must call "setEnvironment()" before calling "%s()".', __METHOD__));
}

$this->escaper->addSafeClass($class, $strategies);
Expand Down
19 changes: 13 additions & 6 deletions tests/Extension/EscaperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ public function testCustomEscaper($expected, $string, $strategy)
{
$twig = new Environment($this->createMock(LoaderInterface::class));
$escaperExt = $twig->getExtension(EscaperExtension::class);
$escaperExt->setEnvironment($twig);
$escaperExt->setEscaperRuntime($twig->getRuntime(EscaperRuntime::class));
$escaperExt->setEscaper('foo', 'Twig\Tests\legacy_escaper');
$this->assertSame($expected, $twig->getRuntime(EscaperRuntime::class)->escape($string, $strategy));
}
Expand All @@ -43,21 +41,30 @@ public function provideCustomEscaperCases()
];
}

/**
* @dataProvider provideCustomEscaperCases
*
* @group legacy
*/
public function testCustomEscaperWithoutCallingSetEscaperRuntime($expected, $string, $strategy)
{
$twig = new Environment($this->createMock(LoaderInterface::class));
$escaperExt = $twig->getExtension(EscaperExtension::class);
$escaperExt->setEscaper('foo', 'Twig\Tests\legacy_escaper');
$this->assertSame($expected, $twig->getRuntime(EscaperRuntime::class)->escape($string, $strategy));
}

/**
* @group legacy
*/
public function testCustomEscapersOnMultipleEnvs()
{
$env1 = new Environment($this->createMock(LoaderInterface::class));
$escaperExt1 = $env1->getExtension(EscaperExtension::class);
$escaperExt1->setEnvironment($env1);
$escaperExt1->setEscaperRuntime($env1->getRuntime(EscaperRuntime::class));
$escaperExt1->setEscaper('foo', 'Twig\Tests\legacy_escaper');

$env2 = new Environment($this->createMock(LoaderInterface::class));
$escaperExt2 = $env2->getExtension(EscaperExtension::class);
$escaperExt2->setEscaperRuntime($env2->getRuntime(EscaperRuntime::class));
$escaperExt2->setEnvironment($env2);
$escaperExt2->setEscaper('foo', 'Twig\Tests\legacy_escaper_again');

$this->assertSame('fooUTF-8', $env1->getRuntime(EscaperRuntime::class)->escape('foo', 'foo'));
Expand Down

0 comments on commit 1de5736

Please sign in to comment.