Skip to content

Commit

Permalink
feature #4072 Move back default strategy to the extension (fabpot)
Browse files Browse the repository at this point in the history
This PR was merged into the 3.x branch.

Discussion
----------

Move back default strategy to the extension

Commits
-------

2281084 Move back default strategy to the extension
  • Loading branch information
fabpot committed May 4, 2024
2 parents 7ccf825 + 2281084 commit 450d2fb
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 64 deletions.
6 changes: 3 additions & 3 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
6 changes: 3 additions & 3 deletions doc/deprecated.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions src/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
30 changes: 20 additions & 10 deletions src/Extension/EscaperExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -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;
}

/**
Expand Down
3 changes: 1 addition & 2 deletions src/NodeVisitor/EscaperNodeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
use Twig\Node\Node;
use Twig\Node\PrintNode;
use Twig\NodeTraverser;
use Twig\Runtime\EscaperRuntime;

/**
* @author Fabien Potencier <fabien@symfony.com>
Expand All @@ -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 = [];
Expand Down
45 changes: 1 addition & 44 deletions src/Runtime/EscaperRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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.
*
Expand Down

0 comments on commit 450d2fb

Please sign in to comment.