056d99185c9a2df023110273fd89c2859386e1b2
[yaffs-website] / vendor / twig / twig / lib / Twig / Node / Expression / BlockReference.php
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
13 /**
14  * Represents a block call node.
15  *
16  * @author Fabien Potencier <fabien@symfony.com>
17  */
18 class Twig_Node_Expression_BlockReference extends Twig_Node_Expression
19 {
20     /**
21      * @param Twig_Node|null $template
22      */
23     public function __construct(Twig_NodeInterface $name, $template = null, $lineno, $tag = null)
24     {
25         if (is_bool($template)) {
26             @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);
27
28             $template = null;
29         }
30
31         $nodes = array('name' => $name);
32         if (null !== $template) {
33             $nodes['template'] = $template;
34         }
35
36         parent::__construct($nodes, array('is_defined_test' => false, 'output' => false), $lineno, $tag);
37     }
38
39     public function compile(Twig_Compiler $compiler)
40     {
41         if ($this->getAttribute('is_defined_test')) {
42             $this->compileTemplateCall($compiler, 'hasBlock');
43         } else {
44             if ($this->getAttribute('output')) {
45                 $compiler->addDebugInfo($this);
46
47                 $this
48                     ->compileTemplateCall($compiler, 'displayBlock')
49                     ->raw(";\n");
50             } else {
51                 $this->compileTemplateCall($compiler, 'renderBlock');
52             }
53         }
54     }
55
56     private function compileTemplateCall(Twig_Compiler $compiler, $method)
57     {
58         if (!$this->hasNode('template')) {
59             $compiler->write('$this');
60         } else {
61             $compiler
62                 ->write('$this->loadTemplate(')
63                 ->subcompile($this->getNode('template'))
64                 ->raw(', ')
65                 ->repr($this->getTemplateName())
66                 ->raw(', ')
67                 ->repr($this->getTemplateLine())
68                 ->raw(')')
69             ;
70         }
71
72         $compiler->raw(sprintf('->%s', $method));
73         $this->compileBlockArguments($compiler);
74
75         return $compiler;
76     }
77
78     private function compileBlockArguments(Twig_Compiler $compiler)
79     {
80         $compiler
81             ->raw('(')
82             ->subcompile($this->getNode('name'))
83             ->raw(', $context');
84
85         if (!$this->hasNode('template')) {
86             $compiler->raw(', $blocks');
87         }
88
89         return $compiler->raw(')');
90     }
91 }