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, $lineno, $tag = null)
26    {
27        parent::__construct(['expr' => $expr, 'var' => $var], [], $lineno, $tag);
28    }
29
30    public function compile(Compiler $compiler)
31    {
32        $compiler
33            ->addDebugInfo($this)
34            ->write('')
35            ->subcompile($this->getNode('var'))
36            ->raw(' = ')
37        ;
38
39        if ($this->getNode('expr') instanceof NameExpression && '_self' === $this->getNode('expr')->getAttribute('name')) {
40            $compiler->raw('$this');
41        } else {
42            $compiler
43                ->raw('$this->loadTemplate(')
44                ->subcompile($this->getNode('expr'))
45                ->raw(', ')
46                ->repr($this->getTemplateName())
47                ->raw(', ')
48                ->repr($this->getTemplateLine())
49                ->raw(')')
50            ;
51        }
52
53        $compiler->raw(";\n");
54    }
55}
56
57class_alias('Twig\Node\ImportNode', 'Twig_Node_Import');
58