mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-14 19:36:43 +00:00
Simplify implementation
This commit is contained in:
@@ -467,8 +467,10 @@ class ExpressionParser
|
|||||||
}
|
}
|
||||||
$recurseArgs = new ArrayExpression([], $line);
|
$recurseArgs = new ArrayExpression([], $line);
|
||||||
$recurseArgs->addElement($args->getNode('0'));
|
$recurseArgs->addElement($args->getNode('0'));
|
||||||
|
$expr = new GetAttrExpression(new NameExpression('loop', $line), new ConstantExpression('__invoke', $line), $recurseArgs, Template::METHOD_CALL, $line);
|
||||||
|
$expr->setAttribute('is_generator', true);
|
||||||
|
|
||||||
return new GetAttrExpression(new NameExpression('loop', $line), new ConstantExpression('__invoke', $line), $recurseArgs, Template::METHOD_CALL, $line);
|
return $expr;
|
||||||
default:
|
default:
|
||||||
if (null !== $alias = $this->parser->getImportedSymbol('function', $name)) {
|
if (null !== $alias = $this->parser->getImportedSymbol('function', $name)) {
|
||||||
$arguments = new ArrayExpression([], $line);
|
$arguments = new ArrayExpression([], $line);
|
||||||
|
|||||||
@@ -56,7 +56,6 @@ use Twig\Node\Expression\Test\SameasTest;
|
|||||||
use Twig\Node\Expression\Unary\NegUnary;
|
use Twig\Node\Expression\Unary\NegUnary;
|
||||||
use Twig\Node\Expression\Unary\NotUnary;
|
use Twig\Node\Expression\Unary\NotUnary;
|
||||||
use Twig\Node\Expression\Unary\PosUnary;
|
use Twig\Node\Expression\Unary\PosUnary;
|
||||||
use Twig\NodeVisitor\ForNodeVisitor;
|
|
||||||
use Twig\NodeVisitor\MacroAutoImportNodeVisitor;
|
use Twig\NodeVisitor\MacroAutoImportNodeVisitor;
|
||||||
use Twig\Source;
|
use Twig\Source;
|
||||||
use Twig\Template;
|
use Twig\Template;
|
||||||
@@ -269,10 +268,7 @@ final class CoreExtension extends AbstractExtension
|
|||||||
|
|
||||||
public function getNodeVisitors(): array
|
public function getNodeVisitors(): array
|
||||||
{
|
{
|
||||||
return [
|
return [new MacroAutoImportNodeVisitor()];
|
||||||
new MacroAutoImportNodeVisitor(),
|
|
||||||
new ForNodeVisitor(),
|
|
||||||
];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getOperators(): array
|
public function getOperators(): array
|
||||||
|
|||||||
@@ -1,74 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
/*
|
|
||||||
* This file is part of Twig.
|
|
||||||
*
|
|
||||||
* (c) Fabien Potencier
|
|
||||||
*
|
|
||||||
* For the full copyright and license information, please view the LICENSE
|
|
||||||
* file that was distributed with this source code.
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace Twig\NodeVisitor;
|
|
||||||
|
|
||||||
use Twig\Environment;
|
|
||||||
use Twig\Node\Expression\ConstantExpression;
|
|
||||||
use Twig\Node\Expression\GetAttrExpression;
|
|
||||||
use Twig\Node\Expression\NameExpression;
|
|
||||||
use Twig\Node\ForNode;
|
|
||||||
use Twig\Node\Node;
|
|
||||||
use Twig\Node\PrintNode;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author Fabien Potencier <fabien@symfony.com>
|
|
||||||
*
|
|
||||||
* @internal
|
|
||||||
*/
|
|
||||||
final class ForNodeVisitor implements NodeVisitorInterface
|
|
||||||
{
|
|
||||||
private int $loops = 0;
|
|
||||||
|
|
||||||
public function enterNode(Node $node, Environment $env): Node
|
|
||||||
{
|
|
||||||
if ($node instanceof ForNode) {
|
|
||||||
++$this->loops;
|
|
||||||
|
|
||||||
return $node;
|
|
||||||
} elseif (!$this->loops) {
|
|
||||||
// we are outside a loop
|
|
||||||
return $node;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!$node instanceof PrintNode) {
|
|
||||||
return $node;
|
|
||||||
}
|
|
||||||
|
|
||||||
// We look for exactly {{ loop.__invoke(...) }}
|
|
||||||
$exprNode = $node->getNode('expr');
|
|
||||||
if (
|
|
||||||
$exprNode instanceof GetAttrExpression
|
|
||||||
&& $exprNode->getNode('node') instanceof NameExpression
|
|
||||||
&& 'loop' === $exprNode->getNode('node')->getAttribute('name')
|
|
||||||
&& $exprNode->getNode('attribute') instanceof ConstantExpression
|
|
||||||
&& '__invoke' === $exprNode->getNode('attribute')->getAttribute('value')
|
|
||||||
) {
|
|
||||||
$exprNode->setAttribute('is_generator', true);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $node;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function leaveNode(Node $node, Environment $env): ?Node
|
|
||||||
{
|
|
||||||
if ($node instanceof ForNode) {
|
|
||||||
--$this->loops;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $node;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getPriority(): int
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user