Matthias Andreas Benkard | 12a5735 | 2021-12-28 18:02:04 +0100 | [diff] [blame^] | 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 | |
| 12 | namespace Twig\Node; |
| 13 | |
| 14 | use Twig\Compiler; |
| 15 | |
| 16 | /** |
| 17 | * Internal node used by the for node. |
| 18 | * |
| 19 | * @author Fabien Potencier <fabien@symfony.com> |
| 20 | */ |
| 21 | class ForLoopNode extends Node |
| 22 | { |
| 23 | public function __construct(int $lineno, string $tag = null) |
| 24 | { |
| 25 | parent::__construct([], ['with_loop' => false, 'ifexpr' => false, 'else' => false], $lineno, $tag); |
| 26 | } |
| 27 | |
| 28 | public function compile(Compiler $compiler): void |
| 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 | ->write("if (isset(\$context['loop']['length'])) {\n") |
| 40 | ->indent() |
| 41 | ->write("--\$context['loop']['revindex0'];\n") |
| 42 | ->write("--\$context['loop']['revindex'];\n") |
| 43 | ->write("\$context['loop']['last'] = 0 === \$context['loop']['revindex0'];\n") |
| 44 | ->outdent() |
| 45 | ->write("}\n") |
| 46 | ; |
| 47 | } |
| 48 | } |
| 49 | } |