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

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