mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-14 11:27:00 +00:00
added ignore_invalid_variables setting to throw an exception when an invalid variable is used in a template
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
Backward incompatibilities:
|
||||
* The short notation of the `block` tag changed.
|
||||
|
||||
* added ignore_invalid_variables setting to throw an exception when an invalid variable is used in a template (disabled automatically when debug is true)
|
||||
* added the lexer, parser, and compiler as arguments to the Twig_Environment constructor
|
||||
* changed the cache option to only accepts an explicit path to a cache directory or false
|
||||
* added a way to add token parsers, filters, and visitors without creating an extension
|
||||
|
||||
@@ -29,6 +29,7 @@ class Twig_Environment
|
||||
protected $filters;
|
||||
protected $runtimeInitialized;
|
||||
protected $loadedTemplates;
|
||||
protected $ignoreInvalidVars;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
@@ -55,6 +56,8 @@ class Twig_Environment
|
||||
* If you don't provide the auto_reload option, it will be
|
||||
* determined automatically base on the debug value.
|
||||
*
|
||||
* * ignore_invalid_variables: Whether to ignore invalid variables in templates (default to the opposite value of debug).
|
||||
*
|
||||
* @param Twig_LoaderInterface $loader A Twig_LoaderInterface instance
|
||||
* @param array $options An array of options
|
||||
*/
|
||||
@@ -74,6 +77,7 @@ class Twig_Environment
|
||||
$this->baseTemplateClass = isset($options['base_template_class']) ? $options['base_template_class'] : 'Twig_Template';
|
||||
$this->autoReload = isset($options['auto_reload']) ? (bool) $options['auto_reload'] : $this->debug;
|
||||
$this->extensions = array('core' => new Twig_Extension_Core());
|
||||
$this->ignoreInvalidVars = isset($options['ignore_invalid_variables']) ? (bool) $options['ignore_invalid_variables'] : !$this->debug;
|
||||
$this->runtimeInitialized = false;
|
||||
if (isset($options['cache']) && $options['cache']) {
|
||||
$this->setCache($options['cache']);
|
||||
@@ -115,6 +119,21 @@ class Twig_Environment
|
||||
$this->autoReload = (Boolean) $autoReload;
|
||||
}
|
||||
|
||||
public function enableignoreInvalidVars()
|
||||
{
|
||||
$this->ignoreInvalidVars = true;
|
||||
}
|
||||
|
||||
public function disableignoreInvalidVars()
|
||||
{
|
||||
$this->ignoreInvalidVars = false;
|
||||
}
|
||||
|
||||
public function ignoresInvalidVars()
|
||||
{
|
||||
return $this->ignoreInvalidVars;
|
||||
}
|
||||
|
||||
public function getCache()
|
||||
{
|
||||
return $this->cache;
|
||||
|
||||
@@ -26,7 +26,7 @@ class Twig_Node_Expression_Name extends Twig_Node_Expression
|
||||
|
||||
public function compile($compiler)
|
||||
{
|
||||
$compiler->raw(sprintf('(isset($context[\'%s\']) ? $context[\'%s\'] : null)', $this->name, $this->name));
|
||||
$compiler->raw(sprintf('$this->getContext($context, \'%s\')', $this->name, $this->name));
|
||||
}
|
||||
|
||||
public function getName()
|
||||
|
||||
+23
-2
@@ -29,6 +29,19 @@ abstract class Twig_Resource
|
||||
throw new Twig_RuntimeError(sprintf('The filter "%s" does not exist', $name));
|
||||
}
|
||||
|
||||
protected function getContext($context, $item)
|
||||
{
|
||||
if (isset($context[$item])) {
|
||||
return $context[$item];
|
||||
}
|
||||
|
||||
if ($this->env->ignoresInvalidVars()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
throw new InvalidArgumentException(sprintf('Item "%s" from context does not exist.', $item));
|
||||
}
|
||||
|
||||
protected function getAttribute($object, $item, array $arguments = array(), $arrayOnly = false)
|
||||
{
|
||||
$item = (string) $item;
|
||||
@@ -38,7 +51,11 @@ abstract class Twig_Resource
|
||||
}
|
||||
|
||||
if ($arrayOnly || !is_object($object)) {
|
||||
return null;
|
||||
if ($this->env->ignoresInvalidVars()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
throw new InvalidArgumentException(sprintf('Key "%s" for array "%s" does not exist.', $item, $object));
|
||||
}
|
||||
|
||||
if (isset($object->$item)) {
|
||||
@@ -67,7 +84,11 @@ abstract class Twig_Resource
|
||||
} elseif (isset($this->cache[$class]['__call'])) {
|
||||
$method = $item;
|
||||
} else {
|
||||
return null;
|
||||
if ($this->env->ignoresInvalidVars()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
throw new InvalidArgumentException(sprintf('Method "%s" for object "%s" does not exist.', $item, get_class($object)));
|
||||
}
|
||||
|
||||
if ($this->env->hasExtension('sandbox')) {
|
||||
|
||||
Reference in New Issue
Block a user