vendor/twig/twig/src/Extension/EscaperExtension.php line 127

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of Twig.
  4.  *
  5.  * (c) Fabien Potencier
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Twig\Extension;
  11. use Twig\Environment;
  12. use Twig\FileExtensionEscapingStrategy;
  13. use Twig\Node\Expression\ConstantExpression;
  14. use Twig\Node\Expression\Filter\RawFilter;
  15. use Twig\Node\Node;
  16. use Twig\NodeVisitor\EscaperNodeVisitor;
  17. use Twig\Runtime\EscaperRuntime;
  18. use Twig\TokenParser\AutoEscapeTokenParser;
  19. use Twig\TwigFilter;
  20. final class EscaperExtension extends AbstractExtension
  21. {
  22.     private $environment;
  23.     private $escapers = [];
  24.     private $escaper;
  25.     private $defaultStrategy;
  26.     /**
  27.      * @param string|false|callable $defaultStrategy An escaping strategy
  28.      *
  29.      * @see setDefaultStrategy()
  30.      */
  31.     public function __construct($defaultStrategy 'html')
  32.     {
  33.         $this->setDefaultStrategy($defaultStrategy);
  34.     }
  35.     public function getTokenParsers(): array
  36.     {
  37.         return [new AutoEscapeTokenParser()];
  38.     }
  39.     public function getNodeVisitors(): array
  40.     {
  41.         return [new EscaperNodeVisitor()];
  42.     }
  43.     public function getFilters(): array
  44.     {
  45.         return [
  46.             new TwigFilter('escape', [EscaperRuntime::class, 'escape'], ['is_safe_callback' => [self::class, 'escapeFilterIsSafe']]),
  47.             new TwigFilter('e', [EscaperRuntime::class, 'escape'], ['is_safe_callback' => [self::class, 'escapeFilterIsSafe']]),
  48.             new TwigFilter('raw'null, ['is_safe' => ['all'], 'node_class' => RawFilter::class]),
  49.         ];
  50.     }
  51.     /**
  52.      * @deprecated since Twig 3.10
  53.      */
  54.     public function setEnvironment(Environment $environment): void
  55.     {
  56.         $triggerDeprecation \func_num_args() > func_get_arg(1) : true;
  57.         if ($triggerDeprecation) {
  58.             trigger_deprecation('twig/twig''3.10''The "%s()" method is deprecated and not needed if you are using methods from "Twig\Runtime\EscaperRuntime".'__METHOD__);
  59.         }
  60.         $this->environment $environment;
  61.         $this->escaper $environment->getRuntime(EscaperRuntime::class);
  62.     }
  63.     /**
  64.      * @deprecated since Twig 3.10
  65.      */
  66.     public function setEscaperRuntime(EscaperRuntime $escaper)
  67.     {
  68.         trigger_deprecation('twig/twig''3.10''The "%s()" method is deprecated and not needed if you are using methods from "Twig\Runtime\EscaperRuntime".'__METHOD__);
  69.         $this->escaper $escaper;
  70.     }
  71.     /**
  72.      * Sets the default strategy to use when not defined by the user.
  73.      *
  74.      * The strategy can be a valid PHP callback that takes the template
  75.      * name as an argument and returns the strategy to use.
  76.      *
  77.      * @param string|false|callable(string $templateName): string $defaultStrategy An escaping strategy
  78.      */
  79.     public function setDefaultStrategy($defaultStrategy): void
  80.     {
  81.         if ('name' === $defaultStrategy) {
  82.             $defaultStrategy = [FileExtensionEscapingStrategy::class, 'guess'];
  83.         }
  84.         $this->defaultStrategy $defaultStrategy;
  85.     }
  86.     /**
  87.      * Gets the default strategy to use when not defined by the user.
  88.      *
  89.      * @param string $name The template name
  90.      *
  91.      * @return string|false The default strategy to use for the template
  92.      */
  93.     public function getDefaultStrategy(string $name)
  94.     {
  95.         // disable string callables to avoid calling a function named html or js,
  96.         // or any other upcoming escaping strategy
  97.         if (!\is_string($this->defaultStrategy) && false !== $this->defaultStrategy) {
  98.             return \call_user_func($this->defaultStrategy$name);
  99.         }
  100.         return $this->defaultStrategy;
  101.     }
  102.     /**
  103.      * Defines a new escaper to be used via the escape filter.
  104.      *
  105.      * @param string                                        $strategy The strategy name that should be used as a strategy in the escape call
  106.      * @param callable(Environment, string, string): string $callable A valid PHP callable
  107.      *
  108.      * @deprecated since Twig 3.10
  109.      */
  110.     public function setEscaper($strategy, callable $callable)
  111.     {
  112.         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__);
  113.         if (!isset($this->environment)) {
  114.             throw new \LogicException(\sprintf('You must call "setEnvironment()" before calling "%s()".'__METHOD__));
  115.         }
  116.         $this->escapers[$strategy] = $callable;
  117.         $callable = function ($string$charset) use ($callable) {
  118.             return $callable($this->environment$string$charset);
  119.         };
  120.         $this->escaper->setEscaper($strategy$callable);
  121.     }
  122.     /**
  123.      * Gets all defined escapers.
  124.      *
  125.      * @return array<string, callable(Environment, string, string): string> An array of escapers
  126.      *
  127.      * @deprecated since Twig 3.10
  128.      */
  129.     public function getEscapers()
  130.     {
  131.         trigger_deprecation('twig/twig''3.10''The "%s()" method is deprecated, use the "Twig\Runtime\EscaperRuntime::getEscaper()" method instead.'__METHOD__);
  132.         return $this->escapers;
  133.     }
  134.     /**
  135.      * @deprecated since Twig 3.10
  136.      */
  137.     public function setSafeClasses(array $safeClasses = [])
  138.     {
  139.         trigger_deprecation('twig/twig''3.10''The "%s()" method is deprecated, use the "Twig\Runtime\EscaperRuntime::setSafeClasses()" method instead.'__METHOD__);
  140.         if (!isset($this->escaper)) {
  141.             throw new \LogicException(\sprintf('You must call "setEnvironment()" before calling "%s()".'__METHOD__));
  142.         }
  143.         $this->escaper->setSafeClasses($safeClasses);
  144.     }
  145.     /**
  146.      * @deprecated since Twig 3.10
  147.      */
  148.     public function addSafeClass(string $class, array $strategies)
  149.     {
  150.         trigger_deprecation('twig/twig''3.10''The "%s()" method is deprecated, use the "Twig\Runtime\EscaperRuntime::addSafeClass()" method instead.'__METHOD__);
  151.         if (!isset($this->escaper)) {
  152.             throw new \LogicException(\sprintf('You must call "setEnvironment()" before calling "%s()".'__METHOD__));
  153.         }
  154.         $this->escaper->addSafeClass($class$strategies);
  155.     }
  156.     /**
  157.      * @internal
  158.      */
  159.     public static function escapeFilterIsSafe(Node $filterArgs)
  160.     {
  161.         foreach ($filterArgs as $arg) {
  162.             if ($arg instanceof ConstantExpression) {
  163.                 return [$arg->getAttribute('value')];
  164.             }
  165.             return [];
  166.         }
  167.         return ['html'];
  168.     }
  169. }