blob: 04addfbfe58dee357b0595ce9b88eb99c9862924 [file] [log] [blame]
Matthias Andreas Benkard12a57352021-12-28 18:02:04 +01001<?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
13namespace Twig\Node;
14
15use Twig\Compiler;
16use Twig\Node\Expression\AbstractExpression;
17use Twig\Node\Expression\AssignNameExpression;
18
19/**
20 * Represents a for node.
21 *
22 * @author Fabien Potencier <fabien@symfony.com>
23 */
24class ForNode extends Node
25{
26 private $loop;
27
28 public function __construct(AssignNameExpression $keyTarget, AssignNameExpression $valueTarget, AbstractExpression $seq, ?Node $ifexpr, Node $body, ?Node $else, int $lineno, string $tag = null)
29 {
30 $body = new Node([$body, $this->loop = new ForLoopNode($lineno, $tag)]);
31
32 $nodes = ['key_target' => $keyTarget, 'value_target' => $valueTarget, 'seq' => $seq, 'body' => $body];
33 if (null !== $else) {
34 $nodes['else'] = $else;
35 }
36
37 parent::__construct($nodes, ['with_loop' => true], $lineno, $tag);
38 }
39
40 public function compile(Compiler $compiler): void
41 {
42 $compiler
43 ->addDebugInfo($this)
44 ->write("\$context['_parent'] = \$context;\n")
45 ->write("\$context['_seq'] = twig_ensure_traversable(")
46 ->subcompile($this->getNode('seq'))
47 ->raw(");\n")
48 ;
49
50 if ($this->hasNode('else')) {
51 $compiler->write("\$context['_iterated'] = false;\n");
52 }
53
54 if ($this->getAttribute('with_loop')) {
55 $compiler
56 ->write("\$context['loop'] = [\n")
57 ->write(" 'parent' => \$context['_parent'],\n")
58 ->write(" 'index0' => 0,\n")
59 ->write(" 'index' => 1,\n")
60 ->write(" 'first' => true,\n")
61 ->write("];\n")
62 ->write("if (is_array(\$context['_seq']) || (is_object(\$context['_seq']) && \$context['_seq'] instanceof \Countable)) {\n")
63 ->indent()
64 ->write("\$length = count(\$context['_seq']);\n")
65 ->write("\$context['loop']['revindex0'] = \$length - 1;\n")
66 ->write("\$context['loop']['revindex'] = \$length;\n")
67 ->write("\$context['loop']['length'] = \$length;\n")
68 ->write("\$context['loop']['last'] = 1 === \$length;\n")
69 ->outdent()
70 ->write("}\n")
71 ;
72 }
73
74 $this->loop->setAttribute('else', $this->hasNode('else'));
75 $this->loop->setAttribute('with_loop', $this->getAttribute('with_loop'));
76
77 $compiler
78 ->write("foreach (\$context['_seq'] as ")
79 ->subcompile($this->getNode('key_target'))
80 ->raw(' => ')
81 ->subcompile($this->getNode('value_target'))
82 ->raw(") {\n")
83 ->indent()
84 ->subcompile($this->getNode('body'))
85 ->outdent()
86 ->write("}\n")
87 ;
88
89 if ($this->hasNode('else')) {
90 $compiler
91 ->write("if (!\$context['_iterated']) {\n")
92 ->indent()
93 ->subcompile($this->getNode('else'))
94 ->outdent()
95 ->write("}\n")
96 ;
97 }
98
99 $compiler->write("\$_parent = \$context['_parent'];\n");
100
101 // remove some "private" loop variables (needed for nested loops)
102 $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");
103
104 // keep the values set in the inner context for variables defined in the outer context
105 $compiler->write("\$context = array_intersect_key(\$context, \$_parent) + \$_parent;\n");
106 }
107}