made some small optimizations

This commit is contained in:
Fabien Potencier
2010-05-26 08:02:03 +02:00
parent 6d39e22499
commit 46ed296f56
3 changed files with 26 additions and 17 deletions
+19 -5
View 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.
*
+5 -2
View 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);
+2 -10
View 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()");