1 | <?php |
---|
2 | |
---|
3 | /* |
---|
4 | * This file is part of Twig. |
---|
5 | * |
---|
6 | * (c) 2009 Fabien Potencier |
---|
7 | * (c) 2009 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 module node. |
---|
15 | * |
---|
16 | * @package twig |
---|
17 | * @author Fabien Potencier <fabien.potencier@symfony-project.com> |
---|
18 | */ |
---|
19 | class Twig_Node_Module extends Twig_Node |
---|
20 | { |
---|
21 | public function __construct(Twig_NodeInterface $body, Twig_Node_Expression $parent = null, Twig_NodeInterface $blocks, Twig_NodeInterface $macros, $filename) |
---|
22 | { |
---|
23 | parent::__construct(array('parent' => $parent, 'body' => $body, 'blocks' => $blocks, 'macros' => $macros), array('filename' => $filename), 1); |
---|
24 | } |
---|
25 | |
---|
26 | /** |
---|
27 | * Compiles the node to PHP. |
---|
28 | * |
---|
29 | * @param Twig_Compiler A Twig_Compiler instance |
---|
30 | */ |
---|
31 | public function compile(Twig_Compiler $compiler) |
---|
32 | { |
---|
33 | $this->compileTemplate($compiler); |
---|
34 | } |
---|
35 | |
---|
36 | protected function compileTemplate(Twig_Compiler $compiler) |
---|
37 | { |
---|
38 | $this->compileClassHeader($compiler); |
---|
39 | |
---|
40 | if (count($this->getNode('blocks'))) { |
---|
41 | $this->compileConstructor($compiler); |
---|
42 | } |
---|
43 | |
---|
44 | $this->compileGetParent($compiler); |
---|
45 | |
---|
46 | $this->compileDisplayHeader($compiler); |
---|
47 | |
---|
48 | $this->compileDisplayBody($compiler); |
---|
49 | |
---|
50 | $this->compileDisplayFooter($compiler); |
---|
51 | |
---|
52 | $compiler->subcompile($this->getNode('blocks')); |
---|
53 | |
---|
54 | $this->compileMacros($compiler); |
---|
55 | |
---|
56 | $this->compileGetTemplateName($compiler); |
---|
57 | |
---|
58 | $this->compileClassFooter($compiler); |
---|
59 | } |
---|
60 | |
---|
61 | protected function compileGetParent(Twig_Compiler $compiler) |
---|
62 | { |
---|
63 | if (null === $this->getNode('parent')) { |
---|
64 | return; |
---|
65 | } |
---|
66 | |
---|
67 | $compiler |
---|
68 | ->write("public function getParent(array \$context)\n", "{\n") |
---|
69 | ->indent() |
---|
70 | ->write("if (null === \$this->parent) {\n") |
---|
71 | ->indent(); |
---|
72 | ; |
---|
73 | |
---|
74 | if ($this->getNode('parent') instanceof Twig_Node_Expression_Constant) { |
---|
75 | $compiler |
---|
76 | ->write("\$this->parent = \$this->env->loadTemplate(") |
---|
77 | ->subcompile($this->getNode('parent')) |
---|
78 | ->raw(");\n") |
---|
79 | ; |
---|
80 | } else { |
---|
81 | $compiler |
---|
82 | ->write("\$this->parent = ") |
---|
83 | ->subcompile($this->getNode('parent')) |
---|
84 | ->raw(";\n") |
---|
85 | ->write("if (!\$this->parent") |
---|
86 | ->raw(" instanceof Twig_Template) {\n") |
---|
87 | ->indent() |
---|
88 | ->write("\$this->parent = \$this->env->loadTemplate(\$this->parent);\n") |
---|
89 | ->outdent() |
---|
90 | ->write("}\n") |
---|
91 | ; |
---|
92 | } |
---|
93 | |
---|
94 | $compiler |
---|
95 | ->outdent() |
---|
96 | ->write("}\n\n") |
---|
97 | ->write("return \$this->parent;\n") |
---|
98 | ->outdent() |
---|
99 | ->write("}\n\n") |
---|
100 | ; |
---|
101 | } |
---|
102 | |
---|
103 | protected function compileDisplayBody(Twig_Compiler $compiler) |
---|
104 | { |
---|
105 | $compiler->write("\$context = array_merge(\$this->env->getGlobals(), \$context);\n\n"); |
---|
106 | |
---|
107 | if (null !== $this->getNode('parent')) { |
---|
108 | // remove all output nodes |
---|
109 | foreach ($this->getNode('body') as $node) { |
---|
110 | if (!$node instanceof Twig_NodeOutputInterface) { |
---|
111 | $compiler->subcompile($node); |
---|
112 | } |
---|
113 | } |
---|
114 | |
---|
115 | $compiler |
---|
116 | ->write("\$this->getParent(\$context)->display(\$context, array_merge(\$this->blocks, \$blocks));\n") |
---|
117 | ; |
---|
118 | } else { |
---|
119 | $compiler->subcompile($this->getNode('body')); |
---|
120 | } |
---|
121 | } |
---|
122 | |
---|
123 | protected function compileClassHeader(Twig_Compiler $compiler) |
---|
124 | { |
---|
125 | $compiler |
---|
126 | ->write("<?php\n\n") |
---|
127 | // if the filename contains */, add a blank to avoid a PHP parse error |
---|
128 | ->write("/* ".str_replace('*/', '* /', $this->getAttribute('filename'))." */\n") |
---|
129 | ->write('class '.$compiler->getEnvironment()->getTemplateClass($this->getAttribute('filename'))) |
---|
130 | ->raw(sprintf(" extends %s\n", $compiler->getEnvironment()->getBaseTemplateClass())) |
---|
131 | ->write("{\n") |
---|
132 | ->indent() |
---|
133 | ; |
---|
134 | |
---|
135 | if (null !== $this->getNode('parent')) { |
---|
136 | $compiler->write("protected \$parent;\n\n"); |
---|
137 | } |
---|
138 | } |
---|
139 | |
---|
140 | protected function compileConstructor(Twig_Compiler $compiler) |
---|
141 | { |
---|
142 | $compiler |
---|
143 | ->write("public function __construct(Twig_Environment \$env)\n", "{\n") |
---|
144 | ->indent() |
---|
145 | ->write("parent::__construct(\$env);\n\n") |
---|
146 | ->write("\$this->blocks = array(\n") |
---|
147 | ->indent() |
---|
148 | ; |
---|
149 | |
---|
150 | foreach ($this->getNode('blocks') as $name => $node) { |
---|
151 | $compiler |
---|
152 | ->write(sprintf("'%s' => array(\$this, 'block_%s'),\n", $name, $name)) |
---|
153 | ; |
---|
154 | } |
---|
155 | |
---|
156 | $compiler |
---|
157 | ->outdent() |
---|
158 | ->write(");\n") |
---|
159 | ->outdent() |
---|
160 | ->write("}\n\n"); |
---|
161 | ; |
---|
162 | } |
---|
163 | |
---|
164 | protected function compileDisplayHeader(Twig_Compiler $compiler) |
---|
165 | { |
---|
166 | $compiler |
---|
167 | ->write("protected function doDisplay(array \$context, array \$blocks = array())\n", "{\n") |
---|
168 | ->indent() |
---|
169 | ; |
---|
170 | } |
---|
171 | |
---|
172 | protected function compileDisplayFooter(Twig_Compiler $compiler) |
---|
173 | { |
---|
174 | $compiler |
---|
175 | ->outdent() |
---|
176 | ->write("}\n\n") |
---|
177 | ; |
---|
178 | } |
---|
179 | |
---|
180 | protected function compileClassFooter(Twig_Compiler $compiler) |
---|
181 | { |
---|
182 | $compiler |
---|
183 | ->outdent() |
---|
184 | ->write("}\n") |
---|
185 | ; |
---|
186 | } |
---|
187 | |
---|
188 | protected function compileMacros(Twig_Compiler $compiler) |
---|
189 | { |
---|
190 | $compiler->subcompile($this->getNode('macros')); |
---|
191 | } |
---|
192 | |
---|
193 | protected function compileGetTemplateName(Twig_Compiler $compiler) |
---|
194 | { |
---|
195 | $compiler |
---|
196 | ->write("public function getTemplateName()\n", "{\n") |
---|
197 | ->indent() |
---|
198 | ->write('return ') |
---|
199 | ->repr($this->getAttribute('filename')) |
---|
200 | ->raw(";\n") |
---|
201 | ->outdent() |
---|
202 | ->write("}\n") |
---|
203 | ; |
---|
204 | } |
---|
205 | } |
---|