composer package updates
[openemr.git] / vendor / twig / twig / lib / Twig / Node / Module.php
blob5cd8d0502f60c9de753d9eb23532743edd6f5349
1 <?php
3 /*
4 * This file is part of Twig.
6 * (c) Fabien Potencier
7 * (c) Armin Ronacher
9 * For the full copyright and license information, please view the LICENSE
10 * file that was distributed with this source code.
13 /**
14 * Represents a module node.
16 * Consider this class as being final. If you need to customize the behavior of
17 * the generated class, consider adding nodes to the following nodes: display_start,
18 * display_end, constructor_start, constructor_end, and class_end.
20 * @author Fabien Potencier <fabien@symfony.com>
22 class Twig_Node_Module extends Twig_Node
24 private $source;
26 public function __construct(Twig_NodeInterface $body, Twig_Node_Expression $parent = null, Twig_NodeInterface $blocks, Twig_NodeInterface $macros, Twig_NodeInterface $traits, $embeddedTemplates, $name, $source = '')
28 if (!$name instanceof Twig_Source) {
29 @trigger_error(sprintf('Passing a string as the $name argument of %s() is deprecated since version 1.27. Pass a Twig_Source instance instead.', __METHOD__), E_USER_DEPRECATED);
30 $this->source = new Twig_Source($source, $name);
31 } else {
32 $this->source = $name;
35 $nodes = array(
36 'body' => $body,
37 'blocks' => $blocks,
38 'macros' => $macros,
39 'traits' => $traits,
40 'display_start' => new Twig_Node(),
41 'display_end' => new Twig_Node(),
42 'constructor_start' => new Twig_Node(),
43 'constructor_end' => new Twig_Node(),
44 'class_end' => new Twig_Node(),
46 if (null !== $parent) {
47 $nodes['parent'] = $parent;
50 // embedded templates are set as attributes so that they are only visited once by the visitors
51 parent::__construct($nodes, array(
52 // source to be remove in 2.0
53 'source' => $this->source->getCode(),
54 // filename to be remove in 2.0 (use getTemplateName() instead)
55 'filename' => $this->source->getName(),
56 'index' => null,
57 'embedded_templates' => $embeddedTemplates,
58 ), 1);
60 // populate the template name of all node children
61 $this->setTemplateName($this->source->getName());
64 public function setIndex($index)
66 $this->setAttribute('index', $index);
69 public function compile(Twig_Compiler $compiler)
71 $this->compileTemplate($compiler);
73 foreach ($this->getAttribute('embedded_templates') as $template) {
74 $compiler->subcompile($template);
78 protected function compileTemplate(Twig_Compiler $compiler)
80 if (!$this->getAttribute('index')) {
81 $compiler->write('<?php');
84 $this->compileClassHeader($compiler);
86 if (
87 count($this->getNode('blocks'))
88 || count($this->getNode('traits'))
89 || !$this->hasNode('parent')
90 || $this->getNode('parent') instanceof Twig_Node_Expression_Constant
91 || count($this->getNode('constructor_start'))
92 || count($this->getNode('constructor_end'))
93 ) {
94 $this->compileConstructor($compiler);
97 $this->compileGetParent($compiler);
99 $this->compileDisplay($compiler);
101 $compiler->subcompile($this->getNode('blocks'));
103 $this->compileMacros($compiler);
105 $this->compileGetTemplateName($compiler);
107 $this->compileIsTraitable($compiler);
109 $this->compileDebugInfo($compiler);
111 $this->compileGetSource($compiler);
113 $this->compileGetSourceContext($compiler);
115 $this->compileClassFooter($compiler);
118 protected function compileGetParent(Twig_Compiler $compiler)
120 if (!$this->hasNode('parent')) {
121 return;
123 $parent = $this->getNode('parent');
125 $compiler
126 ->write("protected function doGetParent(array \$context)\n", "{\n")
127 ->indent()
128 ->addDebugInfo($parent)
129 ->write('return ')
132 if ($parent instanceof Twig_Node_Expression_Constant) {
133 $compiler->subcompile($parent);
134 } else {
135 $compiler
136 ->raw('$this->loadTemplate(')
137 ->subcompile($parent)
138 ->raw(', ')
139 ->repr($this->source->getName())
140 ->raw(', ')
141 ->repr($parent->getTemplateLine())
142 ->raw(')')
146 $compiler
147 ->raw(";\n")
148 ->outdent()
149 ->write("}\n\n")
153 protected function compileClassHeader(Twig_Compiler $compiler)
155 $compiler
156 ->write("\n\n")
157 // if the template name contains */, add a blank to avoid a PHP parse error
158 ->write('/* '.str_replace('*/', '* /', $this->source->getName())." */\n")
159 ->write('class '.$compiler->getEnvironment()->getTemplateClass($this->source->getName(), $this->getAttribute('index')))
160 ->raw(sprintf(" extends %s\n", $compiler->getEnvironment()->getBaseTemplateClass()))
161 ->write("{\n")
162 ->indent()
166 protected function compileConstructor(Twig_Compiler $compiler)
168 $compiler
169 ->write("public function __construct(Twig_Environment \$env)\n", "{\n")
170 ->indent()
171 ->subcompile($this->getNode('constructor_start'))
172 ->write("parent::__construct(\$env);\n\n")
175 // parent
176 if (!$this->hasNode('parent')) {
177 $compiler->write("\$this->parent = false;\n\n");
178 } elseif (($parent = $this->getNode('parent')) && $parent instanceof Twig_Node_Expression_Constant) {
179 $compiler
180 ->addDebugInfo($parent)
181 ->write('$this->parent = $this->loadTemplate(')
182 ->subcompile($parent)
183 ->raw(', ')
184 ->repr($this->source->getName())
185 ->raw(', ')
186 ->repr($parent->getTemplateLine())
187 ->raw(");\n")
191 $countTraits = count($this->getNode('traits'));
192 if ($countTraits) {
193 // traits
194 foreach ($this->getNode('traits') as $i => $trait) {
195 $this->compileLoadTemplate($compiler, $trait->getNode('template'), sprintf('$_trait_%s', $i));
197 $compiler
198 ->addDebugInfo($trait->getNode('template'))
199 ->write(sprintf("if (!\$_trait_%s->isTraitable()) {\n", $i))
200 ->indent()
201 ->write("throw new Twig_Error_Runtime('Template \"'.")
202 ->subcompile($trait->getNode('template'))
203 ->raw(".'\" cannot be used as a trait.');\n")
204 ->outdent()
205 ->write("}\n")
206 ->write(sprintf("\$_trait_%s_blocks = \$_trait_%s->getBlocks();\n\n", $i, $i))
209 foreach ($trait->getNode('targets') as $key => $value) {
210 $compiler
211 ->write(sprintf('if (!isset($_trait_%s_blocks[', $i))
212 ->string($key)
213 ->raw("])) {\n")
214 ->indent()
215 ->write("throw new Twig_Error_Runtime(sprintf('Block ")
216 ->string($key)
217 ->raw(' is not defined in trait ')
218 ->subcompile($trait->getNode('template'))
219 ->raw(".'));\n")
220 ->outdent()
221 ->write("}\n\n")
223 ->write(sprintf('$_trait_%s_blocks[', $i))
224 ->subcompile($value)
225 ->raw(sprintf('] = $_trait_%s_blocks[', $i))
226 ->string($key)
227 ->raw(sprintf(']; unset($_trait_%s_blocks[', $i))
228 ->string($key)
229 ->raw("]);\n\n")
234 if ($countTraits > 1) {
235 $compiler
236 ->write("\$this->traits = array_merge(\n")
237 ->indent()
240 for ($i = 0; $i < $countTraits; ++$i) {
241 $compiler
242 ->write(sprintf('$_trait_%s_blocks'.($i == $countTraits - 1 ? '' : ',')."\n", $i))
246 $compiler
247 ->outdent()
248 ->write(");\n\n")
250 } else {
251 $compiler
252 ->write("\$this->traits = \$_trait_0_blocks;\n\n")
256 $compiler
257 ->write("\$this->blocks = array_merge(\n")
258 ->indent()
259 ->write("\$this->traits,\n")
260 ->write("array(\n")
262 } else {
263 $compiler
264 ->write("\$this->blocks = array(\n")
268 // blocks
269 $compiler
270 ->indent()
273 foreach ($this->getNode('blocks') as $name => $node) {
274 $compiler
275 ->write(sprintf("'%s' => array(\$this, 'block_%s'),\n", $name, $name))
279 if ($countTraits) {
280 $compiler
281 ->outdent()
282 ->write(")\n")
286 $compiler
287 ->outdent()
288 ->write(");\n")
289 ->outdent()
290 ->subcompile($this->getNode('constructor_end'))
291 ->write("}\n\n")
295 protected function compileDisplay(Twig_Compiler $compiler)
297 $compiler
298 ->write("protected function doDisplay(array \$context, array \$blocks = array())\n", "{\n")
299 ->indent()
300 ->subcompile($this->getNode('display_start'))
301 ->subcompile($this->getNode('body'))
304 if ($this->hasNode('parent')) {
305 $parent = $this->getNode('parent');
306 $compiler->addDebugInfo($parent);
307 if ($parent instanceof Twig_Node_Expression_Constant) {
308 $compiler->write('$this->parent');
309 } else {
310 $compiler->write('$this->getParent($context)');
312 $compiler->raw("->display(\$context, array_merge(\$this->blocks, \$blocks));\n");
315 $compiler
316 ->subcompile($this->getNode('display_end'))
317 ->outdent()
318 ->write("}\n\n")
322 protected function compileClassFooter(Twig_Compiler $compiler)
324 $compiler
325 ->subcompile($this->getNode('class_end'))
326 ->outdent()
327 ->write("}\n")
331 protected function compileMacros(Twig_Compiler $compiler)
333 $compiler->subcompile($this->getNode('macros'));
336 protected function compileGetTemplateName(Twig_Compiler $compiler)
338 $compiler
339 ->write("public function getTemplateName()\n", "{\n")
340 ->indent()
341 ->write('return ')
342 ->repr($this->source->getName())
343 ->raw(";\n")
344 ->outdent()
345 ->write("}\n\n")
349 protected function compileIsTraitable(Twig_Compiler $compiler)
351 // A template can be used as a trait if:
352 // * it has no parent
353 // * it has no macros
354 // * it has no body
356 // Put another way, a template can be used as a trait if it
357 // only contains blocks and use statements.
358 $traitable = !$this->hasNode('parent') && 0 === count($this->getNode('macros'));
359 if ($traitable) {
360 if ($this->getNode('body') instanceof Twig_Node_Body) {
361 $nodes = $this->getNode('body')->getNode(0);
362 } else {
363 $nodes = $this->getNode('body');
366 if (!count($nodes)) {
367 $nodes = new Twig_Node(array($nodes));
370 foreach ($nodes as $node) {
371 if (!count($node)) {
372 continue;
375 if ($node instanceof Twig_Node_Text && ctype_space($node->getAttribute('data'))) {
376 continue;
379 if ($node instanceof Twig_Node_BlockReference) {
380 continue;
383 $traitable = false;
384 break;
388 if ($traitable) {
389 return;
392 $compiler
393 ->write("public function isTraitable()\n", "{\n")
394 ->indent()
395 ->write(sprintf("return %s;\n", $traitable ? 'true' : 'false'))
396 ->outdent()
397 ->write("}\n\n")
401 protected function compileDebugInfo(Twig_Compiler $compiler)
403 $compiler
404 ->write("public function getDebugInfo()\n", "{\n")
405 ->indent()
406 ->write(sprintf("return %s;\n", str_replace("\n", '', var_export(array_reverse($compiler->getDebugInfo(), true), true))))
407 ->outdent()
408 ->write("}\n\n")
412 protected function compileGetSource(Twig_Compiler $compiler)
414 $compiler
415 ->write("/** @deprecated since 1.27 (to be removed in 2.0). Use getSourceContext() instead */\n")
416 ->write("public function getSource()\n", "{\n")
417 ->indent()
418 ->write("@trigger_error('The '.__METHOD__.' method is deprecated since version 1.27 and will be removed in 2.0. Use getSourceContext() instead.', E_USER_DEPRECATED);\n\n")
419 ->write('return $this->getSourceContext()->getCode();')
420 ->raw("\n")
421 ->outdent()
422 ->write("}\n\n")
426 protected function compileGetSourceContext(Twig_Compiler $compiler)
428 $compiler
429 ->write("public function getSourceContext()\n", "{\n")
430 ->indent()
431 ->write('return new Twig_Source(')
432 ->string($compiler->getEnvironment()->isDebug() ? $this->source->getCode() : '')
433 ->raw(', ')
434 ->string($this->source->getName())
435 ->raw(', ')
436 ->string($this->source->getPath())
437 ->raw(");\n")
438 ->outdent()
439 ->write("}\n")
443 protected function compileLoadTemplate(Twig_Compiler $compiler, $node, $var)
445 if ($node instanceof Twig_Node_Expression_Constant) {
446 $compiler
447 ->write(sprintf('%s = $this->loadTemplate(', $var))
448 ->subcompile($node)
449 ->raw(', ')
450 ->repr($node->getTemplateName())
451 ->raw(', ')
452 ->repr($node->getTemplateLine())
453 ->raw(");\n")
455 } else {
456 throw new LogicException('Trait templates can only be constant nodes.');
461 class_alias('Twig_Node_Module', 'Twig\Node\ModuleNode', false);