106 @trigger_error(
'Not passing a Twig_LoaderInterface as the first constructor argument of Twig_Environment is deprecated since version 1.21.', E_USER_DEPRECATED);
111 'charset' =>
'UTF-8',
112 'base_template_class' =>
'Twig_Template',
113 'strict_variables' =>
false,
114 'autoescape' =>
'html',
116 'auto_reload' => null,
117 'optimizations' => -1,
120 $this->debug = (bool)
$options[
'debug'];
121 $this->charset = strtoupper(
$options[
'charset']);
122 $this->baseTemplateClass =
$options[
'base_template_class'];
123 $this->autoReload = null ===
$options[
'auto_reload'] ? $this->debug : (bool)
$options[
'auto_reload'];
124 $this->strictVariables = (bool)
$options[
'strict_variables'];
133 if (is_string($this->originalCache)) {
135 if (__CLASS__ !==
$r->getDeclaringClass()->getName()) {
136 @trigger_error(
'The Twig_Environment::writeCacheFile method is deprecated since version 1.22 and will be removed in Twig 2.0.', E_USER_DEPRECATED);
138 $this->bcWriteCacheFile =
true;
142 if (__CLASS__ !==
$r->getDeclaringClass()->getName()) {
143 @trigger_error(
'The Twig_Environment::getCacheFilename method is deprecated since version 1.22 and will be removed in Twig 2.0.', E_USER_DEPRECATED);
145 $this->bcGetCacheFilename =
true;
167 $this->baseTemplateClass = $class;
185 $this->debug =
false;
204 $this->autoReload =
true;
212 $this->autoReload =
false;
230 $this->strictVariables =
true;
239 $this->strictVariables =
false;
277 $this->originalCache =
$cache;
279 } elseif (
false ===
$cache) {
280 $this->originalCache =
$cache;
282 } elseif (null ===
$cache) {
283 @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);
284 $this->originalCache =
false;
287 $this->originalCache = $this->cache =
$cache;
289 throw new LogicException(sprintf(
'Cache can only be a string, false, or a Twig_CacheInterface implementation.'));
304 @trigger_error(sprintf(
'The %s method is deprecated since version 1.22 and will be removed in Twig 2.0.', __METHOD__), E_USER_DEPRECATED);
332 return $this->templateClassPrefix.hash(
'sha256',
$key).(null ===
$index ?
'' :
'_'.$index);
344 @trigger_error(sprintf(
'The %s method is deprecated since version 1.22 and will be removed in Twig 2.0.', __METHOD__), E_USER_DEPRECATED);
399 return new Twig_TemplateWrapper($this,
$name);
429 if (isset($this->loadedTemplates[$cls])) {
430 return $this->loadedTemplates[$cls];
433 if (!class_exists($cls,
false)) {
434 if ($this->bcGetCacheFilename) {
437 $key = $this->cache->generateKey(
$name, $mainCls);
441 $this->cache->load(
$key);
444 if (!class_exists($cls,
false)) {
454 if ($this->bcWriteCacheFile) {
457 $this->cache->write(
$key, $content);
458 $this->cache->load(
$key);
461 if (!class_exists($mainCls,
false)) {
471 if (!class_exists($cls,
false)) {
476 if (!$this->runtimeInitialized) {
480 if (isset($this->loading[$cls])) {
481 throw new Twig_Error_Runtime(sprintf(
'Circular reference detected for Twig template "%s", path: %s.',
$name, implode(
' -> ', array_merge($this->loading, array(
$name)))));
484 $this->loading[$cls] =
$name;
487 $this->loadedTemplates[$cls] =
new $cls($this);
488 unset($this->loading[$cls]);
490 unset($this->loading[$cls]);
495 return $this->loadedTemplates[$cls];
550 if (0 === $this->lastModifiedExtension) {
551 foreach ($this->extensions as $extension) {
552 $r =
new ReflectionObject($extension);
554 $this->lastModifiedExtension = $extensionTime;
577 if (!is_array($names)) {
578 $names = array($names);
581 foreach ($names as
$name) {
596 if (1 === count($names)) {
600 throw new Twig_Error_Loader(sprintf(
'Unable to find one of the following templates: "%s".', implode(
'", "', $names)));
610 @trigger_error(sprintf(
'The %s method is deprecated since version 1.18.3 and will be removed in Twig 2.0.', __METHOD__), E_USER_DEPRECATED);
612 $this->loadedTemplates = array();
622 @trigger_error(sprintf(
'The %s method is deprecated since version 1.22 and will be removed in Twig 2.0.', __METHOD__), E_USER_DEPRECATED);
624 if (is_string($this->originalCache)) {
626 if ($file->isFile()) {
627 @unlink($file->getPathname());
642 @trigger_error(sprintf(
'The %s() method is deprecated since version 1.25 and will be removed in 2.0.', __FUNCTION__), E_USER_DEPRECATED);
644 if (null === $this->lexer) {
669 @trigger_error(sprintf(
'Passing a string as the $source argument of %s() is deprecated since version 1.27. Pass a Twig_Source instance instead.', __METHOD__), E_USER_DEPRECATED);
673 if (null === $this->lexer) {
677 return $this->lexer->tokenize(
$source);
689 @trigger_error(sprintf(
'The %s() method is deprecated since version 1.25 and will be removed in 2.0.', __FUNCTION__), E_USER_DEPRECATED);
691 if (null === $this->parser) {
712 if (null === $this->parser) {
716 return $this->parser->parse($stream);
728 @trigger_error(sprintf(
'The %s() method is deprecated since version 1.25 and will be removed in 2.0.', __FUNCTION__), E_USER_DEPRECATED);
730 if (null === $this->compiler) {
749 if (null === $this->compiler) {
753 return $this->compiler->compile($node)->getSource();
769 @trigger_error(sprintf(
'Passing a string as the $source argument of %s() is deprecated since version 1.27. Pass a Twig_Source instance instead.', __METHOD__), E_USER_DEPRECATED);
779 throw new Twig_Error_Syntax(sprintf(
'An exception has been thrown during the compilation of a template ("%s").', $e->getMessage()), -1,
$source, $e);
786 @trigger_error(sprintf(
'Twig loader "%s" should implement Twig_SourceContextLoaderInterface since version 1.27.', get_class($loader)), E_USER_DEPRECATED);
799 if (null === $this->loader) {
813 $this->charset = strtoupper(
$charset);
833 $this->runtimeInitialized =
true;
839 if (
'Twig_Extension' !==
$m->getDeclaringClass()->getName()) {
840 @trigger_error(sprintf(
'Defining the initRuntime() method in the "%s" extension is deprecated since version 1.23. Use the `needs_environment` option to get the Twig_Environment instance in filters, functions, or tests; or explicitly implement Twig_Extension_InitRuntimeInterface if needed (not recommended).',
$name), E_USER_DEPRECATED);
844 $extension->initRuntime($this);
857 $class = ltrim($class,
'\\');
858 if (!isset($this->extensionsByClass[$class]) && class_exists($class,
false)) {
861 $class = $class->name;
864 if (isset($this->extensions[$class])) {
865 if ($class !== get_class($this->extensions[$class])) {
866 @trigger_error(sprintf(
'Referencing the "%s" extension by its name (defined by getName()) is deprecated since 1.26 and will be removed in Twig 2.0. Use the Fully Qualified Extension Class Name instead.', $class), E_USER_DEPRECATED);
872 return isset($this->extensionsByClass[$class]);
880 $this->runtimeLoaders[] =
$loader;
892 $class = ltrim($class,
'\\');
893 if (!isset($this->extensionsByClass[$class]) && class_exists($class,
false)) {
896 $class = $class->name;
899 if (isset($this->extensions[$class])) {
900 if ($class !== get_class($this->extensions[$class])) {
901 @trigger_error(sprintf(
'Referencing the "%s" extension by its name (defined by getName()) is deprecated since 1.26 and will be removed in Twig 2.0. Use the Fully Qualified Extension Class Name instead.', $class), E_USER_DEPRECATED);
904 return $this->extensions[$class];
907 if (!isset($this->extensionsByClass[$class])) {
911 return $this->extensionsByClass[$class];
925 if (isset($this->runtimes[$class])) {
926 return $this->runtimes[$class];
929 foreach ($this->runtimeLoaders as
$loader) {
930 if (null !== $runtime = $loader->load($class)) {
931 return $this->runtimes[$class] = $runtime;
940 if ($this->extensionInitialized) {
941 throw new LogicException(sprintf(
'Unable to register extension "%s" as extensions have already been initialized.', $extension->
getName()));
944 $class = get_class($extension);
945 if ($class !== $extension->
getName()) {
946 if (isset($this->extensions[$extension->
getName()])) {
947 unset($this->extensions[$extension->
getName()], $this->extensionsByClass[$class]);
948 @trigger_error(sprintf(
'The possibility to register the same extension twice ("%s") is deprecated since version 1.23 and will be removed in Twig 2.0. Use proper PHP inheritance instead.', $extension->
getName()), E_USER_DEPRECATED);
952 $this->lastModifiedExtension = 0;
953 $this->extensionsByClass[$class] = $extension;
954 $this->extensions[$extension->
getName()] = $extension;
969 @trigger_error(sprintf(
'The %s method is deprecated since version 1.12 and will be removed in Twig 2.0.', __METHOD__), E_USER_DEPRECATED);
971 if ($this->extensionInitialized) {
972 throw new LogicException(sprintf(
'Unable to remove extension "%s" as extensions have already been initialized.',
$name));
975 $class = ltrim(
$name,
'\\');
976 if (!isset($this->extensionsByClass[$class]) && class_exists($class,
false)) {
979 $class = $class->name;
982 if (isset($this->extensions[$class])) {
983 if ($class !== get_class($this->extensions[$class])) {
984 @trigger_error(sprintf(
'Referencing the "%s" extension by its name (defined by getName()) is deprecated since 1.26 and will be removed in Twig 2.0. Use the Fully Qualified Extension Class Name instead.', $class), E_USER_DEPRECATED);
987 unset($this->extensions[$class]);
990 unset($this->extensions[$class]);
1001 foreach ($extensions as $extension) {
1018 if ($this->extensionInitialized) {
1019 throw new LogicException(
'Unable to add a token parser as extensions have already been initialized.');
1022 $this->staging->addTokenParser($parser);
1034 if (!$this->extensionInitialized) {
1064 if ($this->extensionInitialized) {
1065 throw new LogicException(
'Unable to add a node visitor as extensions have already been initialized.');
1068 $this->staging->addNodeVisitor($visitor);
1080 if (!$this->extensionInitialized) {
1096 throw new LogicException(
'A filter must be an instance of Twig_FilterInterface or Twig_SimpleFilter.');
1101 $name = $filter->getName();
1103 @trigger_error(sprintf(
'Passing a name as a first argument to the %s method is deprecated since version 1.21. Pass an instance of "Twig_SimpleFilter" instead when defining filter "%s".', __METHOD__,
$name), E_USER_DEPRECATED);
1106 if ($this->extensionInitialized) {
1107 throw new LogicException(sprintf(
'Unable to add filter "%s" as extensions have already been initialized.',
$name));
1110 $this->staging->addFilter(
$name, $filter);
1127 if (!$this->extensionInitialized) {
1131 if (isset($this->filters[
$name])) {
1132 return $this->filters[
$name];
1135 foreach ($this->filters as $pattern => $filter) {
1136 $pattern = str_replace(
'\\*',
'(.*?)', preg_quote($pattern,
'#'), $count);
1139 if (preg_match(
'#^'.$pattern.
'$#', $name, $matches)) {
1140 array_shift($matches);
1141 $filter->setArguments($matches);
1148 foreach ($this->filterCallbacks as $callback) {
1149 if (
false !== $filter = call_user_func($callback, $name)) {
1159 $this->filterCallbacks[] = $callable;
1175 if (!$this->extensionInitialized) {
1191 throw new LogicException(
'A test must be an instance of Twig_TestInterface or Twig_SimpleTest.');
1198 @trigger_error(sprintf(
'Passing a name as a first argument to the %s method is deprecated since version 1.21. Pass an instance of "Twig_SimpleTest" instead when defining test "%s".', __METHOD__,
$name), E_USER_DEPRECATED);
1201 if ($this->extensionInitialized) {
1202 throw new LogicException(sprintf(
'Unable to add test "%s" as extensions have already been initialized.',
$name));
1217 if (!$this->extensionInitialized) {
1235 if (!$this->extensionInitialized) {
1239 if (isset($this->tests[
$name])) {
1240 return $this->tests[
$name];
1255 throw new LogicException(
'A function must be an instance of Twig_FunctionInterface or Twig_SimpleFunction.');
1260 $name = $function->getName();
1262 @trigger_error(sprintf(
'Passing a name as a first argument to the %s method is deprecated since version 1.21. Pass an instance of "Twig_SimpleFunction" instead when defining function "%s".', __METHOD__,
$name), E_USER_DEPRECATED);
1265 if ($this->extensionInitialized) {
1266 throw new LogicException(sprintf(
'Unable to add function "%s" as extensions have already been initialized.',
$name));
1269 $this->staging->addFunction(
$name, $function);
1286 if (!$this->extensionInitialized) {
1290 if (isset($this->functions[
$name])) {
1291 return $this->functions[
$name];
1294 foreach ($this->functions as $pattern => $function) {
1295 $pattern = str_replace(
'\\*',
'(.*?)', preg_quote($pattern,
'#'), $count);
1298 if (preg_match(
'#^'.$pattern.
'$#', $name, $matches)) {
1299 array_shift($matches);
1300 $function->setArguments($matches);
1307 foreach ($this->functionCallbacks as $callback) {
1308 if (
false !== $function = call_user_func($callback, $name)) {
1318 $this->functionCallbacks[] = $callable;
1334 if (!$this->extensionInitialized) {
1352 if ($this->extensionInitialized || $this->runtimeInitialized) {
1353 if (null === $this->globals) {
1357 if (!array_key_exists(
$name, $this->globals)) {
1359 @trigger_error(sprintf(
'Registering global variable "%s" at runtime or when the extensions have already been initialized is deprecated since version 1.21.',
$name), E_USER_DEPRECATED);
1364 if ($this->extensionInitialized || $this->runtimeInitialized) {
1366 $this->globals[
$name] = $value;
1368 $this->staging->addGlobal(
$name, $value);
1381 if (!$this->runtimeInitialized && !$this->extensionInitialized) {
1385 if (null === $this->globals) {
1404 if (!array_key_exists(
$key, $context)) {
1405 $context[
$key] = $value;
1421 if (!$this->extensionInitialized) {
1437 if (!$this->extensionInitialized) {
1449 @trigger_error(sprintf(
'The %s method is deprecated since version 1.23 and will be removed in Twig 2.0.', __METHOD__), E_USER_DEPRECATED);
1460 foreach ($this->extensions as
$name => $extension) {
1464 if (
'Twig_Extension' !==
$m->getDeclaringClass()->getName()) {
1465 @trigger_error(sprintf(
'Defining the getGlobals() method in the "%s" extension without explicitly implementing Twig_Extension_GlobalsInterface is deprecated since version 1.23.',
$name), E_USER_DEPRECATED);
1469 $extGlob = $extension->getGlobals();
1470 if (!is_array($extGlob)) {
1471 throw new UnexpectedValueException(sprintf(
'"%s::getGlobals()" must return an array of globals.', get_class($extension)));
1477 $globals[] = $this->staging->getGlobals();
1479 return call_user_func_array(
'array_merge',
$globals);
1487 if ($this->extensionInitialized) {
1492 $this->filters = array();
1493 $this->functions = array();
1494 $this->tests = array();
1495 $this->visitors = array();
1496 $this->unaryOperators = array();
1497 $this->binaryOperators = array();
1499 foreach ($this->extensions as $extension) {
1504 $this->extensionInitialized =
true;
1515 $name = $filter->getName();
1517 @trigger_error(sprintf(
'Using an instance of "%s" for filter "%s" is deprecated since version 1.21. Use Twig_SimpleFilter instead.', get_class($filter), $name), E_USER_DEPRECATED);
1520 $this->filters[
$name] = $filter;
1524 foreach ($extension->
getFunctions() as $name => $function) {
1526 $name = $function->getName();
1528 @trigger_error(sprintf(
'Using an instance of "%s" for function "%s" is deprecated since version 1.21. Use Twig_SimpleFunction instead.', get_class($function), $name), E_USER_DEPRECATED);
1531 $this->functions[
$name] = $function;
1537 $name =
$test->getName();
1539 @trigger_error(sprintf(
'Using an instance of "%s" for test "%s" is deprecated since version 1.21. Use Twig_SimpleTest instead.', get_class(
$test), $name), E_USER_DEPRECATED);
1548 $this->parsers->addTokenParser(
$parser);
1550 @trigger_error(
'Registering a Twig_TokenParserBrokerInterface instance is deprecated since version 1.21.', E_USER_DEPRECATED);
1552 $this->parsers->addTokenParserBroker(
$parser);
1554 throw new LogicException(
'getTokenParsers() must return an array of Twig_TokenParserInterface or Twig_TokenParserBrokerInterface instances.');
1560 $this->visitors[] = $visitor;
1565 if (!is_array($operators)) {
1566 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)));
1569 if (2 !== count($operators)) {
1570 throw new InvalidArgumentException(sprintf(
'"%s::getOperators()" must return an array of 2 elements, got %d.', get_class($extension), count($operators)));
1573 $this->unaryOperators = array_merge($this->unaryOperators, $operators[0]);
1574 $this->binaryOperators = array_merge($this->binaryOperators, $operators[1]);
1583 $this->cache->write($file, $content);
1588 $hashParts = array_merge(
1589 array_keys($this->extensions),
1591 (
int) function_exists(
'twig_template_get_attributes'),
1596 $this->baseTemplateClass,
1597 (
int) $this->strictVariables,
1600 $this->optionsHash = implode(
':', $hashParts);
1604 class_alias(
'Twig_Environment',
'Twig\Environment',
false);
Interface implemented by extension classes.
getBinaryOperators()
Gets the registered binary Operators.
getCache($original=true)
Gets the current cache implementation.
Represents a node in the AST.
writeCacheFile($file, $content)
getCacheFilename($name)
Gets the cache filename for a given template.
resolveTemplate($names)
Tries to load a template consecutively from an array.
getTemplateClass($name, $index=null)
Gets the template class associated with the given string.
getLexer()
Gets the Lexer instance.
Interface implemented by token parser brokers.
Interface implemented by parser classes.
Represents a template function.
isDebug()
Checks if debug mode is enabled.
removeExtension($name)
Removes an extension by name.
Exposes a template to userland.
Represents a template test.
setCharset($charset)
Sets the default template charset.
setParser(Twig_ParserInterface $parser)
Default parser implementation.
addExtension(Twig_ExtensionInterface $extension)
getFilter($name)
Get a filter by name.
__construct(Twig_LoaderInterface $loader=null, $options=array())
Constructor.
getNodeVisitors()
Returns the node visitor instances to add to the existing list.
Exception thrown when an error occurs during template loading.
getBaseTemplateClass()
Gets the base template class for compiled templates.
registerUndefinedFunctionCallback($callable)
display($name, array $context=array())
Displays a template.
isTemplateFresh($name, $time)
Returns true if the template is still fresh.
setSourceContext(Twig_Source $source=null)
Sets the source context of the Twig template where the error occurred.
static computeAlternatives($name, $items)
addFilter($name, $filter=null)
Registers a Filter.
Represents a template filter.
addFunction($name, $function=null)
Registers a Function.
computeAlternatives($name, $items)
Creates runtime implementations for Twig elements (filters/functions/tests).
Enables usage of the deprecated Twig_Extension::getGlobals() method.
Exception thrown when an error occurs at runtime.
Represents a token stream.
enableAutoReload()
Enables the auto_reload option.
getNodeVisitors()
Gets the registered Node Visitors.
Enables usage of the deprecated Twig_Extension::initRuntime() method.
$stream
PHP stream implementation.
Represents a template filter.
Exception thrown when a syntax error occurs during lexing or parsing of a template.
getCharset()
Gets the default template charset.
getLoader()
Gets the Loader instance.
initRuntime()
Initializes the runtime environment.
setLexer(Twig_LexerInterface $lexer)
getFunction($name)
Get a function by name.
getFunctions()
Returns a list of functions to add to the existing list.
getTests()
Gets the registered Tests.
setLoader(Twig_LoaderInterface $loader)
getRuntime($class)
Returns the runtime implementation of a Twig element (filter/function/test).
getTags()
Gets registered tags.
getFilters()
Gets the registered Filters.
Twig_NodeVisitorInterface is the interface the all node visitor classes must implement.
tokenize($source, $name=null)
Tokenizes a source code.
mergeGlobals(array $context)
Merges a context with the defined globals.
addRuntimeLoader(Twig_RuntimeLoaderInterface $loader)
Adds a runtime loader.
Interface implemented by lexer classes.
compileSource($source, $name=null)
Compiles a template source code.
Represents a template function.
getOperators()
Returns a list of operators to add to the existing list.
getTests()
Returns a list of tests to add to the existing list.
enableDebug()
Enables debugging mode.
getTest($name)
Gets a test by name.
getTemplateClassPrefix()
Gets the template class prefix.
clearTemplateCache()
Clears the internal template cache.
getTokenParsers()
Returns the token parser instances to add to the existing list.
Adds a getSourceContext() method for loaders.
getCompiler()
Gets the Compiler instance.
getParser()
Gets the Parser instance.
addGlobal($name, $value)
Registers a Global.
getGlobals()
Gets the registered Globals.
Loads templates from other loaders.
getFilters()
Returns a list of filters to add to the existing list.
addNodeVisitor(Twig_NodeVisitorInterface $visitor)
parse(Twig_TokenStream $stream)
Converts a token stream to a node tree.
hasExtension($class)
Returns true if the given extension is registered.
setCompiler(Twig_CompilerInterface $compiler)
getTokenParsers()
Gets the registered Token Parsers.
Default base class for compiled templates.
setCache($cache)
Sets the current cache implementation.
addTokenParser(Twig_TokenParserInterface $parser)
clearCacheFiles()
Clears the template cache files on the filesystem.
loadTemplate($name, $index=null)
Loads a template internal representation.
initExtension(Twig_ExtensionInterface $extension)
enableStrictVariables()
Enables the strict_variables option.
isAutoReload()
Checks if the auto_reload option is enabled.
registerUndefinedFilterCallback($callable)
Implements a cache on the filesystem.
setExtensions(array $extensions)
Registers an array of extensions.
getExtensions()
Returns all registered extensions.
Interface implemented by token parsers.
Default implementation of a token parser broker.
render($name, array $context=array())
Renders a template.
Represents a template test.
Holds information about a non-compiled Twig template.
disableStrictVariables()
Disables the strict_variables option.
setBaseTemplateClass($class)
Sets the base template class for compiled templates.
getUnaryOperators()
Gets the registered unary Operators.
compile(Twig_NodeInterface $node)
Compiles a node and returns the PHP code.
Stores the Twig configuration.
Interface implemented by cache classes.
addTest($name, $test=null)
Registers a Test.
disableDebug()
Disables debugging mode.
createTemplate($template)
Creates a template from source.
hash(StreamInterface $stream, $algo, $rawOutput=false)
Calculate a hash of a Stream.
Interface all loaders must implement.
Loads a template from an array.
Implements a no-cache strategy.
getFunctions()
Gets registered functions.
getExtension($class)
Gets an extension by class name.
load($name)
Loads a template.
getName()
Returns the name of the extension.
isStrictVariables()
Checks if the strict_variables option is enabled.
Interface implemented by compiler classes.
disableAutoReload()
Disables the auto_reload option.