mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-10 01:16:52 +00:00
Fix EscaperExtension constructor to not change from previous releases (will be the same in 4.x as well)
This commit is contained in:
+1
-1
@@ -135,7 +135,7 @@ class Environment
|
||||
]);
|
||||
|
||||
$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));
|
||||
}
|
||||
|
||||
@@ -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
|
||||
@@ -67,6 +66,16 @@ final class EscaperExtension extends AbstractExtension
|
||||
$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.
|
||||
*
|
||||
@@ -115,7 +124,10 @@ final class EscaperExtension extends AbstractExtension
|
||||
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;
|
||||
@@ -147,6 +159,10 @@ final class EscaperExtension extends AbstractExtension
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -157,6 +173,10 @@ final class EscaperExtension extends AbstractExtension
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
@@ -334,7 +334,7 @@ class EnvironmentTest extends TestCase
|
||||
'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);
|
||||
|
||||
|
||||
@@ -29,6 +29,7 @@ class EscaperTest extends TestCase
|
||||
$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));
|
||||
}
|
||||
@@ -50,10 +51,12 @@ class EscaperTest extends TestCase
|
||||
$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');
|
||||
|
||||
|
||||
Reference in New Issue
Block a user