From 5da378ceaa6322bb318695e593bd12260458b319 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Wed, 26 Oct 2011 10:27:18 +0200 Subject: [PATCH] removed unneeded complexity in GetAttr node --- lib/Twig/Node/Expression/GetAttr.php | 13 ++++--------- lib/Twig/Node/Expression/Name.php | 9 ++++++++- lib/Twig/Template.php | 9 +++++---- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/lib/Twig/Node/Expression/GetAttr.php b/lib/Twig/Node/Expression/GetAttr.php index c3be2f8..1ea1512 100644 --- a/lib/Twig/Node/Expression/GetAttr.php +++ b/lib/Twig/Node/Expression/GetAttr.php @@ -20,17 +20,12 @@ class Twig_Node_Expression_GetAttr extends Twig_Node_Expression { $compiler->raw('$this->getAttribute('); - if ($this->getAttribute('is_defined_test') && $compiler->getEnvironment()->isStrictVariables()) { - $compiler->subcompile(new Twig_Node_Expression_Filter( - $this->getNode('node'), - new Twig_Node_Expression_Constant('default', $this->getLine()), - new Twig_Node(), - $this->getLine() - )); - } else { - $compiler->subcompile($this->getNode('node')); + if ($this->getAttribute('is_defined_test')) { + $this->getNode('node')->setAttribute('ignore_strict_check', true); } + $compiler->subcompile($this->getNode('node')); + $compiler->raw(', ')->subcompile($this->getNode('attribute')); if (count($this->getNode('arguments')) || Twig_TemplateInterface::ANY_CALL !== $this->getAttribute('type') || $this->getAttribute('is_defined_test')) { diff --git a/lib/Twig/Node/Expression/Name.php b/lib/Twig/Node/Expression/Name.php index 632270a..a8d206f 100644 --- a/lib/Twig/Node/Expression/Name.php +++ b/lib/Twig/Node/Expression/Name.php @@ -19,7 +19,7 @@ class Twig_Node_Expression_Name extends Twig_Node_Expression public function __construct($name, $lineno) { - parent::__construct(array(), array('name' => $name, 'is_defined_test' => false), $lineno); + parent::__construct(array(), array('name' => $name, 'is_defined_test' => false, 'ignore_strict_check' => false), $lineno); } public function compile(Twig_Compiler $compiler) @@ -38,6 +38,13 @@ class Twig_Node_Expression_Name extends Twig_Node_Expression $compiler ->raw('$this->getContext($context, ') ->string($name) + ; + + if ($this->getAttribute('ignore_strict_check')) { + $compiler->raw(', true'); + } + + $compiler ->raw(')') ; } diff --git a/lib/Twig/Template.php b/lib/Twig/Template.php index fa502fa..0b3e36d 100644 --- a/lib/Twig/Template.php +++ b/lib/Twig/Template.php @@ -276,17 +276,18 @@ abstract class Twig_Template implements Twig_TemplateInterface /** * Returns a variable from the context. * - * @param array $context The context - * @param string $item The variable to return from the context + * @param array $context The context + * @param string $item The variable to return from the context + * @param Boolean $isDefinedTest Whether to ignore the strict variable check or not * * @return The content of the context variable * * @throws Twig_Error_Runtime if the variable does not exist and Twig is running in strict mode */ - protected function getContext($context, $item) + protected function getContext($context, $item, $ignoreStrictCheck = false) { if (!array_key_exists($item, $context)) { - if (!$this->env->isStrictVariables()) { + if ($ignoreStrictCheck || !$this->env->isStrictVariables()) { return null; } -- 1.7.2.5