vendor/twig/twig/src/Environment.php line 280

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\Cache\CacheInterface;
  12. use Twig\Cache\FilesystemCache;
  13. use Twig\Cache\NullCache;
  14. use Twig\Error\Error;
  15. use Twig\Error\LoaderError;
  16. use Twig\Error\RuntimeError;
  17. use Twig\Error\SyntaxError;
  18. use Twig\Extension\CoreExtension;
  19. use Twig\Extension\EscaperExtension;
  20. use Twig\Extension\ExtensionInterface;
  21. use Twig\Extension\OptimizerExtension;
  22. use Twig\Loader\ArrayLoader;
  23. use Twig\Loader\ChainLoader;
  24. use Twig\Loader\LoaderInterface;
  25. use Twig\Node\Expression\Binary\AbstractBinary;
  26. use Twig\Node\Expression\Unary\AbstractUnary;
  27. use Twig\Node\ModuleNode;
  28. use Twig\Node\Node;
  29. use Twig\NodeVisitor\NodeVisitorInterface;
  30. use Twig\RuntimeLoader\RuntimeLoaderInterface;
  31. use Twig\TokenParser\TokenParserInterface;
  32. /**
  33.  * Stores the Twig configuration and renders templates.
  34.  *
  35.  * @author Fabien Potencier <[email protected]>
  36.  */
  37. class Environment
  38. {
  39.     public const VERSION '3.8.0';
  40.     public const VERSION_ID 30800;
  41.     public const MAJOR_VERSION 3;
  42.     public const MINOR_VERSION 8;
  43.     public const RELEASE_VERSION 0;
  44.     public const EXTRA_VERSION '';
  45.     private $charset;
  46.     private $loader;
  47.     private $debug;
  48.     private $autoReload;
  49.     private $cache;
  50.     private $lexer;
  51.     private $parser;
  52.     private $compiler;
  53.     /** @var array<string, mixed> */
  54.     private $globals = [];
  55.     private $resolvedGlobals;
  56.     private $loadedTemplates;
  57.     private $strictVariables;
  58.     private $templateClassPrefix '__TwigTemplate_';
  59.     private $originalCache;
  60.     private $extensionSet;
  61.     private $runtimeLoaders = [];
  62.     private $runtimes = [];
  63.     private $optionsHash;
  64.     /**
  65.      * Constructor.
  66.      *
  67.      * Available options:
  68.      *
  69.      *  * debug: When set to true, it automatically set "auto_reload" to true as
  70.      *           well (default to false).
  71.      *
  72.      *  * charset: The charset used by the templates (default to UTF-8).
  73.      *
  74.      *  * cache: An absolute path where to store the compiled templates,
  75.      *           a \Twig\Cache\CacheInterface implementation,
  76.      *           or false to disable compilation cache (default).
  77.      *
  78.      *  * auto_reload: Whether to reload the template if the original source changed.
  79.      *                 If you don't provide the auto_reload option, it will be
  80.      *                 determined automatically based on the debug value.
  81.      *
  82.      *  * strict_variables: Whether to ignore invalid variables in templates
  83.      *                      (default to false).
  84.      *
  85.      *  * autoescape: Whether to enable auto-escaping (default to html):
  86.      *                  * false: disable auto-escaping
  87.      *                  * html, js: set the autoescaping to one of the supported strategies
  88.      *                  * name: set the autoescaping strategy based on the template name extension
  89.      *                  * PHP callback: a PHP callback that returns an escaping strategy based on the template "name"
  90.      *
  91.      *  * optimizations: A flag that indicates which optimizations to apply
  92.      *                   (default to -1 which means that all optimizations are enabled;
  93.      *                   set it to 0 to disable).
  94.      */
  95.     public function __construct(LoaderInterface $loader$options = [])
  96.     {
  97.         $this->setLoader($loader);
  98.         $options array_merge([
  99.             'debug' => false,
  100.             'charset' => 'UTF-8',
  101.             'strict_variables' => false,
  102.             'autoescape' => 'html',
  103.             'cache' => false,
  104.             'auto_reload' => null,
  105.             'optimizations' => -1,
  106.         ], $options);
  107.         $this->debug = (bool) $options['debug'];
  108.         $this->setCharset($options['charset'] ?? 'UTF-8');
  109.         $this->autoReload null === $options['auto_reload'] ? $this->debug : (bool) $options['auto_reload'];
  110.         $this->strictVariables = (bool) $options['strict_variables'];
  111.         $this->setCache($options['cache']);
  112.         $this->extensionSet = new ExtensionSet();
  113.         $this->addExtension(new CoreExtension());
  114.         $this->addExtension(new EscaperExtension($options['autoescape']));
  115.         $this->addExtension(new OptimizerExtension($options['optimizations']));
  116.     }
  117.     /**
  118.      * Enables debugging mode.
  119.      */
  120.     public function enableDebug()
  121.     {
  122.         $this->debug true;
  123.         $this->updateOptionsHash();
  124.     }
  125.     /**
  126.      * Disables debugging mode.
  127.      */
  128.     public function disableDebug()
  129.     {
  130.         $this->debug false;
  131.         $this->updateOptionsHash();
  132.     }
  133.     /**
  134.      * Checks if debug mode is enabled.
  135.      *
  136.      * @return bool true if debug mode is enabled, false otherwise
  137.      */
  138.     public function isDebug()
  139.     {
  140.         return $this->debug;
  141.     }
  142.     /**
  143.      * Enables the auto_reload option.
  144.      */
  145.     public function enableAutoReload()
  146.     {
  147.         $this->autoReload true;
  148.     }
  149.     /**
  150.      * Disables the auto_reload option.
  151.      */
  152.     public function disableAutoReload()
  153.     {
  154.         $this->autoReload false;
  155.     }
  156.     /**
  157.      * Checks if the auto_reload option is enabled.
  158.      *
  159.      * @return bool true if auto_reload is enabled, false otherwise
  160.      */
  161.     public function isAutoReload()
  162.     {
  163.         return $this->autoReload;
  164.     }
  165.     /**
  166.      * Enables the strict_variables option.
  167.      */
  168.     public function enableStrictVariables()
  169.     {
  170.         $this->strictVariables true;
  171.         $this->updateOptionsHash();
  172.     }
  173.     /**
  174.      * Disables the strict_variables option.
  175.      */
  176.     public function disableStrictVariables()
  177.     {
  178.         $this->strictVariables false;
  179.         $this->updateOptionsHash();
  180.     }
  181.     /**
  182.      * Checks if the strict_variables option is enabled.
  183.      *
  184.      * @return bool true if strict_variables is enabled, false otherwise
  185.      */
  186.     public function isStrictVariables()
  187.     {
  188.         return $this->strictVariables;
  189.     }
  190.     /**
  191.      * Gets the current cache implementation.
  192.      *
  193.      * @param bool $original Whether to return the original cache option or the real cache instance
  194.      *
  195.      * @return CacheInterface|string|false A Twig\Cache\CacheInterface implementation,
  196.      *                                     an absolute path to the compiled templates,
  197.      *                                     or false to disable cache
  198.      */
  199.     public function getCache($original true)
  200.     {
  201.         return $original $this->originalCache $this->cache;
  202.     }
  203.     /**
  204.      * Sets the current cache implementation.
  205.      *
  206.      * @param CacheInterface|string|false $cache A Twig\Cache\CacheInterface implementation,
  207.      *                                           an absolute path to the compiled templates,
  208.      *                                           or false to disable cache
  209.      */
  210.     public function setCache($cache)
  211.     {
  212.         if (\is_string($cache)) {
  213.             $this->originalCache $cache;
  214.             $this->cache = new FilesystemCache($cache$this->autoReload FilesystemCache::FORCE_BYTECODE_INVALIDATION 0);
  215.         } elseif (false === $cache) {
  216.             $this->originalCache $cache;
  217.             $this->cache = new NullCache();
  218.         } elseif ($cache instanceof CacheInterface) {
  219.             $this->originalCache $this->cache $cache;
  220.         } else {
  221.             throw new \LogicException('Cache can only be a string, false, or a \Twig\Cache\CacheInterface implementation.');
  222.         }
  223.     }
  224.     /**
  225.      * Gets the template class associated with the given string.
  226.      *
  227.      * The generated template class is based on the following parameters:
  228.      *
  229.      *  * The cache key for the given template;
  230.      *  * The currently enabled extensions;
  231.      *  * Whether the Twig C extension is available or not;
  232.      *  * PHP version;
  233.      *  * Twig version;
  234.      *  * Options with what environment was created.
  235.      *
  236.      * @param string   $name  The name for which to calculate the template class name
  237.      * @param int|null $index The index if it is an embedded template
  238.      *
  239.      * @internal
  240.      */
  241.     public function getTemplateClass(string $nameint $index null): string
  242.     {
  243.         $key $this->getLoader()->getCacheKey($name).$this->optionsHash;
  244.         return $this->templateClassPrefix.hash(\PHP_VERSION_ID 80100 'sha256' 'xxh128'$key).(null === $index '' '___'.$index);
  245.     }
  246.     /**
  247.      * Renders a template.
  248.      *
  249.      * @param string|TemplateWrapper $name The template name
  250.      *
  251.      * @throws LoaderError  When the template cannot be found
  252.      * @throws SyntaxError  When an error occurred during compilation
  253.      * @throws RuntimeError When an error occurred during rendering
  254.      */
  255.     public function render($name, array $context = []): string
  256.     {
  257.         return $this->load($name)->render($context);
  258.     }
  259.     /**
  260.      * Displays a template.
  261.      *
  262.      * @param string|TemplateWrapper $name The template name
  263.      *
  264.      * @throws LoaderError  When the template cannot be found
  265.      * @throws SyntaxError  When an error occurred during compilation
  266.      * @throws RuntimeError When an error occurred during rendering
  267.      */
  268.     public function display($name, array $context = []): void
  269.     {
  270.         $this->load($name)->display($context);
  271.     }
  272.     /**
  273.      * Loads a template.
  274.      *
  275.      * @param string|TemplateWrapper $name The template name
  276.      *
  277.      * @throws LoaderError  When the template cannot be found
  278.      * @throws RuntimeError When a previously generated cache is corrupted
  279.      * @throws SyntaxError  When an error occurred during compilation
  280.      */
  281.     public function load($name): TemplateWrapper
  282.     {
  283.         if ($name instanceof TemplateWrapper) {
  284.             return $name;
  285.         }
  286.         return new TemplateWrapper($this$this->loadTemplate($this->getTemplateClass($name), $name));
  287.     }
  288.     /**
  289.      * Loads a template internal representation.
  290.      *
  291.      * This method is for internal use only and should never be called
  292.      * directly.
  293.      *
  294.      * @param string $name  The template name
  295.      * @param int    $index The index if it is an embedded template
  296.      *
  297.      * @throws LoaderError  When the template cannot be found
  298.      * @throws RuntimeError When a previously generated cache is corrupted
  299.      * @throws SyntaxError  When an error occurred during compilation
  300.      *
  301.      * @internal
  302.      */
  303.     public function loadTemplate(string $clsstring $nameint $index null): Template
  304.     {
  305.         $mainCls $cls;
  306.         if (null !== $index) {
  307.             $cls .= '___'.$index;
  308.         }
  309.         if (isset($this->loadedTemplates[$cls])) {
  310.             return $this->loadedTemplates[$cls];
  311.         }
  312.         if (!class_exists($clsfalse)) {
  313.             $key $this->cache->generateKey($name$mainCls);
  314.             if (!$this->isAutoReload() || $this->isTemplateFresh($name$this->cache->getTimestamp($key))) {
  315.                 $this->cache->load($key);
  316.             }
  317.             if (!class_exists($clsfalse)) {
  318.                 $source $this->getLoader()->getSourceContext($name);
  319.                 $content $this->compileSource($source);
  320.                 $this->cache->write($key$content);
  321.                 $this->cache->load($key);
  322.                 if (!class_exists($mainClsfalse)) {
  323.                     /* Last line of defense if either $this->bcWriteCacheFile was used,
  324.                      * $this->cache is implemented as a no-op or we have a race condition
  325.                      * where the cache was cleared between the above calls to write to and load from
  326.                      * the cache.
  327.                      */
  328.                     eval('?>'.$content);
  329.                 }
  330.                 if (!class_exists($clsfalse)) {
  331.                     throw new RuntimeError(sprintf('Failed to load Twig template "%s", index "%s": cache might be corrupted.'$name$index), -1$source);
  332.                 }
  333.             }
  334.         }
  335.         $this->extensionSet->initRuntime();
  336.         return $this->loadedTemplates[$cls] = new $cls($this);
  337.     }
  338.     /**
  339.      * Creates a template from source.
  340.      *
  341.      * This method should not be used as a generic way to load templates.
  342.      *
  343.      * @param string $template The template source
  344.      * @param string $name     An optional name of the template to be used in error messages
  345.      *
  346.      * @throws LoaderError When the template cannot be found
  347.      * @throws SyntaxError When an error occurred during compilation
  348.      */
  349.     public function createTemplate(string $templatestring $name null): TemplateWrapper
  350.     {
  351.         $hash hash(\PHP_VERSION_ID 80100 'sha256' 'xxh128'$templatefalse);
  352.         if (null !== $name) {
  353.             $name sprintf('%s (string template %s)'$name$hash);
  354.         } else {
  355.             $name sprintf('__string_template__%s'$hash);
  356.         }
  357.         $loader = new ChainLoader([
  358.             new ArrayLoader([$name => $template]),
  359.             $current $this->getLoader(),
  360.         ]);
  361.         $this->setLoader($loader);
  362.         try {
  363.             return new TemplateWrapper($this$this->loadTemplate($this->getTemplateClass($name), $name));
  364.         } finally {
  365.             $this->setLoader($current);
  366.         }
  367.     }
  368.     /**
  369.      * Returns true if the template is still fresh.
  370.      *
  371.      * Besides checking the loader for freshness information,
  372.      * this method also checks if the enabled extensions have
  373.      * not changed.
  374.      *
  375.      * @param int $time The last modification time of the cached template
  376.      */
  377.     public function isTemplateFresh(string $nameint $time): bool
  378.     {
  379.         return $this->extensionSet->getLastModified() <= $time && $this->getLoader()->isFresh($name$time);
  380.     }
  381.     /**
  382.      * Tries to load a template consecutively from an array.
  383.      *
  384.      * Similar to load() but it also accepts instances of \Twig\Template and
  385.      * \Twig\TemplateWrapper, and an array of templates where each is tried to be loaded.
  386.      *
  387.      * @param string|TemplateWrapper|array $names A template or an array of templates to try consecutively
  388.      *
  389.      * @throws LoaderError When none of the templates can be found
  390.      * @throws SyntaxError When an error occurred during compilation
  391.      */
  392.     public function resolveTemplate($names): TemplateWrapper
  393.     {
  394.         if (!\is_array($names)) {
  395.             return $this->load($names);
  396.         }
  397.         $count \count($names);
  398.         foreach ($names as $name) {
  399.             if ($name instanceof Template) {
  400.                 return $name;
  401.             }
  402.             if ($name instanceof TemplateWrapper) {
  403.                 return $name;
  404.             }
  405.             if (!== $count && !$this->getLoader()->exists($name)) {
  406.                 continue;
  407.             }
  408.             return $this->load($name);
  409.         }
  410.         throw new LoaderError(sprintf('Unable to find one of the following templates: "%s".'implode('", "'$names)));
  411.     }
  412.     public function setLexer(Lexer $lexer)
  413.     {
  414.         $this->lexer $lexer;
  415.     }
  416.     /**
  417.      * @throws SyntaxError When the code is syntactically wrong
  418.      */
  419.     public function tokenize(Source $source): TokenStream
  420.     {
  421.         if (null === $this->lexer) {
  422.             $this->lexer = new Lexer($this);
  423.         }
  424.         return $this->lexer->tokenize($source);
  425.     }
  426.     public function setParser(Parser $parser)
  427.     {
  428.         $this->parser $parser;
  429.     }
  430.     /**
  431.      * Converts a token stream to a node tree.
  432.      *
  433.      * @throws SyntaxError When the token stream is syntactically or semantically wrong
  434.      */
  435.     public function parse(TokenStream $stream): ModuleNode
  436.     {
  437.         if (null === $this->parser) {
  438.             $this->parser = new Parser($this);
  439.         }
  440.         return $this->parser->parse($stream);
  441.     }
  442.     public function setCompiler(Compiler $compiler)
  443.     {
  444.         $this->compiler $compiler;
  445.     }
  446.     /**
  447.      * Compiles a node and returns the PHP code.
  448.      */
  449.     public function compile(Node $node): string
  450.     {
  451.         if (null === $this->compiler) {
  452.             $this->compiler = new Compiler($this);
  453.         }
  454.         return $this->compiler->compile($node)->getSource();
  455.     }
  456.     /**
  457.      * Compiles a template source code.
  458.      *
  459.      * @throws SyntaxError When there was an error during tokenizing, parsing or compiling
  460.      */
  461.     public function compileSource(Source $source): string
  462.     {
  463.         try {
  464.             return $this->compile($this->parse($this->tokenize($source)));
  465.         } catch (Error $e) {
  466.             $e->setSourceContext($source);
  467.             throw $e;
  468.         } catch (\Exception $e) {
  469.             throw new SyntaxError(sprintf('An exception has been thrown during the compilation of a template ("%s").'$e->getMessage()), -1$source$e);
  470.         }
  471.     }
  472.     public function setLoader(LoaderInterface $loader)
  473.     {
  474.         $this->loader $loader;
  475.     }
  476.     public function getLoader(): LoaderInterface
  477.     {
  478.         return $this->loader;
  479.     }
  480.     public function setCharset(string $charset)
  481.     {
  482.         if ('UTF8' === $charset null === $charset null strtoupper($charset)) {
  483.             // iconv on Windows requires "UTF-8" instead of "UTF8"
  484.             $charset 'UTF-8';
  485.         }
  486.         $this->charset $charset;
  487.     }
  488.     public function getCharset(): string
  489.     {
  490.         return $this->charset;
  491.     }
  492.     public function hasExtension(string $class): bool
  493.     {
  494.         return $this->extensionSet->hasExtension($class);
  495.     }
  496.     public function addRuntimeLoader(RuntimeLoaderInterface $loader)
  497.     {
  498.         $this->runtimeLoaders[] = $loader;
  499.     }
  500.     /**
  501.      * @template TExtension of ExtensionInterface
  502.      *
  503.      * @param class-string<TExtension> $class
  504.      *
  505.      * @return TExtension
  506.      */
  507.     public function getExtension(string $class): ExtensionInterface
  508.     {
  509.         return $this->extensionSet->getExtension($class);
  510.     }
  511.     /**
  512.      * Returns the runtime implementation of a Twig element (filter/function/tag/test).
  513.      *
  514.      * @template TRuntime of object
  515.      *
  516.      * @param class-string<TRuntime> $class A runtime class name
  517.      *
  518.      * @return TRuntime The runtime implementation
  519.      *
  520.      * @throws RuntimeError When the template cannot be found
  521.      */
  522.     public function getRuntime(string $class)
  523.     {
  524.         if (isset($this->runtimes[$class])) {
  525.             return $this->runtimes[$class];
  526.         }
  527.         foreach ($this->runtimeLoaders as $loader) {
  528.             if (null !== $runtime $loader->load($class)) {
  529.                 return $this->runtimes[$class] = $runtime;
  530.             }
  531.         }
  532.         throw new RuntimeError(sprintf('Unable to load the "%s" runtime.'$class));
  533.     }
  534.     public function addExtension(ExtensionInterface $extension)
  535.     {
  536.         $this->extensionSet->addExtension($extension);
  537.         $this->updateOptionsHash();
  538.     }
  539.     /**
  540.      * @param ExtensionInterface[] $extensions An array of extensions
  541.      */
  542.     public function setExtensions(array $extensions)
  543.     {
  544.         $this->extensionSet->setExtensions($extensions);
  545.         $this->updateOptionsHash();
  546.     }
  547.     /**
  548.      * @return ExtensionInterface[] An array of extensions (keys are for internal usage only and should not be relied on)
  549.      */
  550.     public function getExtensions(): array
  551.     {
  552.         return $this->extensionSet->getExtensions();
  553.     }
  554.     public function addTokenParser(TokenParserInterface $parser)
  555.     {
  556.         $this->extensionSet->addTokenParser($parser);
  557.     }
  558.     /**
  559.      * @return TokenParserInterface[]
  560.      *
  561.      * @internal
  562.      */
  563.     public function getTokenParsers(): array
  564.     {
  565.         return $this->extensionSet->getTokenParsers();
  566.     }
  567.     /**
  568.      * @internal
  569.      */
  570.     public function getTokenParser(string $name): ?TokenParserInterface
  571.     {
  572.         return $this->extensionSet->getTokenParser($name);
  573.     }
  574.     public function registerUndefinedTokenParserCallback(callable $callable): void
  575.     {
  576.         $this->extensionSet->registerUndefinedTokenParserCallback($callable);
  577.     }
  578.     public function addNodeVisitor(NodeVisitorInterface $visitor)
  579.     {
  580.         $this->extensionSet->addNodeVisitor($visitor);
  581.     }
  582.     /**
  583.      * @return NodeVisitorInterface[]
  584.      *
  585.      * @internal
  586.      */
  587.     public function getNodeVisitors(): array
  588.     {
  589.         return $this->extensionSet->getNodeVisitors();
  590.     }
  591.     public function addFilter(TwigFilter $filter)
  592.     {
  593.         $this->extensionSet->addFilter($filter);
  594.     }
  595.     /**
  596.      * @internal
  597.      */
  598.     public function getFilter(string $name): ?TwigFilter
  599.     {
  600.         return $this->extensionSet->getFilter($name);
  601.     }
  602.     public function registerUndefinedFilterCallback(callable $callable): void
  603.     {
  604.         $this->extensionSet->registerUndefinedFilterCallback($callable);
  605.     }
  606.     /**
  607.      * Gets the registered Filters.
  608.      *
  609.      * Be warned that this method cannot return filters defined with registerUndefinedFilterCallback.
  610.      *
  611.      * @return TwigFilter[]
  612.      *
  613.      * @see registerUndefinedFilterCallback
  614.      *
  615.      * @internal
  616.      */
  617.     public function getFilters(): array
  618.     {
  619.         return $this->extensionSet->getFilters();
  620.     }
  621.     public function addTest(TwigTest $test)
  622.     {
  623.         $this->extensionSet->addTest($test);
  624.     }
  625.     /**
  626.      * @return TwigTest[]
  627.      *
  628.      * @internal
  629.      */
  630.     public function getTests(): array
  631.     {
  632.         return $this->extensionSet->getTests();
  633.     }
  634.     /**
  635.      * @internal
  636.      */
  637.     public function getTest(string $name): ?TwigTest
  638.     {
  639.         return $this->extensionSet->getTest($name);
  640.     }
  641.     public function addFunction(TwigFunction $function)
  642.     {
  643.         $this->extensionSet->addFunction($function);
  644.     }
  645.     /**
  646.      * @internal
  647.      */
  648.     public function getFunction(string $name): ?TwigFunction
  649.     {
  650.         return $this->extensionSet->getFunction($name);
  651.     }
  652.     public function registerUndefinedFunctionCallback(callable $callable): void
  653.     {
  654.         $this->extensionSet->registerUndefinedFunctionCallback($callable);
  655.     }
  656.     /**
  657.      * Gets registered functions.
  658.      *
  659.      * Be warned that this method cannot return functions defined with registerUndefinedFunctionCallback.
  660.      *
  661.      * @return TwigFunction[]
  662.      *
  663.      * @see registerUndefinedFunctionCallback
  664.      *
  665.      * @internal
  666.      */
  667.     public function getFunctions(): array
  668.     {
  669.         return $this->extensionSet->getFunctions();
  670.     }
  671.     /**
  672.      * Registers a Global.
  673.      *
  674.      * New globals can be added before compiling or rendering a template;
  675.      * but after, you can only update existing globals.
  676.      *
  677.      * @param mixed $value The global value
  678.      */
  679.     public function addGlobal(string $name$value)
  680.     {
  681.         if ($this->extensionSet->isInitialized() && !\array_key_exists($name$this->getGlobals())) {
  682.             throw new \LogicException(sprintf('Unable to add global "%s" as the runtime or the extensions have already been initialized.'$name));
  683.         }
  684.         if (null !== $this->resolvedGlobals) {
  685.             $this->resolvedGlobals[$name] = $value;
  686.         } else {
  687.             $this->globals[$name] = $value;
  688.         }
  689.     }
  690.     /**
  691.      * @internal
  692.      *
  693.      * @return array<string, mixed>
  694.      */
  695.     public function getGlobals(): array
  696.     {
  697.         if ($this->extensionSet->isInitialized()) {
  698.             if (null === $this->resolvedGlobals) {
  699.                 $this->resolvedGlobals array_merge($this->extensionSet->getGlobals(), $this->globals);
  700.             }
  701.             return $this->resolvedGlobals;
  702.         }
  703.         return array_merge($this->extensionSet->getGlobals(), $this->globals);
  704.     }
  705.     public function mergeGlobals(array $context): array
  706.     {
  707.         // we don't use array_merge as the context being generally
  708.         // bigger than globals, this code is faster.
  709.         foreach ($this->getGlobals() as $key => $value) {
  710.             if (!\array_key_exists($key$context)) {
  711.                 $context[$key] = $value;
  712.             }
  713.         }
  714.         return $context;
  715.     }
  716.     /**
  717.      * @internal
  718.      *
  719.      * @return array<string, array{precedence: int, class: class-string<AbstractUnary>}>
  720.      */
  721.     public function getUnaryOperators(): array
  722.     {
  723.         return $this->extensionSet->getUnaryOperators();
  724.     }
  725.     /**
  726.      * @internal
  727.      *
  728.      * @return array<string, array{precedence: int, class: class-string<AbstractBinary>, associativity: ExpressionParser::OPERATOR_*}>
  729.      */
  730.     public function getBinaryOperators(): array
  731.     {
  732.         return $this->extensionSet->getBinaryOperators();
  733.     }
  734.     private function updateOptionsHash(): void
  735.     {
  736.         $this->optionsHash implode(':', [
  737.             $this->extensionSet->getSignature(),
  738.             \PHP_MAJOR_VERSION,
  739.             \PHP_MINOR_VERSION,
  740.             self::VERSION,
  741.             (int) $this->debug,
  742.             (int) $this->strictVariables,
  743.         ]);
  744.     }
  745. }