4 * This file is part of Twig.
9 * For the full copyright and license information, please view the LICENSE
10 * file that was distributed with this source code.
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
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);
32 $this->source = $name;
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(),
57 'embedded_templates' => $embeddedTemplates,
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);
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'))
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')) {
123 $parent = $this->getNode('parent');
126 ->write("protected function doGetParent(array \$context)\n", "{\n")
128 ->addDebugInfo($parent)
132 if ($parent instanceof Twig_Node_Expression_Constant) {
133 $compiler->subcompile($parent);
136 ->raw('$this->loadTemplate(')
137 ->subcompile($parent)
139 ->repr($this->source->getName())
141 ->repr($parent->getTemplateLine())
153 protected function compileClassHeader(Twig_Compiler $compiler)
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()))
166 protected function compileConstructor(Twig_Compiler $compiler)
169 ->write("public function __construct(Twig_Environment \$env)\n", "{\n")
171 ->subcompile($this->getNode('constructor_start'))
172 ->write("parent::__construct(\$env);\n\n")
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) {
180 ->addDebugInfo($parent)
181 ->write('$this->parent = $this->loadTemplate(')
182 ->subcompile($parent)
184 ->repr($this->source->getName())
186 ->repr($parent->getTemplateLine())
191 $countTraits = count($this->getNode('traits'));
194 foreach ($this->getNode('traits') as $i => $trait) {
195 $this->compileLoadTemplate($compiler, $trait->getNode('template'), sprintf('$_trait_%s', $i));
198 ->addDebugInfo($trait->getNode('template'))
199 ->write(sprintf("if (!\$_trait_%s->isTraitable()) {\n", $i))
201 ->write("throw new Twig_Error_Runtime('Template \"'.")
202 ->subcompile($trait->getNode('template'))
203 ->raw(".'\" cannot be used as a trait.');\n")
206 ->write(sprintf("\$_trait_%s_blocks = \$_trait_%s->getBlocks();\n\n", $i, $i))
209 foreach ($trait->getNode('targets') as $key => $value) {
211 ->write(sprintf('if (!isset($_trait_%s_blocks[', $i))
215 ->write("throw new Twig_Error_Runtime(sprintf('Block ")
217 ->raw(' is not defined in trait ')
218 ->subcompile($trait->getNode('template'))
223 ->write(sprintf('$_trait_%s_blocks[', $i))
225 ->raw(sprintf('] = $_trait_%s_blocks[', $i))
227 ->raw(sprintf(']; unset($_trait_%s_blocks[', $i))
234 if ($countTraits > 1) {
236 ->write("\$this->traits = array_merge(\n")
240 for ($i = 0; $i < $countTraits; ++$i) {
242 ->write(sprintf('$_trait_%s_blocks'.($i == $countTraits - 1 ? '' : ',')."\n", $i))
252 ->write("\$this->traits = \$_trait_0_blocks;\n\n")
257 ->write("\$this->blocks = array_merge(\n")
259 ->write("\$this->traits,\n")
264 ->write("\$this->blocks = array(\n")
273 foreach ($this->getNode('blocks') as $name => $node) {
275 ->write(sprintf("'%s' => array(\$this, 'block_%s'),\n", $name, $name))
290 ->subcompile($this->getNode('constructor_end'))
295 protected function compileDisplay(Twig_Compiler $compiler)
298 ->write("protected function doDisplay(array \$context, array \$blocks = array())\n", "{\n")
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');
310 $compiler->write('$this->getParent($context)');
312 $compiler->raw("->display(\$context, array_merge(\$this->blocks, \$blocks));\n");
316 ->subcompile($this->getNode('display_end'))
322 protected function compileClassFooter(Twig_Compiler $compiler)
325 ->subcompile($this->getNode('class_end'))
331 protected function compileMacros(Twig_Compiler $compiler)
333 $compiler->subcompile($this->getNode('macros'));
336 protected function compileGetTemplateName(Twig_Compiler $compiler)
339 ->write("public function getTemplateName()\n", "{\n")
342 ->repr($this->source->getName())
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
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'));
360 if ($this->getNode('body') instanceof Twig_Node_Body) {
361 $nodes = $this->getNode('body')->getNode(0);
363 $nodes = $this->getNode('body');
366 if (!count($nodes)) {
367 $nodes = new Twig_Node(array($nodes));
370 foreach ($nodes as $node) {
375 if ($node instanceof Twig_Node_Text && ctype_space($node->getAttribute('data'))) {
379 if ($node instanceof Twig_Node_BlockReference) {
393 ->write("public function isTraitable()\n", "{\n")
395 ->write(sprintf("return %s;\n", $traitable ? 'true' : 'false'))
401 protected function compileDebugInfo(Twig_Compiler $compiler)
404 ->write("public function getDebugInfo()\n", "{\n")
406 ->write(sprintf("return %s;\n", str_replace("\n", '', var_export(array_reverse($compiler->getDebugInfo(), true), true))))
412 protected function compileGetSource(Twig_Compiler $compiler)
415 ->write("/** @deprecated since 1.27 (to be removed in 2.0). Use getSourceContext() instead */\n")
416 ->write("public function getSource()\n", "{\n")
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();')
426 protected function compileGetSourceContext(Twig_Compiler $compiler)
429 ->write("public function getSourceContext()\n", "{\n")
431 ->write('return new Twig_Source(')
432 ->string($compiler->getEnvironment()->isDebug() ? $this->source->getCode() : '')
434 ->string($this->source->getName())
436 ->string($this->source->getPath())
443 protected function compileLoadTemplate(Twig_Compiler $compiler, $node, $var)
445 if ($node instanceof Twig_Node_Expression_Constant) {
447 ->write(sprintf('%s = $this->loadTemplate(', $var))
450 ->repr($node->getTemplateName())
452 ->repr($node->getTemplateLine())
456 throw new LogicException('Trait templates can only be constant nodes.');
461 class_alias('Twig_Node_Module', 'Twig\Node\ModuleNode', false);