function ModuleNode::compileConstructor
1 call to ModuleNode::compileConstructor()
- ModuleNode::compileTemplate in vendor/
twig/ twig/ src/ Node/ ModuleNode.php
File
-
vendor/
twig/ twig/ src/ Node/ ModuleNode.php, line 183
Class
- ModuleNode
- Represents a module node.
Namespace
Twig\NodeCode
protected function compileConstructor(Compiler $compiler) {
$compiler->write("public function __construct(Environment \$env)\n", "{\n")
->indent()
->subcompile($this->getNode('constructor_start'))
->write("parent::__construct(\$env);\n\n")
->write("\$this->source = \$this->getSourceContext();\n\n");
// parent
if (!$this->hasNode('parent')) {
$compiler->write("\$this->parent = false;\n\n");
}
$countTraits = \count($this->getNode('traits'));
if ($countTraits) {
// traits
foreach ($this->getNode('traits') as $i => $trait) {
$node = $trait->getNode('template');
$compiler->addDebugInfo($node)
->write(\sprintf('$_trait_%s = $this->loadTemplate(', $i))
->subcompile($node)
->raw(', ')
->repr($node->getTemplateName())
->raw(', ')
->repr($node->getTemplateLine())
->raw(");\n")
->write(\sprintf("if (!\$_trait_%s->unwrap()->isTraitable()) {\n", $i))
->indent()
->write("throw new RuntimeError('Template \"'.")
->subcompile($trait->getNode('template'))
->raw(".'\" cannot be used as a trait.', ")
->repr($node->getTemplateLine())
->raw(", \$this->source);\n")
->outdent()
->write("}\n")
->write(\sprintf("\$_trait_%s_blocks = \$_trait_%s->unwrap()->getBlocks();\n\n", $i, $i));
foreach ($trait->getNode('targets') as $key => $value) {
$compiler->write(\sprintf('if (!isset($_trait_%s_blocks[', $i))
->string($key)
->raw("])) {\n")
->indent()
->write("throw new RuntimeError('Block ")
->string($key)
->raw(' is not defined in trait ')
->subcompile($trait->getNode('template'))
->raw(".', ")
->repr($node->getTemplateLine())
->raw(", \$this->source);\n")
->outdent()
->write("}\n\n")
->write(\sprintf('$_trait_%s_blocks[', $i))
->subcompile($value)
->raw(\sprintf('] = $_trait_%s_blocks[', $i))
->string($key)
->raw(\sprintf(']; unset($_trait_%s_blocks[', $i))
->string($key)
->raw("]); \$this->traitAliases[")
->subcompile($value)
->raw("] = ")
->string($key)
->raw(";\n\n");
}
}
if ($countTraits > 1) {
$compiler->write("\$this->traits = array_merge(\n")
->indent();
for ($i = 0; $i < $countTraits; ++$i) {
$compiler->write(\sprintf('$_trait_%s_blocks' . ($i == $countTraits - 1 ? '' : ',') . "\n", $i));
}
$compiler->outdent()
->write(");\n\n");
}
else {
$compiler->write("\$this->traits = \$_trait_0_blocks;\n\n");
}
$compiler->write("\$this->blocks = array_merge(\n")
->indent()
->write("\$this->traits,\n")
->write("[\n");
}
else {
$compiler->write("\$this->blocks = [\n");
}
// blocks
$compiler->indent();
foreach ($this->getNode('blocks') as $name => $node) {
$compiler->write(\sprintf("'%s' => [\$this, 'block_%s'],\n", $name, $name));
}
if ($countTraits) {
$compiler->outdent()
->write("]\n")
->outdent()
->write(");\n");
}
else {
$compiler->outdent()
->write("];\n");
}
$compiler->subcompile($this->getNode('constructor_end'))
->outdent()
->write("}\n\n");
}