Skip to content

Commit

Permalink
added more hooks in Module
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Jan 20, 2015
1 parent 3b12d55 commit 57400e2
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 32 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
@@ -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
Expand Down
64 changes: 35 additions & 29 deletions lib/Twig/Node/Module.php
Expand Up @@ -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
Expand All @@ -26,8 +30,11 @@ public function __construct(Twig_NodeInterface $body, Twig_Node_Expression $pare
'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,
Expand Down Expand Up @@ -62,17 +69,20 @@ protected function compileTemplate(Twig_Compiler $compiler)

$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'));

Expand Down Expand Up @@ -117,21 +127,6 @@ protected function compileGetParent(Twig_Compiler $compiler)
;
}

protected function compileDisplayBody(Twig_Compiler $compiler)

This comment has been minimized.

Copy link
@stof

stof Jan 28, 2015

Member

removing these protected methods is a BC break

This comment has been minimized.

Copy link
@fabpot

fabpot Jan 28, 2015

Author Contributor

Do you know anyone relying on those?

{
$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
Expand All @@ -150,6 +145,7 @@ protected function compileConstructor(Twig_Compiler $compiler)
$compiler
->write("public function __construct(Twig_Environment \$env)\n", "{\n")
->indent()
->subcompile($this->getNode('constructor_start'))
->write("parent::__construct(\$env);\n\n")
;

Expand Down Expand Up @@ -274,23 +270,32 @@ protected function compileConstructor(Twig_Compiler $compiler)
->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")
;
Expand All @@ -299,6 +304,7 @@ protected function compileDisplayFooter(Twig_Compiler $compiler)
protected function compileClassFooter(Twig_Compiler $compiler)
{
$compiler
->subcompile($this->getNode('class_end'))
->outdent()
->write("}\n")
;
Expand Down
2 changes: 1 addition & 1 deletion lib/Twig/NodeVisitor/Sandbox.php
Expand Up @@ -76,7 +76,7 @@ public function leaveNode(Twig_NodeInterface $node, Twig_Environment $env)
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;
Expand Down
4 changes: 2 additions & 2 deletions lib/Twig/Profiler/NodeVisitor/Profiler.php
Expand Up @@ -36,8 +36,8 @@ public function leaveNode(Twig_NodeInterface $node, Twig_Environment $env)
{
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(
Expand Down

0 comments on commit 57400e2

Please sign in to comment.