vendor/twig/twig/src/Node/IncludeNode.php line 25

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. /**
  15.  * Represents an include node.
  16.  *
  17.  * @author Fabien Potencier <fabien@symfony.com>
  18.  */
  19. class IncludeNode extends Node implements NodeOutputInterface
  20. {
  21.     public function __construct(AbstractExpression $expr, ?AbstractExpression $variablesbool $onlybool $ignoreMissingint $linenostring $tag null)
  22.     {
  23.         $nodes = ['expr' => $expr];
  24.         if (null !== $variables) {
  25.             $nodes['variables'] = $variables;
  26.         }
  27.         parent::__construct($nodes, ['only' => $only'ignore_missing' => $ignoreMissing], $lineno$tag);
  28.     }
  29.     public function compile(Compiler $compiler): void
  30.     {
  31.         $compiler->addDebugInfo($this);
  32.         if ($this->getAttribute('ignore_missing')) {
  33.             $template $compiler->getVarName();
  34.             $compiler
  35.                 ->write(sprintf("$%s = null;\n"$template))
  36.                 ->write("try {\n")
  37.                 ->indent()
  38.                 ->write(sprintf('$%s = '$template))
  39.             ;
  40.             $this->addGetTemplate($compiler);
  41.             $compiler
  42.                 ->raw(";\n")
  43.                 ->outdent()
  44.                 ->write("} catch (LoaderError \$e) {\n")
  45.                 ->indent()
  46.                 ->write("// ignore missing template\n")
  47.                 ->outdent()
  48.                 ->write("}\n")
  49.                 ->write(sprintf("if ($%s) {\n"$template))
  50.                 ->indent()
  51.                 ->write(sprintf('$%s->display('$template))
  52.             ;
  53.             $this->addTemplateArguments($compiler);
  54.             $compiler
  55.                 ->raw(");\n")
  56.                 ->outdent()
  57.                 ->write("}\n")
  58.             ;
  59.         } else {
  60.             $this->addGetTemplate($compiler);
  61.             $compiler->raw('->display(');
  62.             $this->addTemplateArguments($compiler);
  63.             $compiler->raw(");\n");
  64.         }
  65.     }
  66.     protected function addGetTemplate(Compiler $compiler)
  67.     {
  68.         $compiler
  69.             ->write('$this->loadTemplate(')
  70.             ->subcompile($this->getNode('expr'))
  71.             ->raw(', ')
  72.             ->repr($this->getTemplateName())
  73.             ->raw(', ')
  74.             ->repr($this->getTemplateLine())
  75.             ->raw(')')
  76.         ;
  77.     }
  78.     protected function addTemplateArguments(Compiler $compiler)
  79.     {
  80.         if (!$this->hasNode('variables')) {
  81.             $compiler->raw(false === $this->getAttribute('only') ? '$context' '[]');
  82.         } elseif (false === $this->getAttribute('only')) {
  83.             $compiler
  84.                 ->raw('twig_array_merge($context, ')
  85.                 ->subcompile($this->getNode('variables'))
  86.                 ->raw(')')
  87.             ;
  88.         } else {
  89.             $compiler->raw('twig_to_array(');
  90.             $compiler->subcompile($this->getNode('variables'));
  91.             $compiler->raw(')');
  92.         }
  93.     }
  94. }