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\Expression;
14
15use Twig\Compiler;
16use Twig\Node\Node;
17
18/**
19 * Represents a block call node.
20 *
21 * @author Fabien Potencier <fabien@symfony.com>
22 */
23class BlockReferenceExpression extends AbstractExpression
24{
25    /**
26     * @param Node|null $template
27     */
28    public function __construct(\Twig_NodeInterface $name, $template = null, $lineno, $tag = null)
29    {
30        if (\is_bool($template)) {
31            @trigger_error(sprintf('The %s method "$asString" argument is deprecated since version 1.28 and will be removed in 2.0.', __METHOD__), E_USER_DEPRECATED);
32
33            $template = null;
34        }
35
36        $nodes = ['name' => $name];
37        if (null !== $template) {
38            $nodes['template'] = $template;
39        }
40
41        parent::__construct($nodes, ['is_defined_test' => false, 'output' => false], $lineno, $tag);
42    }
43
44    public function compile(Compiler $compiler)
45    {
46        if ($this->getAttribute('is_defined_test')) {
47            $this->compileTemplateCall($compiler, 'hasBlock');
48        } else {
49            if ($this->getAttribute('output')) {
50                $compiler->addDebugInfo($this);
51
52                $this
53                    ->compileTemplateCall($compiler, 'displayBlock')
54                    ->raw(";\n");
55            } else {
56                $this->compileTemplateCall($compiler, 'renderBlock');
57            }
58        }
59    }
60
61    private function compileTemplateCall(Compiler $compiler, $method)
62    {
63        if (!$this->hasNode('template')) {
64            $compiler->write('$this');
65        } else {
66            $compiler
67                ->write('$this->loadTemplate(')
68                ->subcompile($this->getNode('template'))
69                ->raw(', ')
70                ->repr($this->getTemplateName())
71                ->raw(', ')
72                ->repr($this->getTemplateLine())
73                ->raw(')')
74            ;
75        }
76
77        $compiler->raw(sprintf('->%s', $method));
78        $this->compileBlockArguments($compiler);
79
80        return $compiler;
81    }
82
83    private function compileBlockArguments(Compiler $compiler)
84    {
85        $compiler
86            ->raw('(')
87            ->subcompile($this->getNode('name'))
88            ->raw(', $context');
89
90        if (!$this->hasNode('template')) {
91            $compiler->raw(', $blocks');
92        }
93
94        return $compiler->raw(')');
95    }
96}
97
98class_alias('Twig\Node\Expression\BlockReferenceExpression', 'Twig_Node_Expression_BlockReference');
99