From 65637b7968c23ce44ddb33d464f9a6979e57ca68 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Wed, 14 Nov 2012 15:44:15 +0100 Subject: [PATCH] refactored the code handling arguments for PHP callbacks when compiling nodes --- lib/Twig/Node/Expression/Call.php | 62 +++++++++++++++++++++++++++++++++ lib/Twig/Node/Expression/Filter.php | 33 +++-------------- lib/Twig/Node/Expression/Function.php | 39 +++----------------- lib/Twig/Node/Expression/Test.php | 25 ++------------ 4 files changed, 77 insertions(+), 82 deletions(-) create mode 100644 lib/Twig/Node/Expression/Call.php diff --git a/lib/Twig/Node/Expression/Call.php b/lib/Twig/Node/Expression/Call.php new file mode 100644 index 0000000..d24680c --- /dev/null +++ b/lib/Twig/Node/Expression/Call.php @@ -0,0 +1,62 @@ +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(')'); + } +} diff --git a/lib/Twig/Node/Expression/Filter.php b/lib/Twig/Node/Expression/Filter.php index b7c62cc..45d2c93 100644 --- a/lib/Twig/Node/Expression/Filter.php +++ b/lib/Twig/Node/Expression/Filter.php @@ -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); } } diff --git a/lib/Twig/Node/Expression/Function.php b/lib/Twig/Node/Expression/Function.php index 9e0b305..58461e4 100644 --- a/lib/Twig/Node/Expression/Function.php +++ b/lib/Twig/Node/Expression/Function.php @@ -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); } } diff --git a/lib/Twig/Node/Expression/Test.php b/lib/Twig/Node/Expression/Test.php index 507d5d0..b41858c 100644 --- a/lib/Twig/Node/Expression/Test.php +++ b/lib/Twig/Node/Expression/Test.php @@ -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); } } -- 1.7.2.5