--- /dev/null
+<?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(')');
+ }
+}
* 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)
{
{
$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);
}
}
* 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)
{
{
$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);
}
}
* 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)
{
$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);
}
}