diff --git a/CHANGELOG b/CHANGELOG index 065b8baa03..5098440bdb 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -7,9 +7,9 @@ `EscaperRuntime` class. The following methods from ``Twig\\Extension\\EscaperExtension`` are - deprecated: ``setEscaper()``, ``getEscapers()``, ``setDefaultStrategy()``, - ``getDefaultStrategy()``, ``setSafeClasses``, ``addSafeClasses()``. Use the - same methods on the ``Twig\\Runtime\\EscaperRuntime`` class instead. + deprecated: ``setEscaper()``, ``getEscapers()``, ``setSafeClasses``, + ``addSafeClasses()``. Use the same methods on the + ``Twig\\Runtime\\EscaperRuntime`` class instead. # 3.9.3 (2024-04-18) diff --git a/doc/deprecated.rst b/doc/deprecated.rst index e770f3ff60..27ec642b20 100644 --- a/doc/deprecated.rst +++ b/doc/deprecated.rst @@ -22,9 +22,9 @@ Extensions ``$env->getRuntime(EscaperRuntime::class)->escape()`` instead. * The following methods from ``Twig\Extension\EscaperExtension`` are - deprecated: ``setEscaper()``, ``getEscapers()``, ``setDefaultStrategy()``, - ``getDefaultStrategy()``, ``setSafeClasses``, ``addSafeClasses()``. Use the - same methods on the ``Twig\Runtime\EscaperRuntime`` class instead. + deprecated: ``setEscaper()``, ``getEscapers()``, ``setSafeClasses``, + ``addSafeClasses()``. Use the same methods on the + ``Twig\Runtime\EscaperRuntime`` class instead: Before: $twig->getExtension(EscaperExtension::class)->METHOD() diff --git a/src/Environment.php b/src/Environment.php index bb2a257d12..2405e41fa4 100644 --- a/src/Environment.php +++ b/src/Environment.php @@ -131,11 +131,11 @@ public function __construct(LoaderInterface $loader, $options = []) $this->setCache($options['cache']); $this->extensionSet = new ExtensionSet(); $this->defaultRuntimeLoader = new FactoryRuntimeLoader([ - EscaperRuntime::class => function () use ($options) { return new EscaperRuntime($options['autoescape'], $this->charset); }, + EscaperRuntime::class => function () { return new EscaperRuntime($this->charset); }, ]); $this->addExtension(new CoreExtension()); - $this->addExtension(new EscaperExtension($this->getRuntime(EscaperRuntime::class))); + $this->addExtension(new EscaperExtension($this->getRuntime(EscaperRuntime::class), $options['autoescape'])); if (\PHP_VERSION_ID >= 80000) { $this->addExtension(new YieldNotReadyExtension($this->useYield)); } diff --git a/src/Extension/EscaperExtension.php b/src/Extension/EscaperExtension.php index 82cab28410..8343bfd399 100644 --- a/src/Extension/EscaperExtension.php +++ b/src/Extension/EscaperExtension.php @@ -12,6 +12,7 @@ namespace Twig\Extension; use Twig\Environment; +use Twig\FileExtensionEscapingStrategy; use Twig\Node\Expression\ConstantExpression; use Twig\Node\Node; use Twig\NodeVisitor\EscaperNodeVisitor; @@ -24,9 +25,16 @@ final class EscaperExtension extends AbstractExtension private $environment; private $escapers = []; private $escaper; + private $defaultStrategy; - public function __construct(EscaperRuntime $escaper) + /** + * @param string|false|callable $defaultStrategy An escaping strategy + * + * @see setDefaultStrategy() + */ + public function __construct(EscaperRuntime $escaper, $defaultStrategy = 'html') { + $this->setDefaultStrategy($defaultStrategy); $this->escaper = $escaper; } @@ -65,15 +73,15 @@ public function setEnvironment(Environment $environment): void * The strategy can be a valid PHP callback that takes the template * name as an argument and returns the strategy to use. * - * @param string|false|callable $defaultStrategy An escaping strategy - * - * @deprecated since Twig 3.10 + * @param string|false|callable(string $templateName): string $defaultStrategy An escaping strategy */ public function setDefaultStrategy($defaultStrategy): void { - trigger_deprecation('twig/twig', '3.10', 'The "%s()" method is deprecated, use the "Twig\Runtime\EscaperRuntime::setDefaultStrategy()" method instead.', __METHOD__); + if ('name' === $defaultStrategy) { + $defaultStrategy = [FileExtensionEscapingStrategy::class, 'guess']; + } - $this->escaper->setDefaultStrategy($defaultStrategy); + $this->defaultStrategy = $defaultStrategy; } /** @@ -82,14 +90,16 @@ public function setDefaultStrategy($defaultStrategy): void * @param string $name The template name * * @return string|false The default strategy to use for the template - * - * @deprecated since Twig 3.10 */ public function getDefaultStrategy(string $name) { - trigger_deprecation('twig/twig', '3.10', 'The "%s()" method is deprecated, use the "Twig\Runtime\EscaperRuntime::getDefaultStrategy()" method instead.', __METHOD__); + // disable string callables to avoid calling a function named html or js, + // or any other upcoming escaping strategy + if (!\is_string($this->defaultStrategy) && false !== $this->defaultStrategy) { + return \call_user_func($this->defaultStrategy, $name); + } - return $this->escaper->getDefaultStrategy($name); + return $this->defaultStrategy; } /** diff --git a/src/NodeVisitor/EscaperNodeVisitor.php b/src/NodeVisitor/EscaperNodeVisitor.php index fe9b3472ec..91e2ea8939 100644 --- a/src/NodeVisitor/EscaperNodeVisitor.php +++ b/src/NodeVisitor/EscaperNodeVisitor.php @@ -26,7 +26,6 @@ use Twig\Node\Node; use Twig\Node\PrintNode; use Twig\NodeTraverser; -use Twig\Runtime\EscaperRuntime; /** * @author Fabien Potencier @@ -50,7 +49,7 @@ public function __construct() public function enterNode(Node $node, Environment $env): Node { if ($node instanceof ModuleNode) { - if ($env->hasExtension(EscaperExtension::class) && $defaultStrategy = $env->getRuntime(EscaperRuntime::class)->getDefaultStrategy($node->getTemplateName())) { + if ($env->hasExtension(EscaperExtension::class) && $defaultStrategy = $env->getExtension(EscaperExtension::class)->getDefaultStrategy($node->getTemplateName())) { $this->defaultStrategy = $defaultStrategy; } $this->safeVars = []; diff --git a/src/Runtime/EscaperRuntime.php b/src/Runtime/EscaperRuntime.php index 0124bfe247..0e38ae195d 100644 --- a/src/Runtime/EscaperRuntime.php +++ b/src/Runtime/EscaperRuntime.php @@ -13,12 +13,10 @@ use Twig\Error\RuntimeError; use Twig\Extension\RuntimeExtensionInterface; -use Twig\FileExtensionEscapingStrategy; use Twig\Markup; final class EscaperRuntime implements RuntimeExtensionInterface { - private $defaultStrategy; private $escapers = []; /** @internal */ @@ -29,52 +27,11 @@ final class EscaperRuntime implements RuntimeExtensionInterface private $charset; - /** - * @param string|false|callable $defaultStrategy An escaping strategy - * - * @see setDefaultStrategy() - */ - public function __construct($defaultStrategy = 'html', $charset = 'UTF-8') + public function __construct($charset = 'UTF-8') { - $this->setDefaultStrategy($defaultStrategy); $this->charset = $charset; } - /** - * Sets the default strategy to use when not defined by the user. - * - * The strategy can be a valid PHP callback that takes the template - * name as an argument and returns the strategy to use. - * - * @param string|false|callable $defaultStrategy An escaping strategy - */ - public function setDefaultStrategy($defaultStrategy): void - { - if ('name' === $defaultStrategy) { - $defaultStrategy = [FileExtensionEscapingStrategy::class, 'guess']; - } - - $this->defaultStrategy = $defaultStrategy; - } - - /** - * Gets the default strategy to use when not defined by the user. - * - * @param string $name The template name - * - * @return string|false The default strategy to use for the template - */ - public function getDefaultStrategy(string $name) - { - // disable string callables to avoid calling a function named html or js, - // or any other upcoming escaping strategy - if (!\is_string($this->defaultStrategy) && false !== $this->defaultStrategy) { - return \call_user_func($this->defaultStrategy, $name); - } - - return $this->defaultStrategy; - } - /** * Defines a new escaper to be used via the escape filter. *