refactored the code handling arguments for PHP callbacks when compiling nodes
authorFabien Potencier <fabien.potencier@gmail.com>
Wed, 14 Nov 2012 14:44:15 +0000 (15:44 +0100)
committerFabien Potencier <fabien.potencier@gmail.com>
Wed, 14 Nov 2012 18:26:01 +0000 (19:26 +0100)
lib/Twig/Node/Expression/Call.php [new file with mode: 0644]
lib/Twig/Node/Expression/Filter.php
lib/Twig/Node/Expression/Function.php
lib/Twig/Node/Expression/Test.php

diff --git a/lib/Twig/Node/Expression/Call.php b/lib/Twig/Node/Expression/Call.php
new file mode 100644 (file)
index 0000000..d24680c
--- /dev/null
@@ -0,0 +1,62 @@
+<?php
+
+/*
+ * This file is part of Twig.
+ *
+ * (c) 2012 Fabien Potencier
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+abstract class Twig_Node_Expression_Call extends Twig_Node_Expression
+{
+    public function compileArguments(Twig_Compiler $compiler)
+    {
+        $compiler->raw('(');
+
+        $first = true;
+
+        if ($this->hasAttribute('needs_environment') && $this->getAttribute('needs_environment')) {
+            $compiler->raw('$this->env');
+            $first = false;
+        }
+
+        if ($this->hasAttribute('needs_context') && $this->getAttribute('needs_context')) {
+            if (!$first) {
+                $compiler->raw(', ');
+            }
+            $compiler->raw('$context');
+            $first = false;
+        }
+
+        if ($this->hasAttribute('arguments')) {
+            foreach ($this->getAttribute('arguments') as $argument) {
+                if (!$first) {
+                    $compiler->raw(', ');
+                }
+                $compiler->string($argument);
+                $first = false;
+            }
+        }
+
+        if ($this->hasNode('node')) {
+            if (!$first) {
+                $compiler->raw(', ');
+            }
+            $compiler->subcompile($this->getNode('node'));
+            $first = false;
+        }
+
+        if ($this->hasNode('arguments') && null !== $this->getNode('arguments')) {
+            foreach ($this->getNode('arguments') as $node) {
+                if (!$first) {
+                    $compiler->raw(', ');
+                }
+                $compiler->subcompile($node);
+                $first = false;
+            }
+        }
+
+        $compiler->raw(')');
+    }
+}
index b7c62cc..45d2c93 100644 (file)
@@ -9,7 +9,7 @@
  * For the full copyright and license information, please view the LICENSE
  * file that was distributed with this source code.
  */
-class Twig_Node_Expression_Filter extends Twig_Node_Expression
+class Twig_Node_Expression_Filter extends Twig_Node_Expression_Call
 {
     public function __construct(Twig_NodeInterface $node, Twig_Node_Expression_Constant $filterName, Twig_NodeInterface $arguments, $lineno, $tag = null)
     {
@@ -20,33 +20,12 @@ class Twig_Node_Expression_Filter extends Twig_Node_Expression
     {
         $filter = $compiler->getEnvironment()->getFilter($this->getNode('filter')->getAttribute('value'));
 
-        $this->compileFilter($compiler, $filter);
-    }
-
-    protected function compileFilter(Twig_Compiler $compiler, Twig_FilterInterface $filter)
-    {
-        $compiler
-            ->raw($filter->compile().'(')
-            ->raw($filter->needsEnvironment() ? '$this->env, ' : '')
-            ->raw($filter->needsContext() ? '$context, ' : '')
-        ;
-
-        foreach ($filter->getArguments() as $argument) {
-            $compiler
-                ->string($argument)
-                ->raw(', ')
-            ;
-        }
-
-        $compiler->subcompile($this->getNode('node'));
+        $compiler->raw($filter->compile());
 
-        foreach ($this->getNode('arguments') as $node) {
-            $compiler
-                ->raw(', ')
-                ->subcompile($node)
-            ;
-        }
+        $this->setAttribute('needs_environment', $filter->needsEnvironment());
+        $this->setAttribute('needs_context', $filter->needsContext());
+        $this->setAttribute('arguments', $filter->getArguments());
 
-        $compiler->raw(')');
+        $this->compileArguments($compiler);
     }
 }
index 9e0b305..58461e4 100644 (file)
@@ -8,7 +8,7 @@
  * For the full copyright and license information, please view the LICENSE
  * file that was distributed with this source code.
  */
-class Twig_Node_Expression_Function extends Twig_Node_Expression
+class Twig_Node_Expression_Function extends Twig_Node_Expression_Call
 {
     public function __construct($name, Twig_NodeInterface $arguments, $lineno)
     {
@@ -19,39 +19,12 @@ class Twig_Node_Expression_Function extends Twig_Node_Expression
     {
         $function = $compiler->getEnvironment()->getFunction($this->getAttribute('name'));
 
-        $compiler->raw($function->compile().'(');
+        $compiler->raw($function->compile());
 
-        $first = true;
+        $this->setAttribute('needs_environment', $function->needsEnvironment());
+        $this->setAttribute('needs_context', $function->needsContext());
+        $this->setAttribute('arguments', $function->getArguments());
 
-        if ($function->needsEnvironment()) {
-            $compiler->raw('$this->env');
-            $first = false;
-        }
-
-        if ($function->needsContext()) {
-            if (!$first) {
-                $compiler->raw(', ');
-            }
-            $compiler->raw('$context');
-            $first = false;
-        }
-
-        foreach ($function->getArguments() as $argument) {
-            if (!$first) {
-                $compiler->raw(', ');
-            }
-            $compiler->string($argument);
-            $first = false;
-        }
-
-        foreach ($this->getNode('arguments') as $node) {
-            if (!$first) {
-                $compiler->raw(', ');
-            }
-            $compiler->subcompile($node);
-            $first = false;
-        }
-
-        $compiler->raw(')');
+        $this->compileArguments($compiler);
     }
 }
index 507d5d0..b41858c 100644 (file)
@@ -8,7 +8,7 @@
  * For the full copyright and license information, please view the LICENSE
  * file that was distributed with this source code.
  */
-class Twig_Node_Expression_Test extends Twig_Node_Expression
+class Twig_Node_Expression_Test extends Twig_Node_Expression_Call
 {
     public function __construct(Twig_NodeInterface $node, $name, Twig_NodeInterface $arguments = null, $lineno)
     {
@@ -20,27 +20,8 @@ class Twig_Node_Expression_Test extends Twig_Node_Expression
         $name = $this->getAttribute('name');
         $testMap = $compiler->getEnvironment()->getTests();
 
-        $name = $this->getAttribute('name');
-        $node = $this->getNode('node');
-
-        $compiler
-            ->raw($testMap[$name]->compile().'(')
-            ->subcompile($node)
-        ;
-
-        if (null !== $this->getNode('arguments')) {
-            $compiler->raw(', ');
-
-            $max = count($this->getNode('arguments')) - 1;
-            foreach ($this->getNode('arguments') as $i => $arg) {
-                $compiler->subcompile($arg);
-
-                if ($i != $max) {
-                    $compiler->raw(', ');
-                }
-            }
-        }
+        $compiler->raw($testMap[$name]->compile());
 
-        $compiler->raw(')');
+        $this->compileArguments($compiler);
     }
 }