Skip to content

Commit

Permalink
feature twigphp#4074 Fix EscaperExtension constructor to not change f…
Browse files Browse the repository at this point in the history
…rom previous releases (will be the same in 4.x as well) (fabpot)

This PR was merged into the 3.x branch.

Discussion
----------

Fix EscaperExtension constructor to not change from previous releases (will be the same in 4.x as well)

Commits
-------

f2c9795 Fix EscaperExtension constructor to not change from previous releases (will be the same in 4.x as well)
  • Loading branch information
fabpot committed May 4, 2024
2 parents 450d2fb + f2c9795 commit 78466e8
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public function __construct(LoaderInterface $loader, $options = [])
]);

$this->addExtension(new CoreExtension());
$this->addExtension(new EscaperExtension($this->getRuntime(EscaperRuntime::class), $options['autoescape']));
$this->addExtension(new EscaperExtension($options['autoescape']));
if (\PHP_VERSION_ID >= 80000) {
$this->addExtension(new YieldNotReadyExtension($this->useYield));
}
Expand Down
26 changes: 23 additions & 3 deletions src/Extension/EscaperExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,9 @@ final class EscaperExtension extends AbstractExtension
*
* @see setDefaultStrategy()
*/
public function __construct(EscaperRuntime $escaper, $defaultStrategy = 'html')
public function __construct($defaultStrategy = 'html')
{
$this->setDefaultStrategy($defaultStrategy);
$this->escaper = $escaper;
}

public function getTokenParsers(): array
Expand Down Expand Up @@ -67,6 +66,16 @@ public function setEnvironment(Environment $environment): void
$this->environment = $environment;
}

/**
* @deprecated since Twig 3.10
*/
public function setEscaperRuntime(EscaperRuntime $escaper)
{
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->escaper = $escaper;
}

/**
* Sets the default strategy to use when not defined by the user.
*
Expand Down Expand Up @@ -115,7 +124,10 @@ public function setEscaper($strategy, callable $callable)
trigger_deprecation('twig/twig', '3.10', 'The "%s()" method is deprecated, use the "Twig\Runtime\EscaperRuntime::setEscaper()" method instead (be warned that Environment is not passed anymore to the callable).', __METHOD__);

if (!isset($this->environment)) {
throw new \LogicException('You must call setEnvironment() before calling setEscaper().');
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;
Expand Down Expand Up @@ -147,6 +159,10 @@ 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__));
}

$this->escaper->setSafeClasses($safeClasses);
}

Expand All @@ -157,6 +173,10 @@ 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__));
}

$this->escaper->addSafeClass($class, $strategies);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/EnvironmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ public function testAddRuntimeLoader()
'func_string_named_args' => '{{ from_runtime_string(name="foo") }}',
]);

$twig = new Environment($loader);
$twig = new Environment($loader, ['autoescape' => false]);
$twig->addExtension(new EnvironmentTest_ExtensionWithoutRuntime());
$twig->addRuntimeLoader($runtimeLoader);

Expand Down
3 changes: 3 additions & 0 deletions tests/Extension/EscaperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ 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 @@ -50,10 +51,12 @@ 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');

Expand Down

0 comments on commit 78466e8

Please sign in to comment.