made some small optimizations
authorFabien Potencier <fabien.potencier@gmail.com>
Wed, 26 May 2010 06:02:03 +0000 (08:02 +0200)
committerFabien Potencier <fabien.potencier@gmail.com>
Wed, 26 May 2010 06:02:03 +0000 (08:02 +0200)
lib/Twig/Compiler.php
lib/Twig/Node/Expression/Unary.php
lib/Twig/Node/Set.php

index 9abc90e..ce3525f 100644 (file)
@@ -52,23 +52,29 @@ class Twig_Compiler implements Twig_CompilerInterface
     /**
      * Compiles a node.
      *
-     * @param  Twig_NodeInterface $node The node to compile
+     * @param Twig_NodeInterface $node   The node to compile
+     * @param integer            $indent The current indentation
      *
      * @return Twig_Compiler The current compiler instance
      */
-    public function compile(Twig_NodeInterface $node)
+    public function compile(Twig_NodeInterface $node, $indentation = 0)
     {
         $this->lastLine = null;
         $this->source = '';
-        $this->indentation = 0;
+        $this->indentation = $indentation;
 
         $node->compile($this);
 
         return $this;
     }
 
-    public function subcompile(Twig_NodeInterface $node)
+    public function subcompile(Twig_NodeInterface $node, $raw = true)
     {
+        if (false === $raw)
+        {
+            $this->addIndentation();
+        }
+
         $node->compile($this);
 
         return $this;
@@ -97,12 +103,20 @@ class Twig_Compiler implements Twig_CompilerInterface
     {
         $strings = func_get_args();
         foreach ($strings as $string) {
-            $this->source .= str_repeat(' ', $this->indentation * 4).$string;
+            $this->addIndentation();
+            $this->source .= $string;
         }
 
         return $this;
     }
 
+    public function addIndentation()
+    {
+        $this->source .= str_repeat(' ', $this->indentation * 4);
+
+        return $this;
+    }
+
     /**
      * Adds a quoted string to the compiled code.
      *
index d3fec09..225f6c7 100644 (file)
@@ -31,12 +31,15 @@ abstract class Twig_Node_Expression_Unary extends Twig_Node_Expression
 
         return implode("\n", $repr);
     }
+
     public function compile($compiler)
     {
         $compiler->raw('(');
         $this->operator($compiler);
-        $this->node->compile($compiler);
-        $compiler->raw(')');
+        $compiler
+            ->subcompile($this->node)
+            ->raw(')')
+        ;
     }
 
     abstract public function operator($compiler);
index 6df66bb..07e45cf 100644 (file)
@@ -71,11 +71,7 @@ class Twig_Node_Set extends Twig_Node implements Twig_NodeListInterface
                     $compiler->raw(', ');
                 }
 
-                $compiler
-                    ->raw('$context[')
-                    ->string($node->getName())
-                    ->raw(']')
-                ;
+                $compiler->subcompile($node);
             }
             $compiler->raw(')');
         } else {
@@ -86,11 +82,7 @@ class Twig_Node_Set extends Twig_Node implements Twig_NodeListInterface
                 ;
             }
 
-            $compiler
-                ->write('$context[')
-                ->string($this->names->getName())
-                ->raw(']')
-            ;
+            $compiler->subcompile($this->names, false);
 
             if ($this->capture) {
                 $compiler->raw(" = ob_get_clean()");