refactored the code handling arguments for PHP callbacks when compiling nodes

This commit is contained in:
Fabien Potencier
2012-11-14 15:44:15 +01:00
parent e1ad2bde9c
commit 65637b7968
4 changed files with 77 additions and 82 deletions
+62
View File
@@ -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(')');
}
}
+6 -27
View 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);
}
$compiler->raw($filter->compile());
protected function compileFilter(Twig_Compiler $compiler, Twig_FilterInterface $filter)
{
$compiler
->raw($filter->compile().'(')
->raw($filter->needsEnvironment() ? '$this->env, ' : '')
->raw($filter->needsContext() ? '$context, ' : '')
;
$this->setAttribute('needs_environment', $filter->needsEnvironment());
$this->setAttribute('needs_context', $filter->needsContext());
$this->setAttribute('arguments', $filter->getArguments());
foreach ($filter->getArguments() as $argument) {
$compiler
->string($argument)
->raw(', ')
;
}
$compiler->subcompile($this->getNode('node'));
foreach ($this->getNode('arguments') as $node) {
$compiler
->raw(', ')
->subcompile($node)
;
}
$compiler->raw(')');
$this->compileArguments($compiler);
}
}
+6 -33
View 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);
}
}
+3 -22
View 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());
$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(')');
$this->compileArguments($compiler);
}
}