vendor/twig/twig/src/Node/SetNode.php line 24

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of Twig.
  4.  *
  5.  * (c) Fabien Potencier
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Twig\Node;
  11. use Twig\Compiler;
  12. use Twig\Node\Expression\ConstantExpression;
  13. /**
  14.  * Represents a set node.
  15.  *
  16.  * @author Fabien Potencier <fabien@symfony.com>
  17.  */
  18. class SetNode extends Node implements NodeCaptureInterface
  19. {
  20.     public function __construct(bool $captureNode $namesNode $valuesint $linenostring $tag null)
  21.     {
  22.         parent::__construct(['names' => $names'values' => $values], ['capture' => $capture'safe' => false], $lineno$tag);
  23.         /*
  24.          * Optimizes the node when capture is used for a large block of text.
  25.          *
  26.          * {% set foo %}foo{% endset %} is compiled to $context['foo'] = new Twig\Markup("foo");
  27.          */
  28.         if ($this->getAttribute('capture')) {
  29.             $this->setAttribute('safe'true);
  30.             $values $this->getNode('values');
  31.             if ($values instanceof TextNode) {
  32.                 $this->setNode('values', new ConstantExpression($values->getAttribute('data'), $values->getTemplateLine()));
  33.                 $this->setAttribute('capture'false);
  34.             }
  35.         }
  36.     }
  37.     public function compile(Compiler $compiler): void
  38.     {
  39.         $compiler->addDebugInfo($this);
  40.         if (\count($this->getNode('names')) > 1) {
  41.             $compiler->write('list(');
  42.             foreach ($this->getNode('names') as $idx => $node) {
  43.                 if ($idx) {
  44.                     $compiler->raw(', ');
  45.                 }
  46.                 $compiler->subcompile($node);
  47.             }
  48.             $compiler->raw(')');
  49.         } else {
  50.             if ($this->getAttribute('capture')) {
  51.                 if ($compiler->getEnvironment()->isDebug()) {
  52.                     $compiler->write("ob_start();\n");
  53.                 } else {
  54.                     $compiler->write("ob_start(function () { return ''; });\n");
  55.                 }
  56.                 $compiler
  57.                     ->subcompile($this->getNode('values'))
  58.                 ;
  59.             }
  60.             $compiler->subcompile($this->getNode('names'), false);
  61.             if ($this->getAttribute('capture')) {
  62.                 $compiler->raw(" = ('' === \$tmp = ob_get_clean()) ? '' : new Markup(\$tmp, \$this->env->getCharset())");
  63.             }
  64.         }
  65.         if (!$this->getAttribute('capture')) {
  66.             $compiler->raw(' = ');
  67.             if (\count($this->getNode('names')) > 1) {
  68.                 $compiler->write('[');
  69.                 foreach ($this->getNode('values') as $idx => $value) {
  70.                     if ($idx) {
  71.                         $compiler->raw(', ');
  72.                     }
  73.                     $compiler->subcompile($value);
  74.                 }
  75.                 $compiler->raw(']');
  76.             } else {
  77.                 if ($this->getAttribute('safe')) {
  78.                     $compiler
  79.                         ->raw("('' === \$tmp = ")
  80.                         ->subcompile($this->getNode('values'))
  81.                         ->raw(") ? '' : new Markup(\$tmp, \$this->env->getCharset())")
  82.                     ;
  83.                 } else {
  84.                     $compiler->subcompile($this->getNode('values'));
  85.                 }
  86.             }
  87.         }
  88.         $compiler->raw(";\n");
  89.     }
  90. }