Merge branch '3.x' into 4.x

* 3.x:
  Add ForElseNode
This commit is contained in:
Fabien Potencier
2025-01-24 15:32:30 +01:00
4 changed files with 55 additions and 10 deletions
+41
View File
@@ -0,0 +1,41 @@
<?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\Node;
use Twig\Attribute\YieldReady;
use Twig\Compiler;
/**
* Represents an else node in a for loop.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
#[YieldReady]
class ForElseNode extends Node
{
public function __construct(Node $body, int $lineno)
{
parent::__construct(['body' => $body], [], $lineno);
}
public function compile(Compiler $compiler): void
{
$compiler
->addDebugInfo($this)
->write("if (0 === \$iterator->getIndex0()) {\n")
->indent()
->subcompile($this->getNode('body'))
->outdent()
->write("}\n")
;
}
}
+7 -7
View File
@@ -31,6 +31,12 @@ class ForNode extends Node
$body = new IfNode(new Nodes([$ifexpr, $body]), null, $lineno);
}
if (null !== $else && !$else instanceof ForElseNode) {
trigger_deprecation('twig/twig', '3.19', \sprintf('Not passing an instance of "%s" to the "else" argument of the "%s" constructor is deprecated.', ForElseNode::class, static::class));
$else = new ForElseNode($else, $else->getTemplateLine());
}
$nodes = ['key_target' => $keyTarget, 'value_target' => $valueTarget, 'seq' => $seq, 'body' => $body];
if (null !== $else) {
$nodes['else'] = $else;
@@ -72,13 +78,7 @@ class ForNode extends Node
;
if ($this->hasNode('else')) {
$compiler
->write("if (0 === \$iterator->getIndex0()) {\n")
->indent()
->subcompile($this->getNode('else'))
->outdent()
->write("}\n")
;
$compiler->subcompile($this->getNode('else'));
}
// remove some "private" loop variables (needed for nested loops)
+2 -1
View File
@@ -13,6 +13,7 @@
namespace Twig\TokenParser;
use Twig\Node\Expression\Variable\AssignContextVariable;
use Twig\Node\ForElseNode;
use Twig\Node\ForNode;
use Twig\Node\Node;
use Twig\Token;
@@ -47,7 +48,7 @@ final class ForTokenParser extends AbstractTokenParser
$body = $this->parser->subparse($this->decideForFork(...));
if ('else' == $stream->next()->getValue()) {
$stream->expect(Token::BLOCK_END_TYPE);
$else = $this->parser->subparse($this->decideForEnd(...), true);
$else = new ForElseNode($this->parser->subparse($this->decideForEnd(...), true), $stream->getCurrent()->getLine());
} else {
$else = null;
}
+5 -2
View File
@@ -16,6 +16,7 @@ use Twig\Loader\ArrayLoader;
use Twig\Node\Expression\ConstantExpression;
use Twig\Node\Expression\Variable\AssignContextVariable;
use Twig\Node\Expression\Variable\ContextVariable;
use Twig\Node\ForElseNode;
use Twig\Node\ForNode;
use Twig\Node\IfNode;
use Twig\Node\Nodes;
@@ -43,7 +44,7 @@ class ForTest extends NodeTestCase
$this->assertEquals($body, $node->getNode('body')->getNode('tests')->getNode(1));
$this->assertFalse($node->hasNode('else'));
$else = new PrintNode(new ContextVariable('foo', 1), 1);
$else = new ForElseNode(new PrintNode(new ContextVariable('foo', 1), 1), 5);
$node = new ForNode($keyTarget, $valueTarget, $seq, null, $body, $else, 1);
$node->setAttribute('with_loop', false);
$this->assertEquals($else, $node->getNode('else'));
@@ -136,7 +137,7 @@ EOF
$seq = new ContextVariable('values', 1);
$ifexpr = new ConstantExpression(true, 1);
$body = new Nodes([new PrintNode(new ContextVariable('foo', 1), 1)], 1);
$else = new PrintNode(new ContextVariable('foo', 1), 1);
$else = new ForElseNode(new PrintNode(new ContextVariable('foo', 6), 6), 5);
$node = new ForNode($keyTarget, $valueTarget, $seq, $ifexpr, $body, $else, 1);
$node->setAttribute('with_loop', true);
@@ -152,7 +153,9 @@ yield from (\$_v1 = function (\$iterator, &\$context, \$blocks, \$recurseFunc, \
yield {$fooGetter};
}
}
// line 5
if (0 === \$iterator->getIndex0()) {
// line 6
yield {$fooGetter};
}
unset(\$context['k'], \$context['v'], \$context['loop']);