1<?php
2
3/*
4 * This file is part of Twig.
5 *
6 * (c) Fabien Potencier
7 * (c) Armin Ronacher
8 *
9 * For the full copyright and license information, please view the LICENSE
10 * file that was distributed with this source code.
11 */
12
13namespace Twig\Node;
14
15use Twig\Compiler;
16use Twig\Node\Expression\AbstractExpression;
17
18/**
19 * Represents an include node.
20 *
21 * @author Fabien Potencier <fabien@symfony.com>
22 */
23class IncludeNode extends Node implements NodeOutputInterface
24{
25    public function __construct(AbstractExpression $expr, ?AbstractExpression $variables, bool $only, bool $ignoreMissing, int $lineno, string $tag = null)
26    {
27        $nodes = ['expr' => $expr];
28        if (null !== $variables) {
29            $nodes['variables'] = $variables;
30        }
31
32        parent::__construct($nodes, ['only' => (bool) $only, 'ignore_missing' => (bool) $ignoreMissing], $lineno, $tag);
33    }
34
35    public function compile(Compiler $compiler)
36    {
37        $compiler->addDebugInfo($this);
38
39        if ($this->getAttribute('ignore_missing')) {
40            $template = $compiler->getVarName();
41
42            $compiler
43                ->write(sprintf("$%s = null;\n", $template))
44                ->write("try {\n")
45                ->indent()
46                ->write(sprintf('$%s = ', $template))
47            ;
48
49            $this->addGetTemplate($compiler);
50
51            $compiler
52                ->raw(";\n")
53                ->outdent()
54                ->write("} catch (LoaderError \$e) {\n")
55                ->indent()
56                ->write("// ignore missing template\n")
57                ->outdent()
58                ->write("}\n")
59                ->write(sprintf("if ($%s) {\n", $template))
60                ->indent()
61                ->write(sprintf('$%s->display(', $template))
62            ;
63            $this->addTemplateArguments($compiler);
64            $compiler
65                ->raw(");\n")
66                ->outdent()
67                ->write("}\n")
68            ;
69        } else {
70            $this->addGetTemplate($compiler);
71            $compiler->raw('->display(');
72            $this->addTemplateArguments($compiler);
73            $compiler->raw(");\n");
74        }
75    }
76
77    protected function addGetTemplate(Compiler $compiler)
78    {
79        $compiler
80            ->write('$this->loadTemplate(')
81            ->subcompile($this->getNode('expr'))
82            ->raw(', ')
83            ->repr($this->getTemplateName())
84            ->raw(', ')
85            ->repr($this->getTemplateLine())
86            ->raw(')')
87        ;
88    }
89
90    protected function addTemplateArguments(Compiler $compiler)
91    {
92        if (!$this->hasNode('variables')) {
93            $compiler->raw(false === $this->getAttribute('only') ? '$context' : '[]');
94        } elseif (false === $this->getAttribute('only')) {
95            $compiler
96                ->raw('twig_array_merge($context, ')
97                ->subcompile($this->getNode('variables'))
98                ->raw(')')
99            ;
100        } else {
101            $compiler->raw('twig_to_array(');
102            $compiler->subcompile($this->getNode('variables'));
103            $compiler->raw(')');
104        }
105    }
106}
107
108class_alias('Twig\Node\IncludeNode', 'Twig_Node_Include');
109