1<?php
2
3/*
4 * This file is part of Twig.
5 *
6 * (c) Fabien Potencier
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Twig\Node;
13
14use Twig\Compiler;
15
16/**
17 * Internal node used by the for node.
18 *
19 * @author Fabien Potencier <fabien@symfony.com>
20 */
21class ForLoopNode extends Node
22{
23    public function __construct($lineno, $tag = null)
24    {
25        parent::__construct([], ['with_loop' => false, 'ifexpr' => false, 'else' => false], $lineno, $tag);
26    }
27
28    public function compile(Compiler $compiler)
29    {
30        if ($this->getAttribute('else')) {
31            $compiler->write("\$context['_iterated'] = true;\n");
32        }
33
34        if ($this->getAttribute('with_loop')) {
35            $compiler
36                ->write("++\$context['loop']['index0'];\n")
37                ->write("++\$context['loop']['index'];\n")
38                ->write("\$context['loop']['first'] = false;\n")
39            ;
40
41            if (!$this->getAttribute('ifexpr')) {
42                $compiler
43                    ->write("if (isset(\$context['loop']['length'])) {\n")
44                    ->indent()
45                    ->write("--\$context['loop']['revindex0'];\n")
46                    ->write("--\$context['loop']['revindex'];\n")
47                    ->write("\$context['loop']['last'] = 0 === \$context['loop']['revindex0'];\n")
48                    ->outdent()
49                    ->write("}\n")
50                ;
51            }
52        }
53    }
54}
55
56class_alias('Twig\Node\ForLoopNode', 'Twig_Node_ForLoop');
57