vendor/twig/twig/src/Node/ForNode.php line 42

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