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

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\Variable\AssignContextVariable;
  16. /**
  17.  * Represents a for node.
  18.  *
  19.  * @author Fabien Potencier <[email protected]>
  20.  */
  21. #[YieldReady]
  22. class ForNode extends Node
  23. {
  24.     private $loop;
  25.     public function __construct(AssignContextVariable $keyTargetAssignContextVariable $valueTargetAbstractExpression $seq, ?Node $ifexprNode $body, ?Node $elseint $lineno)
  26.     {
  27.         $body = new Nodes([$body$this->loop = new ForLoopNode($lineno)]);
  28.         if (null !== $ifexpr) {
  29.             trigger_deprecation('twig/twig''3.19'\sprintf('Passing not-null to the "ifexpr" argument of the "%s" constructor is deprecated.', static::class));
  30.         }
  31.         if (null !== $else && !$else instanceof ForElseNode) {
  32.             trigger_deprecation('twig/twig''3.19'\sprintf('Not passing an instance of "%s" to the "else" argument of the "%s" constructor is deprecated.'ForElseNode::class, static::class));
  33.             $else = new ForElseNode($else$else->getTemplateLine());
  34.         }
  35.         $nodes = ['key_target' => $keyTarget'value_target' => $valueTarget'seq' => $seq'body' => $body];
  36.         if (null !== $else) {
  37.             $nodes['else'] = $else;
  38.         }
  39.         parent::__construct($nodes, ['with_loop' => true], $lineno);
  40.     }
  41.     public function compile(Compiler $compiler): void
  42.     {
  43.         $compiler
  44.             ->addDebugInfo($this)
  45.             ->write("\$context['_parent'] = \$context;\n")
  46.             ->write("\$context['_seq'] = CoreExtension::ensureTraversable(")
  47.             ->subcompile($this->getNode('seq'))
  48.             ->raw(");\n")
  49.         ;
  50.         if ($this->hasNode('else')) {
  51.             $compiler->write("\$context['_iterated'] = false;\n");
  52.         }
  53.         if ($this->getAttribute('with_loop')) {
  54.             $compiler
  55.                 ->write("\$context['loop'] = [\n")
  56.                 ->write("  'parent' => \$context['_parent'],\n")
  57.                 ->write("  'index0' => 0,\n")
  58.                 ->write("  'index'  => 1,\n")
  59.                 ->write("  'first'  => true,\n")
  60.                 ->write("];\n")
  61.                 ->write("if (is_array(\$context['_seq']) || (is_object(\$context['_seq']) && \$context['_seq'] instanceof \Countable)) {\n")
  62.                 ->indent()
  63.                 ->write("\$length = count(\$context['_seq']);\n")
  64.                 ->write("\$context['loop']['revindex0'] = \$length - 1;\n")
  65.                 ->write("\$context['loop']['revindex'] = \$length;\n")
  66.                 ->write("\$context['loop']['length'] = \$length;\n")
  67.                 ->write("\$context['loop']['last'] = 1 === \$length;\n")
  68.                 ->outdent()
  69.                 ->write("}\n")
  70.             ;
  71.         }
  72.         $this->loop->setAttribute('else'$this->hasNode('else'));
  73.         $this->loop->setAttribute('with_loop'$this->getAttribute('with_loop'));
  74.         $compiler
  75.             ->write("foreach (\$context['_seq'] as ")
  76.             ->subcompile($this->getNode('key_target'))
  77.             ->raw(' => ')
  78.             ->subcompile($this->getNode('value_target'))
  79.             ->raw(") {\n")
  80.             ->indent()
  81.             ->subcompile($this->getNode('body'))
  82.             ->outdent()
  83.             ->write("}\n")
  84.         ;
  85.         if ($this->hasNode('else')) {
  86.             $compiler->subcompile($this->getNode('else'));
  87.         }
  88.         $compiler->write("\$_parent = \$context['_parent'];\n");
  89.         // remove some "private" loop variables (needed for nested loops)
  90.         $compiler->write('unset($context[\'_seq\'], $context[\''.$this->getNode('key_target')->getAttribute('name').'\'], $context[\''.$this->getNode('value_target')->getAttribute('name').'\'], $context[\'_parent\']');
  91.         if ($this->hasNode('else')) {
  92.             $compiler->raw(', $context[\'_iterated\']');
  93.         }
  94.         if ($this->getAttribute('with_loop')) {
  95.             $compiler->raw(', $context[\'loop\']');
  96.         }
  97.         $compiler->raw(");\n");
  98.         // keep the values set in the inner context for variables defined in the outer context
  99.         $compiler->write("\$context = array_intersect_key(\$context, \$_parent) + \$_parent;\n");
  100.     }
  101. }