: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
* This file is part of Twig.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
namespace WPML\Core\Twig;
use WPML\Core\Twig\Cache\CacheInterface;
use WPML\Core\Twig\Cache\FilesystemCache;
use WPML\Core\Twig\Cache\NullCache;
use WPML\Core\Twig\Error\Error;
use WPML\Core\Twig\Error\LoaderError;
use WPML\Core\Twig\Error\RuntimeError;
use WPML\Core\Twig\Error\SyntaxError;
use WPML\Core\Twig\Extension\CoreExtension;
use WPML\Core\Twig\Extension\EscaperExtension;
use WPML\Core\Twig\Extension\ExtensionInterface;
use WPML\Core\Twig\Extension\GlobalsInterface;
use WPML\Core\Twig\Extension\InitRuntimeInterface;
use WPML\Core\Twig\Extension\OptimizerExtension;
use WPML\Core\Twig\Extension\StagingExtension;
use WPML\Core\Twig\Loader\ArrayLoader;
use WPML\Core\Twig\Loader\ChainLoader;
use WPML\Core\Twig\Loader\LoaderInterface;
use WPML\Core\Twig\Loader\SourceContextLoaderInterface;
use WPML\Core\Twig\Node\ModuleNode;
use WPML\Core\Twig\NodeVisitor\NodeVisitorInterface;
use WPML\Core\Twig\RuntimeLoader\RuntimeLoaderInterface;
use WPML\Core\Twig\TokenParser\TokenParserInterface;
* Stores the Twig configuration.
* @author Fabien Potencier <fabien@symfony.com>
const VERSION = '1.42.4';
const VERSION_ID = 14204;
const MINOR_VERSION = 42;
const RELEASE_VERSION = 4;
const EXTRA_VERSION = '';
protected $baseTemplateClass;
protected $runtimeInitialized = \false;
protected $extensionInitialized = \false;
protected $loadedTemplates;
protected $strictVariables;
protected $unaryOperators;
protected $binaryOperators;
protected $templateClassPrefix = '\\WPML\\Core\\__TwigTemplate_';
protected $functionCallbacks = [];
protected $filterCallbacks = [];
private $bcWriteCacheFile = \false;
private $bcGetCacheFilename = \false;
private $lastModifiedExtension = 0;
private $extensionsByClass = [];
private $runtimeLoaders = [];
* * debug: When set to true, it automatically set "auto_reload" to true as
* well (default to false).
* * charset: The charset used by the templates (default to UTF-8).
* * base_template_class: The base template class to use for generated
* templates (default to \Twig\Template).
* * cache: An absolute path where to store the compiled templates,
* a \Twig\Cache\CacheInterface implementation,
* or false to disable compilation cache (default).
* * auto_reload: Whether to reload the template if the original source changed.
* If you don't provide the auto_reload option, it will be
* determined automatically based on the debug value.
* * strict_variables: Whether to ignore invalid variables in templates
* * autoescape: Whether to enable auto-escaping (default to html):
* * false: disable auto-escaping
* * true: equivalent to html
* * html, js: set the autoescaping to one of the supported strategies
* * name: set the autoescaping strategy based on the template name extension
* * PHP callback: a PHP callback that returns an escaping strategy based on the template "name"
* * optimizations: A flag that indicates which optimizations to apply
* (default to -1 which means that all optimizations are enabled;
* set it to 0 to disable).
public function __construct(\WPML\Core\Twig\Loader\LoaderInterface $loader = null, $options = [])
$this->setLoader($loader);
@\trigger_error('Not passing a "Twig\\Lodaer\\LoaderInterface" as the first constructor argument of "Twig\\Environment" is deprecated since version 1.21.', \E_USER_DEPRECATED);
$options = \array_merge(['debug' => \false, 'charset' => 'UTF-8', 'base_template_class' => '\\WPML\\Core\\Twig\\Template', 'strict_variables' => \false, 'autoescape' => 'html', 'cache' => \false, 'auto_reload' => null, 'optimizations' => -1], $options);
$this->debug = (bool) $options['debug'];
$this->charset = \strtoupper($options['charset']);
$this->baseTemplateClass = $options['base_template_class'];
$this->autoReload = null === $options['auto_reload'] ? $this->debug : (bool) $options['auto_reload'];
$this->strictVariables = (bool) $options['strict_variables'];
$this->setCache($options['cache']);
$this->addExtension(new \WPML\Core\Twig\Extension\CoreExtension());
$this->addExtension(new \WPML\Core\Twig\Extension\EscaperExtension($options['autoescape']));
$this->addExtension(new \WPML\Core\Twig\Extension\OptimizerExtension($options['optimizations']));
$this->staging = new \WPML\Core\Twig\Extension\StagingExtension();
if (\is_string($this->originalCache)) {
$r = new \ReflectionMethod($this, 'writeCacheFile');
if (__CLASS__ !== $r->getDeclaringClass()->getName()) {
@\trigger_error('The Twig\\Environment::writeCacheFile method is deprecated since version 1.22 and will be removed in Twig 2.0.', \E_USER_DEPRECATED);
$this->bcWriteCacheFile = \true;
$r = new \ReflectionMethod($this, 'getCacheFilename');
if (__CLASS__ !== $r->getDeclaringClass()->getName()) {
@\trigger_error('The Twig\\Environment::getCacheFilename method is deprecated since version 1.22 and will be removed in Twig 2.0.', \E_USER_DEPRECATED);
$this->bcGetCacheFilename = \true;
* Gets the base template class for compiled templates.
* @return string The base template class name
public function getBaseTemplateClass()
return $this->baseTemplateClass;
* Sets the base template class for compiled templates.
* @param string $class The base template class name
public function setBaseTemplateClass($class)
$this->baseTemplateClass = $class;
$this->updateOptionsHash();
* Enables debugging mode.
public function enableDebug()
$this->updateOptionsHash();
* Disables debugging mode.
public function disableDebug()
$this->updateOptionsHash();
* Checks if debug mode is enabled.
* @return bool true if debug mode is enabled, false otherwise
public function isDebug()
* Enables the auto_reload option.
public function enableAutoReload()
$this->autoReload = \true;
* Disables the auto_reload option.
public function disableAutoReload()
$this->autoReload = \false;
* Checks if the auto_reload option is enabled.
* @return bool true if auto_reload is enabled, false otherwise
public function isAutoReload()
return $this->autoReload;
* Enables the strict_variables option.
public function enableStrictVariables()
$this->strictVariables = \true;
$this->updateOptionsHash();
* Disables the strict_variables option.
public function disableStrictVariables()
$this->strictVariables = \false;
$this->updateOptionsHash();
* Checks if the strict_variables option is enabled.
* @return bool true if strict_variables is enabled, false otherwise
public function isStrictVariables()
return $this->strictVariables;
* Gets the current cache implementation.
* @param bool $original Whether to return the original cache option or the real cache instance
* @return CacheInterface|string|false A Twig\Cache\CacheInterface implementation,
* an absolute path to the compiled templates,
* or false to disable cache
public function getCache($original = \true)
return $original ? $this->originalCache : $this->cache;
* Sets the current cache implementation.
* @param CacheInterface|string|false $cache A Twig\Cache\CacheInterface implementation,
* an absolute path to the compiled templates,
* or false to disable cache
public function setCache($cache)
if (\is_string($cache)) {
$this->originalCache = $cache;
$this->cache = new \WPML\Core\Twig\Cache\FilesystemCache($cache);
} elseif (\false === $cache) {
$this->originalCache = $cache;
$this->cache = new \WPML\Core\Twig\Cache\NullCache();
} elseif (null === $cache) {
@\trigger_error('Using "null" as the cache strategy is deprecated since version 1.23 and will be removed in Twig 2.0.', \E_USER_DEPRECATED);
$this->originalCache = \false;
$this->cache = new \WPML\Core\Twig\Cache\NullCache();
} elseif ($cache instanceof \WPML\Core\Twig\Cache\CacheInterface) {
$this->originalCache = $this->cache = $cache;
throw new \LogicException(\sprintf('Cache can only be a string, false, or a \\Twig\\Cache\\CacheInterface implementation.'));
* Gets the cache filename for a given template.
* @param string $name The template name
* @return string|false The cache file name or false when caching is disabled
* @deprecated since 1.22 (to be removed in 2.0)
public function getCacheFilename($name)
@\trigger_error(\sprintf('The %s method is deprecated since version 1.22 and will be removed in Twig 2.0.', __METHOD__), \E_USER_DEPRECATED);
$key = $this->cache->generateKey($name, $this->getTemplateClass($name));
return !$key ? \false : $key;
* Gets the template class associated with the given string.
* The generated template class is based on the following parameters:
* * The cache key for the given template;
* * The currently enabled extensions;
* * Whether the Twig C extension is available or not;
* * Options with what environment was created.
* @param string $name The name for which to calculate the template class name
* @param int|null $index The index if it is an embedded template
* @return string The template class name
public function getTemplateClass($name, $index = null)
$key = $this->getLoader()->getCacheKey($name) . $this->optionsHash;
return $this->templateClassPrefix . \hash('sha256', $key) . (null === $index ? '' : '___' . $index);
* Gets the template class prefix.
* @return string The template class prefix
* @deprecated since 1.22 (to be removed in 2.0)
public function getTemplateClassPrefix()
@\trigger_error(\sprintf('The %s method is deprecated since version 1.22 and will be removed in Twig 2.0.', __METHOD__), \E_USER_DEPRECATED);
return $this->templateClassPrefix;
* @param string|TemplateWrapper $name The template name
* @param array $context An array of parameters to pass to the template
* @return string The rendered template
* @throws LoaderError When the template cannot be found
* @throws SyntaxError When an error occurred during compilation
* @throws RuntimeError When an error occurred during rendering
public function render($name, array $context = [])
return $this->load($name)->render($context);
* @param string|TemplateWrapper $name The template name
* @param array $context An array of parameters to pass to the template
* @throws LoaderError When the template cannot be found
* @throws SyntaxError When an error occurred during compilation
* @throws RuntimeError When an error occurred during rendering
public function display($name, array $context = [])
$this->load($name)->display($context);
* @param string|TemplateWrapper|\Twig\Template $name The template name
* @throws LoaderError When the template cannot be found
* @throws RuntimeError When a previously generated cache is corrupted
* @throws SyntaxError When an error occurred during compilation
* @return TemplateWrapper
public function load($name)
if ($name instanceof \WPML\Core\Twig\TemplateWrapper) {
if ($name instanceof \WPML\Core\Twig\Template) {
return new \WPML\Core\Twig\TemplateWrapper($this, $name);
return new \WPML\Core\Twig\TemplateWrapper($this, $this->loadTemplate($name));
* Loads a template internal representation.
* This method is for internal use only and should never be called
* @param string $name The template name
* @param int $index The index if it is an embedded template
* @return \Twig_TemplateInterface A template instance representing the given template name
* @throws LoaderError When the template cannot be found
* @throws RuntimeError When a previously generated cache is corrupted
* @throws SyntaxError When an error occurred during compilation
public function loadTemplate($name, $index = null)
return $this->loadClass($this->getTemplateClass($name), $name, $index);
public function loadClass($cls, $name, $index = null)
if (isset($this->loadedTemplates[$cls])) {
return $this->loadedTemplates[$cls];
if (!\class_exists($cls, \false)) {
if ($this->bcGetCacheFilename) {
$key = $this->getCacheFilename($name);
$key = $this->cache->generateKey($name, $mainCls);
if (!$this->isAutoReload() || $this->isTemplateFresh($name, $this->cache->getTimestamp($key))) {
$this->cache->load($key);
if (!\class_exists($cls, \false)) {
$loader = $this->getLoader();
if (!$loader instanceof \WPML\Core\Twig\Loader\SourceContextLoaderInterface) {
$source = new \WPML\Core\Twig\Source($loader->getSource($name), $name);
$source = $loader->getSourceContext($name);
$content = $this->compileSource($source);
if ($this->bcWriteCacheFile) {
$this->writeCacheFile($key, $content);
$this->cache->write($key, $content);
$this->cache->load($key);
if (!\class_exists($mainCls, \false)) {
/* Last line of defense if either $this->bcWriteCacheFile was used,
* $this->cache is implemented as a no-op or we have a race condition
* where the cache was cleared between the above calls to write to and load from
if (!\class_exists($cls, \false)) {
throw new \WPML\Core\Twig\Error\RuntimeError(\sprintf('Failed to load Twig template "%s", index "%s": cache might be corrupted.', $name, $index), -1, $source);
if (!$this->runtimeInitialized) {
return $this->loadedTemplates[$cls] = new $cls($this);
* Creates a template from source.
* This method should not be used as a generic way to load templates.
* @param string $template The template source
* @param string $name An optional name of the template to be used in error messages
* @return TemplateWrapper A template instance representing the given template name
* @throws LoaderError When the template cannot be found
* @throws SyntaxError When an error occurred during compilation
public function createTemplate($template, $name = null)
$hash = \hash('sha256', $template, \false);
$name = \sprintf('%s (string template %s)', $name, $hash);
$name = \sprintf('__string_template__%s', $hash);
$loader = new \WPML\Core\Twig\Loader\ChainLoader([new \WPML\Core\Twig\Loader\ArrayLoader([$name => $template]), $current = $this->getLoader()]);
$this->setLoader($loader);
$template = new \WPML\Core\Twig\TemplateWrapper($this, $this->loadTemplate($name));
} catch (\Exception $e) {
$this->setLoader($current);
} catch (\Throwable $e) {
$this->setLoader($current);
$this->setLoader($current);
* Returns true if the template is still fresh.
* Besides checking the loader for freshness information,
* this method also checks if the enabled extensions have
* @param string $name The template name
* @param int $time The last modification time of the cached template
* @return bool true if the template is fresh, false otherwise