added more hooks in Module

This commit is contained in:
Fabien Potencier
2015-01-20 19:15:58 +01:00
parent 3b12d558bb
commit 57400e24b4
4 changed files with 39 additions and 32 deletions
+1
View File
@@ -1,5 +1,6 @@
* 1.18.0 (2015-XX-XX)
* added a new way to customize the main Module node (via empty nodes)
* added Twig_Environment::createTemplate() to create a template from a string
* added a profiler
* fixed filesystem loader cache when different file paths are used for the same template
+35 -29
View File
@@ -13,6 +13,10 @@
/**
* Represents a module node.
*
* Consider this class as being final. If you need to customize the behavior of
* the generated class, consider adding nodes to the following nodes: display_start,
* display_end, constructor_start, constructor_end, and class_end.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class Twig_Node_Module extends Twig_Node
@@ -26,8 +30,11 @@ class Twig_Node_Module extends Twig_Node
'blocks' => $blocks,
'macros' => $macros,
'traits' => $traits,
'display_enter' => new Twig_Node(),
'display_leave' => new Twig_Node(),
'display_start' => new Twig_Node(),
'display_end' => new Twig_Node(),
'constructor_start' => new Twig_Node(),
'constructor_end' => new Twig_Node(),
'class_end' => new Twig_Node(),
), array(
'filename' => $filename,
'index' => null,
@@ -62,17 +69,20 @@ class Twig_Node_Module extends Twig_Node
$this->compileClassHeader($compiler);
if (count($this->getNode('blocks')) || count($this->getNode('traits')) || null === $this->getNode('parent') || $this->getNode('parent') instanceof Twig_Node_Expression_Constant) {
if (
count($this->getNode('blocks'))
|| count($this->getNode('traits'))
|| null === $this->getNode('parent')
|| $this->getNode('parent') instanceof Twig_Node_Expression_Constant
|| count($this->getNode('constructor_start'))
|| count($this->getNode('constructor_end'))
) {
$this->compileConstructor($compiler);
}
$this->compileGetParent($compiler);
$this->compileDisplayHeader($compiler);
$this->compileDisplayBody($compiler);
$this->compileDisplayFooter($compiler);
$this->compileDisplay($compiler);
$compiler->subcompile($this->getNode('blocks'));
@@ -117,21 +127,6 @@ class Twig_Node_Module extends Twig_Node
;
}
protected function compileDisplayBody(Twig_Compiler $compiler)
{
$compiler->subcompile($this->getNode('body'));
if (null !== $parent = $this->getNode('parent')) {
$compiler->addDebugInfo($parent);
if ($parent instanceof Twig_Node_Expression_Constant) {
$compiler->write("\$this->parent");
} else {
$compiler->write("\$this->getParent(\$context)");
}
$compiler->raw("->display(\$context, array_merge(\$this->blocks, \$blocks));\n");
}
}
protected function compileClassHeader(Twig_Compiler $compiler)
{
$compiler
@@ -150,6 +145,7 @@ class Twig_Node_Module extends Twig_Node
$compiler
->write("public function __construct(Twig_Environment \$env)\n", "{\n")
->indent()
->subcompile($this->getNode('constructor_start'))
->write("parent::__construct(\$env);\n\n")
;
@@ -274,23 +270,32 @@ class Twig_Node_Module extends Twig_Node
->outdent()
->write(");\n")
->outdent()
->subcompile($this->getNode('constructor_end'))
->write("}\n\n")
;
}
protected function compileDisplayHeader(Twig_Compiler $compiler)
protected function compileDisplay(Twig_Compiler $compiler)
{
$compiler
->write("protected function doDisplay(array \$context, array \$blocks = array())\n", "{\n")
->indent()
->subcompile($this->getNode('display_enter'))
->subcompile($this->getNode('display_start'))
->subcompile($this->getNode('body'))
;
}
protected function compileDisplayFooter(Twig_Compiler $compiler)
{
if (null !== $parent = $this->getNode('parent')) {
$compiler->addDebugInfo($parent);
if ($parent instanceof Twig_Node_Expression_Constant) {
$compiler->write("\$this->parent");
} else {
$compiler->write("\$this->getParent(\$context)");
}
$compiler->raw("->display(\$context, array_merge(\$this->blocks, \$blocks));\n");
}
$compiler
->subcompile($this->getNode('display_leave'))
->subcompile($this->getNode('display_end'))
->outdent()
->write("}\n\n")
;
@@ -299,6 +304,7 @@ class Twig_Node_Module extends Twig_Node
protected function compileClassFooter(Twig_Compiler $compiler)
{
$compiler
->subcompile($this->getNode('class_end'))
->outdent()
->write("}\n")
;
+1 -1
View File
@@ -76,7 +76,7 @@ class Twig_NodeVisitor_Sandbox implements Twig_NodeVisitorInterface
if ($node instanceof Twig_Node_Module) {
$this->inAModule = false;
$node->setNode('display_enter', new Twig_Node(array(new Twig_Node_CheckSecurity($this->filters, $this->tags, $this->functions), $node->getNode('display_enter'))));
$node->setNode('display_start', new Twig_Node(array(new Twig_Node_CheckSecurity($this->filters, $this->tags, $this->functions), $node->getNode('display_start'))));
}
return $node;
+2 -2
View File
@@ -36,8 +36,8 @@ class Twig_Profiler_NodeVisitor_Profiler implements Twig_NodeVisitorInterface
{
if ($node instanceof Twig_Node_Module) {
$varName = $this->getVarName();
$node->setNode('display_enter', new Twig_Node(array(new Twig_Profiler_Node_EnterProfile($this->extensionName, Twig_Profiler_Profile::TEMPLATE, $node->getAttribute('filename'), $varName), $node->getNode('display_enter'))));
$node->setNode('display_leave', new Twig_Node(array(new Twig_Profiler_Node_LeaveProfile($varName), $node->getNode('display_leave'))));
$node->setNode('display_start', new Twig_Node(array(new Twig_Profiler_Node_EnterProfile($this->extensionName, Twig_Profiler_Profile::TEMPLATE, $node->getAttribute('filename'), $varName), $node->getNode('display_start'))));
$node->setNode('display_end', new Twig_Node(array(new Twig_Profiler_Node_LeaveProfile($varName), $node->getNode('display_end'))));
} elseif ($node instanceof Twig_Node_Block) {
$varName = $this->getVarName();
$node->setNode('body', new Twig_Node_Body(array(