1<?php
2
3/*
4 * This file is part of Twig.
5 *
6 * (c) Fabien Potencier
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Twig\Node;
13
14use Twig\Compiler;
15use Twig\Node\Expression\AbstractExpression;
16use Twig\Node\Expression\NameExpression;
17
18/**
19 * Represents an import node.
20 *
21 * @author Fabien Potencier <fabien@symfony.com>
22 */
23class ImportNode extends Node
24{
25    public function __construct(AbstractExpression $expr, AbstractExpression $var, int $lineno, string $tag = null, bool $global = true)
26    {
27        parent::__construct(['expr' => $expr, 'var' => $var], ['global' => $global], $lineno, $tag);
28    }
29
30    public function compile(Compiler $compiler)
31    {
32        $compiler
33            ->addDebugInfo($this)
34            ->write('$macros[')
35            ->repr($this->getNode('var')->getAttribute('name'))
36            ->raw('] = ')
37        ;
38
39        if ($this->getAttribute('global')) {
40            $compiler
41                ->raw('$this->macros[')
42                ->repr($this->getNode('var')->getAttribute('name'))
43                ->raw('] = ')
44            ;
45        }
46
47        if ($this->getNode('expr') instanceof NameExpression && '_self' === $this->getNode('expr')->getAttribute('name')) {
48            $compiler->raw('$this');
49        } else {
50            $compiler
51                ->raw('$this->loadTemplate(')
52                ->subcompile($this->getNode('expr'))
53                ->raw(', ')
54                ->repr($this->getTemplateName())
55                ->raw(', ')
56                ->repr($this->getTemplateLine())
57                ->raw(')->unwrap()')
58            ;
59        }
60
61        $compiler->raw(";\n");
62    }
63}
64
65class_alias('Twig\Node\ImportNode', 'Twig_Node_Import');
66