| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of Twig. |
| 5 |
* |
| 6 |
* (c) Fabien Potencier |
| 7 |
* (c) Armin Ronacher |
| 8 |
* |
| 9 |
* For the full copyright and license information, please view the LICENSE |
| 10 |
* file that was distributed with this source code. |
| 11 |
*/ |
| 12 |
namespace ElementorDeps\Twig\Node; |
| 13 |
|
| 14 |
use ElementorDeps\Twig\Attribute\YieldReady; |
| 15 |
use ElementorDeps\Twig\Compiler; |
| 16 |
use ElementorDeps\Twig\Node\Expression\AbstractExpression; |
| 17 |
use ElementorDeps\Twig\Node\Expression\AssignNameExpression; |
| 18 |
/** |
| 19 |
* Represents a for node. |
| 20 |
* |
| 21 |
* @author Fabien Potencier <fabien@symfony.com> |
| 22 |
*/ |
| 23 |
#[\Twig\Attribute\YieldReady] |
| 24 |
class ForNode extends Node |
| 25 |
{ |
| 26 |
private $loop; |
| 27 |
public function __construct(AssignNameExpression $keyTarget, AssignNameExpression $valueTarget, AbstractExpression $seq, ?Node $ifexpr, Node $body, ?Node $else, int $lineno, ?string $tag = null) |
| 28 |
{ |
| 29 |
$body = new Node([$body, $this->loop = new ForLoopNode($lineno, $tag)]); |
| 30 |
$nodes = ['key_target' => $keyTarget, 'value_target' => $valueTarget, 'seq' => $seq, 'body' => $body]; |
| 31 |
if (null !== $else) { |
| 32 |
$nodes['else'] = $else; |
| 33 |
} |
| 34 |
parent::__construct($nodes, ['with_loop' => \true], $lineno, $tag); |
| 35 |
} |
| 36 |
public function compile(Compiler $compiler) : void |
| 37 |
{ |
| 38 |
$compiler->addDebugInfo($this)->write("\$context['_parent'] = \$context;\n")->write("\$context['_seq'] = CoreExtension::ensureTraversable(")->subcompile($this->getNode('seq'))->raw(");\n"); |
| 39 |
if ($this->hasNode('else')) { |
| 40 |
$compiler->write("\$context['_iterated'] = false;\n"); |
| 41 |
} |
| 42 |
if ($this->getAttribute('with_loop')) { |
| 43 |
$compiler->write("\$context['loop'] = [\n")->write(" 'parent' => \$context['_parent'],\n")->write(" 'index0' => 0,\n")->write(" 'index' => 1,\n")->write(" 'first' => true,\n")->write("];\n")->write("if (is_array(\$context['_seq']) || (is_object(\$context['_seq']) && \$context['_seq'] instanceof \\Countable)) {\n")->indent()->write("\$length = count(\$context['_seq']);\n")->write("\$context['loop']['revindex0'] = \$length - 1;\n")->write("\$context['loop']['revindex'] = \$length;\n")->write("\$context['loop']['length'] = \$length;\n")->write("\$context['loop']['last'] = 1 === \$length;\n")->outdent()->write("}\n"); |
| 44 |
} |
| 45 |
$this->loop->setAttribute('else', $this->hasNode('else')); |
| 46 |
$this->loop->setAttribute('with_loop', $this->getAttribute('with_loop')); |
| 47 |
$compiler->write("foreach (\$context['_seq'] as ")->subcompile($this->getNode('key_target'))->raw(' => ')->subcompile($this->getNode('value_target'))->raw(") {\n")->indent()->subcompile($this->getNode('body'))->outdent()->write("}\n"); |
| 48 |
if ($this->hasNode('else')) { |
| 49 |
$compiler->write("if (!\$context['_iterated']) {\n")->indent()->subcompile($this->getNode('else'))->outdent()->write("}\n"); |
| 50 |
} |
| 51 |
$compiler->write("\$_parent = \$context['_parent'];\n"); |
| 52 |
// remove some "private" loop variables (needed for nested loops) |
| 53 |
$compiler->write('unset($context[\'_seq\'], $context[\'_iterated\'], $context[\'' . $this->getNode('key_target')->getAttribute('name') . '\'], $context[\'' . $this->getNode('value_target')->getAttribute('name') . '\'], $context[\'_parent\'], $context[\'loop\']);' . "\n"); |
| 54 |
// keep the values set in the inner context for variables defined in the outer context |
| 55 |
$compiler->write("\$context = array_intersect_key(\$context, \$_parent) + \$_parent;\n"); |
| 56 |
} |
| 57 |
} |
| 58 |
|