From: Fabien Potencier Date: Tue, 1 Nov 2011 07:43:09 +0000 (+0100) Subject: moved the defined test to a Twig_Test_Node class X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=c749c6b8224bd7dc9b9edfefa58c3502cfd95251;p=web%2Fkonrad%2Ftwig.git moved the defined test to a Twig_Test_Node class --- diff --git a/lib/Twig/Extension/Core.php b/lib/Twig/Extension/Core.php index e39b9f4..fb7067a 100644 --- a/lib/Twig/Extension/Core.php +++ b/lib/Twig/Extension/Core.php @@ -110,7 +110,7 @@ class Twig_Extension_Core extends Twig_Extension return array( 'even' => new Twig_Test_Function('twig_test_even'), 'odd' => new Twig_Test_Function('twig_test_odd'), - 'defined' => new Twig_Test_Function('twig_test_defined'), + 'defined' => new Twig_Test_Node('Twig_Node_Expression_Test_Defined'), 'sameas' => new Twig_Test_Node('Twig_Node_Expression_Test_Sameas'), 'none' => new Twig_Test_Function('twig_test_none'), 'null' => new Twig_Test_Function('twig_test_none'), @@ -838,26 +838,6 @@ function twig_test_constant($value, $constant) } /** - * Checks if a variable is defined in the current context. - * - *
- * {# defined works with variable names #}
- * {% if foo is defined %}
- *     {# ... #}
- * {% endif %}
- * 
- * - * @param mixed $name A PHP variable - * @param array $context The current context - * - * @return Boolean true if the value is defined, false otherwise - */ -function twig_test_defined($name, $context) -{ - return array_key_exists($name, $context); -} - -/** * Checks if a variable is empty. * *
diff --git a/lib/Twig/Node/Expression/DefaultFilter.php b/lib/Twig/Node/Expression/DefaultFilter.php
index b184f1e..e2d8cac 100644
--- a/lib/Twig/Node/Expression/DefaultFilter.php
+++ b/lib/Twig/Node/Expression/DefaultFilter.php
@@ -16,7 +16,7 @@ class Twig_Node_Expression_DefaultFilter extends Twig_Node_Expression
             throw new Twig_Error('The default filter node cannot be created from the given node.', $node->getLine());
         }
 
-        $test = new Twig_Node_Expression_Test(clone $node->getNode('node'), 'defined', new Twig_Node(), $node->getLine());
+        $test = new Twig_Node_Expression_Test_Defined(clone $node->getNode('node'), 'defined', new Twig_Node(), $node->getLine());
         $default = count($node->getNode('arguments')) ? $node->getNode('arguments')->getNode(0) : new Twig_Node_Expression_Constant('', $node->getLine());
 
         $node = new Twig_Node_Expression_Conditional($test, $node, $default, $node->getLine());
diff --git a/lib/Twig/Node/Expression/Test.php b/lib/Twig/Node/Expression/Test.php
index 8991f52..088f60e 100644
--- a/lib/Twig/Node/Expression/Test.php
+++ b/lib/Twig/Node/Expression/Test.php
@@ -13,28 +13,6 @@ class Twig_Node_Expression_Test extends Twig_Node_Expression
     public function __construct(Twig_NodeInterface $node, $name, Twig_NodeInterface $arguments = null, $lineno)
     {
         parent::__construct(array('node' => $node, 'arguments' => $arguments), array('name' => $name), $lineno);
-
-        // defined is a special case
-        if ('defined' === $name) {
-            if ($node instanceof Twig_Node_Expression_Name) {
-                $node->setAttribute('is_defined_test', true);
-            } elseif ($node instanceof Twig_Node_Expression_GetAttr) {
-                $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 changeIgnoreStrictCheck(Twig_Node_Expression_GetAttr $node)
-    {
-        $node->setAttribute('ignore_strict_check', true);
-
-        if ($node->getNode('node') instanceof Twig_Node_Expression_GetAttr) {
-            $this->changeIgnoreStrictCheck($node->getNode('node'));
-        }
     }
 
     public function compile(Twig_Compiler $compiler)
@@ -47,13 +25,6 @@ class Twig_Node_Expression_Test extends Twig_Node_Expression
         $name = $this->getAttribute('name');
         $node = $this->getNode('node');
 
-        // defined is a special case
-        if ('defined' === $name) {
-            $compiler->subcompile($node);
-
-            return;
-        }
-
         $compiler
             ->raw($testMap[$name]->compile().'(')
             ->subcompile($node)
diff --git a/lib/Twig/Node/Expression/Test/Defined.php b/lib/Twig/Node/Expression/Test/Defined.php
new file mode 100644
index 0000000..e7c6828
--- /dev/null
+++ b/lib/Twig/Node/Expression/Test/Defined.php
@@ -0,0 +1,55 @@
+
+ * {# defined works with variable names and variable attributes #}
+ * {% if foo is defined %}
+ *     {# ... #}
+ * {% endif %}
+ * 
+ * + * @package twig + * @author Fabien Potencier + */ +class Twig_Node_Expression_Test_Defined extends Twig_Node_Expression_Test +{ + public function __construct(Twig_NodeInterface $node, $name, Twig_NodeInterface $arguments = null, $lineno) + { + parent::__construct($node, $name, $arguments, $lineno); + + if ($node instanceof Twig_Node_Expression_Name) { + $node->setAttribute('is_defined_test', true); + } elseif ($node instanceof Twig_Node_Expression_GetAttr) { + $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 changeIgnoreStrictCheck(Twig_Node_Expression_GetAttr $node) + { + $node->setAttribute('ignore_strict_check', true); + + if ($node->getNode('node') instanceof Twig_Node_Expression_GetAttr) { + $this->changeIgnoreStrictCheck($node->getNode('node')); + } + } + + public function compile(Twig_Compiler $compiler) + { + $compiler->subcompile($this->getNode('node')); + } +}