mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-15 03:46:39 +00:00
refactored the code handling arguments for PHP callbacks when compiling nodes
This commit is contained in:
@@ -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(')');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -9,7 +9,7 @@
|
|||||||
* For the full copyright and license information, please view the LICENSE
|
* For the full copyright and license information, please view the LICENSE
|
||||||
* file that was distributed with this source code.
|
* 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)
|
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'));
|
$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)
|
$this->setAttribute('needs_environment', $filter->needsEnvironment());
|
||||||
{
|
$this->setAttribute('needs_context', $filter->needsContext());
|
||||||
$compiler
|
$this->setAttribute('arguments', $filter->getArguments());
|
||||||
->raw($filter->compile().'(')
|
|
||||||
->raw($filter->needsEnvironment() ? '$this->env, ' : '')
|
|
||||||
->raw($filter->needsContext() ? '$context, ' : '')
|
|
||||||
;
|
|
||||||
|
|
||||||
foreach ($filter->getArguments() as $argument) {
|
$this->compileArguments($compiler);
|
||||||
$compiler
|
|
||||||
->string($argument)
|
|
||||||
->raw(', ')
|
|
||||||
;
|
|
||||||
}
|
|
||||||
|
|
||||||
$compiler->subcompile($this->getNode('node'));
|
|
||||||
|
|
||||||
foreach ($this->getNode('arguments') as $node) {
|
|
||||||
$compiler
|
|
||||||
->raw(', ')
|
|
||||||
->subcompile($node)
|
|
||||||
;
|
|
||||||
}
|
|
||||||
|
|
||||||
$compiler->raw(')');
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,7 +8,7 @@
|
|||||||
* For the full copyright and license information, please view the LICENSE
|
* For the full copyright and license information, please view the LICENSE
|
||||||
* file that was distributed with this source code.
|
* 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)
|
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'));
|
$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()) {
|
$this->compileArguments($compiler);
|
||||||
$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(')');
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,7 +8,7 @@
|
|||||||
* For the full copyright and license information, please view the LICENSE
|
* For the full copyright and license information, please view the LICENSE
|
||||||
* file that was distributed with this source code.
|
* 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)
|
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');
|
$name = $this->getAttribute('name');
|
||||||
$testMap = $compiler->getEnvironment()->getTests();
|
$testMap = $compiler->getEnvironment()->getTests();
|
||||||
|
|
||||||
$name = $this->getAttribute('name');
|
$compiler->raw($testMap[$name]->compile());
|
||||||
$node = $this->getNode('node');
|
|
||||||
|
|
||||||
$compiler
|
$this->compileArguments($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(')');
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user