From: Fabien Potencier Date: Wed, 26 May 2010 11:56:43 +0000 (+0200) Subject: added ignore_invalid_variables setting to throw an exception when an invalid variable... X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=63a52ae7de04d8aac5d09e2cd45e36d03fcecc7f;p=web%2Fkonrad%2Ftwig.git added ignore_invalid_variables setting to throw an exception when an invalid variable is used in a template --- diff --git a/CHANGELOG b/CHANGELOG index d139d02..24e53d8 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -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 diff --git a/lib/Twig/Environment.php b/lib/Twig/Environment.php index a05a251..87a8d4c 100644 --- a/lib/Twig/Environment.php +++ b/lib/Twig/Environment.php @@ -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; diff --git a/lib/Twig/Node/Expression/Name.php b/lib/Twig/Node/Expression/Name.php index b4a8e41..46a1e6f 100644 --- a/lib/Twig/Node/Expression/Name.php +++ b/lib/Twig/Node/Expression/Name.php @@ -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() diff --git a/lib/Twig/Resource.php b/lib/Twig/Resource.php index f1274c7..bfef8be 100644 --- a/lib/Twig/Resource.php +++ b/lib/Twig/Resource.php @@ -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')) {