vendor/twig/twig/src/ExtensionSet.php line 65

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;
  11. use Twig\Error\RuntimeError;
  12. use Twig\Extension\ExtensionInterface;
  13. use Twig\Extension\GlobalsInterface;
  14. use Twig\Extension\InitRuntimeInterface;
  15. use Twig\Extension\StagingExtension;
  16. use Twig\NodeVisitor\NodeVisitorInterface;
  17. use Twig\TokenParser\TokenParserInterface;
  18. /**
  19.  * @author Fabien Potencier <fabien@symfony.com>
  20.  *
  21.  * @internal
  22.  */
  23. final class ExtensionSet
  24. {
  25.     private $extensions;
  26.     private $initialized false;
  27.     private $runtimeInitialized false;
  28.     private $staging;
  29.     private $parsers;
  30.     private $visitors;
  31.     private $filters;
  32.     private $tests;
  33.     private $functions;
  34.     private $unaryOperators;
  35.     private $binaryOperators;
  36.     private $globals;
  37.     private $functionCallbacks = [];
  38.     private $filterCallbacks = [];
  39.     private $lastModified 0;
  40.     public function __construct()
  41.     {
  42.         $this->staging = new StagingExtension();
  43.     }
  44.     /**
  45.      * Initializes the runtime environment.
  46.      *
  47.      * @deprecated since Twig 2.7
  48.      */
  49.     public function initRuntime(Environment $env)
  50.     {
  51.         if ($this->runtimeInitialized) {
  52.             return;
  53.         }
  54.         $this->runtimeInitialized true;
  55.         foreach ($this->extensions as $extension) {
  56.             if ($extension instanceof InitRuntimeInterface) {
  57.                 $extension->initRuntime($env);
  58.             }
  59.         }
  60.     }
  61.     public function hasExtension(string $class): bool
  62.     {
  63.         $class ltrim($class'\\');
  64.         if (!isset($this->extensions[$class]) && class_exists($classfalse)) {
  65.             // For BC/FC with namespaced aliases
  66.             $class = (new \ReflectionClass($class))->name;
  67.         }
  68.         return isset($this->extensions[$class]);
  69.     }
  70.     public function getExtension(string $class): ExtensionInterface
  71.     {
  72.         $class ltrim($class'\\');
  73.         if (!isset($this->extensions[$class]) && class_exists($classfalse)) {
  74.             // For BC/FC with namespaced aliases
  75.             $class = (new \ReflectionClass($class))->name;
  76.         }
  77.         if (!isset($this->extensions[$class])) {
  78.             throw new RuntimeError(sprintf('The "%s" extension is not enabled.'$class));
  79.         }
  80.         return $this->extensions[$class];
  81.     }
  82.     public function setExtensions(array $extensions)
  83.     {
  84.         foreach ($extensions as $extension) {
  85.             $this->addExtension($extension);
  86.         }
  87.     }
  88.     /**
  89.      * @param ExtensionInterface[] $extensions
  90.      */
  91.     public function getExtensions(): array
  92.     {
  93.         return $this->extensions;
  94.     }
  95.     public function getSignature(): string
  96.     {
  97.         return json_encode(array_keys($this->extensions));
  98.     }
  99.     public function isInitialized(): bool
  100.     {
  101.         return $this->initialized || $this->runtimeInitialized;
  102.     }
  103.     public function getLastModified(): int
  104.     {
  105.         if (!== $this->lastModified) {
  106.             return $this->lastModified;
  107.         }
  108.         foreach ($this->extensions as $extension) {
  109.             $r = new \ReflectionObject($extension);
  110.             if (file_exists($r->getFileName()) && ($extensionTime filemtime($r->getFileName())) > $this->lastModified) {
  111.                 $this->lastModified $extensionTime;
  112.             }
  113.         }
  114.         return $this->lastModified;
  115.     }
  116.     public function addExtension(ExtensionInterface $extension)
  117.     {
  118.         $class = \get_class($extension);
  119.         if ($this->initialized) {
  120.             throw new \LogicException(sprintf('Unable to register extension "%s" as extensions have already been initialized.'$class));
  121.         }
  122.         if (isset($this->extensions[$class])) {
  123.             throw new \LogicException(sprintf('Unable to register extension "%s" as it is already registered.'$class));
  124.         }
  125.         // For BC/FC with namespaced aliases
  126.         $class = (new \ReflectionClass($class))->name;
  127.         $this->extensions[$class] = $extension;
  128.     }
  129.     public function addFunction(TwigFunction $function)
  130.     {
  131.         if ($this->initialized) {
  132.             throw new \LogicException(sprintf('Unable to add function "%s" as extensions have already been initialized.'$function->getName()));
  133.         }
  134.         $this->staging->addFunction($function);
  135.     }
  136.     /**
  137.      * @return TwigFunction[]
  138.      */
  139.     public function getFunctions(): array
  140.     {
  141.         if (!$this->initialized) {
  142.             $this->initExtensions();
  143.         }
  144.         return $this->functions;
  145.     }
  146.     /**
  147.      * @return TwigFunction|false
  148.      */
  149.     public function getFunction(string $name)
  150.     {
  151.         if (!$this->initialized) {
  152.             $this->initExtensions();
  153.         }
  154.         if (isset($this->functions[$name])) {
  155.             return $this->functions[$name];
  156.         }
  157.         foreach ($this->functions as $pattern => $function) {
  158.             $pattern str_replace('\\*''(.*?)'preg_quote($pattern'#'), $count);
  159.             if ($count && preg_match('#^'.$pattern.'$#'$name$matches)) {
  160.                 array_shift($matches);
  161.                 $function->setArguments($matches);
  162.                 return $function;
  163.             }
  164.         }
  165.         foreach ($this->functionCallbacks as $callback) {
  166.             if (false !== $function $callback($name)) {
  167.                 return $function;
  168.             }
  169.         }
  170.         return false;
  171.     }
  172.     public function registerUndefinedFunctionCallback(callable $callable)
  173.     {
  174.         $this->functionCallbacks[] = $callable;
  175.     }
  176.     public function addFilter(TwigFilter $filter)
  177.     {
  178.         if ($this->initialized) {
  179.             throw new \LogicException(sprintf('Unable to add filter "%s" as extensions have already been initialized.'$filter->getName()));
  180.         }
  181.         $this->staging->addFilter($filter);
  182.     }
  183.     /**
  184.      * @return TwigFilter[]
  185.      */
  186.     public function getFilters(): array
  187.     {
  188.         if (!$this->initialized) {
  189.             $this->initExtensions();
  190.         }
  191.         return $this->filters;
  192.     }
  193.     /**
  194.      * @return TwigFilter|false
  195.      */
  196.     public function getFilter(string $name)
  197.     {
  198.         if (!$this->initialized) {
  199.             $this->initExtensions();
  200.         }
  201.         if (isset($this->filters[$name])) {
  202.             return $this->filters[$name];
  203.         }
  204.         foreach ($this->filters as $pattern => $filter) {
  205.             $pattern str_replace('\\*''(.*?)'preg_quote($pattern'#'), $count);
  206.             if ($count && preg_match('#^'.$pattern.'$#'$name$matches)) {
  207.                 array_shift($matches);
  208.                 $filter->setArguments($matches);
  209.                 return $filter;
  210.             }
  211.         }
  212.         foreach ($this->filterCallbacks as $callback) {
  213.             if (false !== $filter $callback($name)) {
  214.                 return $filter;
  215.             }
  216.         }
  217.         return false;
  218.     }
  219.     public function registerUndefinedFilterCallback(callable $callable)
  220.     {
  221.         $this->filterCallbacks[] = $callable;
  222.     }
  223.     public function addNodeVisitor(NodeVisitorInterface $visitor)
  224.     {
  225.         if ($this->initialized) {
  226.             throw new \LogicException('Unable to add a node visitor as extensions have already been initialized.');
  227.         }
  228.         $this->staging->addNodeVisitor($visitor);
  229.     }
  230.     /**
  231.      * @return NodeVisitorInterface[]
  232.      */
  233.     public function getNodeVisitors(): array
  234.     {
  235.         if (!$this->initialized) {
  236.             $this->initExtensions();
  237.         }
  238.         return $this->visitors;
  239.     }
  240.     public function addTokenParser(TokenParserInterface $parser)
  241.     {
  242.         if ($this->initialized) {
  243.             throw new \LogicException('Unable to add a token parser as extensions have already been initialized.');
  244.         }
  245.         $this->staging->addTokenParser($parser);
  246.     }
  247.     /**
  248.      * @return TokenParserInterface[]
  249.      */
  250.     public function getTokenParsers(): array
  251.     {
  252.         if (!$this->initialized) {
  253.             $this->initExtensions();
  254.         }
  255.         return $this->parsers;
  256.     }
  257.     public function getGlobals(): array
  258.     {
  259.         if (null !== $this->globals) {
  260.             return $this->globals;
  261.         }
  262.         $globals = [];
  263.         foreach ($this->extensions as $extension) {
  264.             if (!$extension instanceof GlobalsInterface) {
  265.                 continue;
  266.             }
  267.             $extGlobals $extension->getGlobals();
  268.             if (!\is_array($extGlobals)) {
  269.                 throw new \UnexpectedValueException(sprintf('"%s::getGlobals()" must return an array of globals.', \get_class($extension)));
  270.             }
  271.             $globals array_merge($globals$extGlobals);
  272.         }
  273.         if ($this->initialized) {
  274.             $this->globals $globals;
  275.         }
  276.         return $globals;
  277.     }
  278.     public function addTest(TwigTest $test)
  279.     {
  280.         if ($this->initialized) {
  281.             throw new \LogicException(sprintf('Unable to add test "%s" as extensions have already been initialized.'$test->getName()));
  282.         }
  283.         $this->staging->addTest($test);
  284.     }
  285.     /**
  286.      * @return TwigTest[]
  287.      */
  288.     public function getTests(): array
  289.     {
  290.         if (!$this->initialized) {
  291.             $this->initExtensions();
  292.         }
  293.         return $this->tests;
  294.     }
  295.     /**
  296.      * @return TwigTest|false
  297.      */
  298.     public function getTest(string $name)
  299.     {
  300.         if (!$this->initialized) {
  301.             $this->initExtensions();
  302.         }
  303.         if (isset($this->tests[$name])) {
  304.             return $this->tests[$name];
  305.         }
  306.         foreach ($this->tests as $pattern => $test) {
  307.             $pattern str_replace('\\*''(.*?)'preg_quote($pattern'#'), $count);
  308.             if ($count) {
  309.                 if (preg_match('#^'.$pattern.'$#'$name$matches)) {
  310.                     array_shift($matches);
  311.                     $test->setArguments($matches);
  312.                     return $test;
  313.                 }
  314.             }
  315.         }
  316.         return false;
  317.     }
  318.     public function getUnaryOperators(): array
  319.     {
  320.         if (!$this->initialized) {
  321.             $this->initExtensions();
  322.         }
  323.         return $this->unaryOperators;
  324.     }
  325.     public function getBinaryOperators(): array
  326.     {
  327.         if (!$this->initialized) {
  328.             $this->initExtensions();
  329.         }
  330.         return $this->binaryOperators;
  331.     }
  332.     private function initExtensions()
  333.     {
  334.         $this->parsers = [];
  335.         $this->filters = [];
  336.         $this->functions = [];
  337.         $this->tests = [];
  338.         $this->visitors = [];
  339.         $this->unaryOperators = [];
  340.         $this->binaryOperators = [];
  341.         foreach ($this->extensions as $extension) {
  342.             $this->initExtension($extension);
  343.         }
  344.         $this->initExtension($this->staging);
  345.         // Done at the end only, so that an exception during initialization does not mark the environment as initialized when catching the exception
  346.         $this->initialized true;
  347.     }
  348.     private function initExtension(ExtensionInterface $extension)
  349.     {
  350.         // filters
  351.         foreach ($extension->getFilters() as $filter) {
  352.             $this->filters[$filter->getName()] = $filter;
  353.         }
  354.         // functions
  355.         foreach ($extension->getFunctions() as $function) {
  356.             $this->functions[$function->getName()] = $function;
  357.         }
  358.         // tests
  359.         foreach ($extension->getTests() as $test) {
  360.             $this->tests[$test->getName()] = $test;
  361.         }
  362.         // token parsers
  363.         foreach ($extension->getTokenParsers() as $parser) {
  364.             if (!$parser instanceof TokenParserInterface) {
  365.                 throw new \LogicException('getTokenParsers() must return an array of \Twig\TokenParser\TokenParserInterface.');
  366.             }
  367.             $this->parsers[] = $parser;
  368.         }
  369.         // node visitors
  370.         foreach ($extension->getNodeVisitors() as $visitor) {
  371.             $this->visitors[] = $visitor;
  372.         }
  373.         // operators
  374.         if ($operators $extension->getOperators()) {
  375.             if (!\is_array($operators)) {
  376.                 throw new \InvalidArgumentException(sprintf('"%s::getOperators()" must return an array with operators, got "%s".', \get_class($extension), \is_object($operators) ? \get_class($operators) : \gettype($operators).(\is_resource($operators) ? '' '#'.$operators)));
  377.             }
  378.             if (!== \count($operators)) {
  379.                 throw new \InvalidArgumentException(sprintf('"%s::getOperators()" must return an array of 2 elements, got %d.', \get_class($extension), \count($operators)));
  380.             }
  381.             $this->unaryOperators array_merge($this->unaryOperators$operators[0]);
  382.             $this->binaryOperators array_merge($this->binaryOperators$operators[1]);
  383.         }
  384.     }
  385. }
  386. class_alias('Twig\ExtensionSet''Twig_ExtensionSet');