From: Fabien Potencier Date: Wed, 26 May 2010 06:02:03 +0000 (+0200) Subject: made some small optimizations X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=46ed296f563cc00fb39d280eb98c75aba0864a28;p=web%2Fkonrad%2Ftwig.git made some small optimizations --- diff --git a/lib/Twig/Compiler.php b/lib/Twig/Compiler.php index 9abc90e..ce3525f 100644 --- a/lib/Twig/Compiler.php +++ b/lib/Twig/Compiler.php @@ -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. * diff --git a/lib/Twig/Node/Expression/Unary.php b/lib/Twig/Node/Expression/Unary.php index d3fec09..225f6c7 100644 --- a/lib/Twig/Node/Expression/Unary.php +++ b/lib/Twig/Node/Expression/Unary.php @@ -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); diff --git a/lib/Twig/Node/Set.php b/lib/Twig/Node/Set.php index 6df66bb..07e45cf 100644 --- a/lib/Twig/Node/Set.php +++ b/lib/Twig/Node/Set.php @@ -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()");