fixed the "defined" test and the "default" filter
authorFabien Potencier <fabien.potencier@gmail.com>
Thu, 27 Oct 2011 16:23:43 +0000 (18:23 +0200)
committerFabien Potencier <fabien.potencier@gmail.com>
Thu, 27 Oct 2011 16:26:17 +0000 (18:26 +0200)
CHANGELOG
lib/Twig/Node/Expression/GetAttr.php
lib/Twig/Node/Expression/Test.php
lib/Twig/Template.php
test/Twig/Tests/TemplateTest.php

index 0883a7d..1b6a012 100644 (file)
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,5 +1,6 @@
 * 1.4.0
 
+ * fixed the "defined" test and the "default" filter (now works with more than one call (foo.bar.foo) and for both values of the strict_variables option)
  * changed the way extensions are loaded (addFilter/addFunction/addGlobal/addTest/addNodeVisitor/addTokenParser/addExtension can now be called in any order)
  * added Twig_Environment::display()
  * made the escape filter smarter when the encoding is not supported by PHP
index 1ea1512..8d7184b 100644 (file)
@@ -13,14 +13,14 @@ class Twig_Node_Expression_GetAttr extends Twig_Node_Expression
 {
     public function __construct(Twig_Node_Expression $node, Twig_Node_Expression $attribute, Twig_NodeInterface $arguments, $type, $lineno)
     {
-        parent::__construct(array('node' => $node, 'attribute' => $attribute, 'arguments' => $arguments), array('type' => $type, 'is_defined_test' => false), $lineno);
+        parent::__construct(array('node' => $node, 'attribute' => $attribute, 'arguments' => $arguments), array('type' => $type, 'is_defined_test' => false, 'ignore_strict_check' => false), $lineno);
     }
 
     public function compile(Twig_Compiler $compiler)
     {
         $compiler->raw('$this->getAttribute(');
 
-        if ($this->getAttribute('is_defined_test')) {
+        if ($this->getAttribute('ignore_strict_check')) {
             $this->getNode('node')->setAttribute('ignore_strict_check', true);
         }
 
@@ -28,7 +28,7 @@ class Twig_Node_Expression_GetAttr extends Twig_Node_Expression
 
         $compiler->raw(', ')->subcompile($this->getNode('attribute'));
 
-        if (count($this->getNode('arguments')) || Twig_TemplateInterface::ANY_CALL !== $this->getAttribute('type') || $this->getAttribute('is_defined_test')) {
+        if (count($this->getNode('arguments')) || Twig_TemplateInterface::ANY_CALL !== $this->getAttribute('type') || $this->getAttribute('is_defined_test') || $this->getAttribute('ignore_strict_check')) {
             $compiler->raw(', array(');
 
             foreach ($this->getNode('arguments') as $node) {
@@ -40,12 +40,16 @@ class Twig_Node_Expression_GetAttr extends Twig_Node_Expression
 
             $compiler->raw(')');
 
-            if (Twig_TemplateInterface::ANY_CALL !== $this->getAttribute('type') || $this->getAttribute('is_defined_test')) {
+            if (Twig_TemplateInterface::ANY_CALL !== $this->getAttribute('type') || $this->getAttribute('is_defined_test') || $this->getAttribute('ignore_strict_check')) {
                 $compiler->raw(', ')->repr($this->getAttribute('type'));
             }
 
-            if ($this->getAttribute('is_defined_test')) {
-                $compiler->raw(', true');
+            if ($this->getAttribute('is_defined_test') || $this->getAttribute('ignore_strict_check')) {
+                $compiler->raw(', '.($this->getAttribute('is_defined_test') ? 'true' : 'false'));
+            }
+
+            if ($this->getAttribute('ignore_strict_check')) {
+                $compiler->raw(', '.($this->getAttribute('ignore_strict_check') ? 'true' : 'false'));
             }
         }
 
index 6af4bd0..3a26bcf 100644 (file)
@@ -19,19 +19,25 @@ class Twig_Node_Expression_Test extends Twig_Node_Expression
             if ($node instanceof Twig_Node_Expression_Name) {
                 $node->setAttribute('is_defined_test', true);
             } elseif ($node instanceof Twig_Node_Expression_GetAttr) {
-                $this->changeIsDefined($node);
+                $node->setAttribute('is_defined_test', true);
+
+                $this->changeIgnoreStrictCheck($node);
             } else {
                 throw new Twig_Error_Syntax('The "defined" test only works with simple variables', $this->getLine());
             }
         }
     }
 
-    protected function changeIsDefined(Twig_Node_Expression_GetAttr $node)
+    protected function changeIgnoreStrictCheck(Twig_Node_Expression_GetAttr $node)
     {
-        $node->setAttribute('is_defined_test', true);
+        $node->setAttribute('ignore_strict_check', true);
 
         if ($node->getNode('node') instanceof Twig_Node_Expression_GetAttr) {
-            $this->changeIsDefined($node->getNode('node'));
+            $this->changeIgnoreStrictCheck($node->getNode('node'));
+        }
+
+        if (count($node->getNode('arguments'))) {
+            throw new Twig_Error_Syntax('The "defined" test only works with simple variables', $this->getLine());
         }
     }
 
index 0b3e36d..77e5d04 100644 (file)
@@ -306,7 +306,7 @@ abstract class Twig_Template implements Twig_TemplateInterface
      * @param string  $type          The type of attribute (@see Twig_TemplateInterface)
      * @param Boolean $isDefinedTest Whether this is only a defined check
      */
-    protected function getAttribute($object, $item, array $arguments = array(), $type = Twig_TemplateInterface::ANY_CALL, $isDefinedTest = false)
+    protected function getAttribute($object, $item, array $arguments = array(), $type = Twig_TemplateInterface::ANY_CALL, $isDefinedTest = false, $ignoreStrictCheck = false)
     {
         // array
         if (Twig_TemplateInterface::METHOD_CALL !== $type) {
@@ -325,7 +325,7 @@ abstract class Twig_Template implements Twig_TemplateInterface
                     return false;
                 }
 
-                if (!$this->env->isStrictVariables()) {
+                if ($ignoreStrictCheck || !$this->env->isStrictVariables()) {
                     return null;
                 }
 
@@ -343,7 +343,7 @@ abstract class Twig_Template implements Twig_TemplateInterface
                 return false;
             }
 
-            if (!$this->env->isStrictVariables()) {
+            if ($ignoreStrictCheck || !$this->env->isStrictVariables()) {
                 return null;
             }
 
@@ -396,7 +396,7 @@ abstract class Twig_Template implements Twig_TemplateInterface
                 return false;
             }
 
-            if (!$this->env->isStrictVariables()) {
+            if ($ignoreStrictCheck || !$this->env->isStrictVariables()) {
                 return null;
             }
 
index 94b9204..8f94c89 100644 (file)
@@ -142,9 +142,9 @@ class Twig_TemplateTest extends Twig_Template
     {
     }
 
-    public function getAttribute($object, $item, array $arguments = array(), $type = Twig_TemplateInterface::ANY_CALL, $isDefinedTest = false)
+    public function getAttribute($object, $item, array $arguments = array(), $type = Twig_TemplateInterface::ANY_CALL, $isDefinedTest = false, $ignoreStrictCheck = false)
     {
-        return parent::getAttribute($object, $item, $arguments, $type, $isDefinedTest);
+        return parent::getAttribute($object, $item, $arguments, $type, $isDefinedTest, $ignoreStrictCheck);
     }
 }