Move back default strategy to the extension

This commit is contained in:
Fabien Potencier
2024-05-04 08:48:54 +02:00
parent 68e4e5545f
commit 22810841ba
6 changed files with 30 additions and 64 deletions
+3 -3
View File
@@ -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)
+3 -3
View File
@@ -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()
+2 -2
View File
@@ -131,11 +131,11 @@ class Environment
$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));
}
+20 -10
View File
@@ -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 @@ final class EscaperExtension extends AbstractExtension
* 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 @@ final class EscaperExtension extends AbstractExtension
* @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;
}
/**
+1 -2
View File
@@ -26,7 +26,6 @@ use Twig\Node\ModuleNode;
use Twig\Node\Node;
use Twig\Node\PrintNode;
use Twig\NodeTraverser;
use Twig\Runtime\EscaperRuntime;
/**
* @author Fabien Potencier <fabien@symfony.com>
@@ -50,7 +49,7 @@ final class EscaperNodeVisitor implements NodeVisitorInterface
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 = [];
+1 -44
View File
@@ -13,12 +13,10 @@ namespace Twig\Runtime;
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.
*