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

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