diff options
| author | czaks <marcin@6irc.net> | 2013-08-01 23:43:20 -0400 |
|---|---|---|
| committer | czaks <marcin@6irc.net> | 2013-08-01 23:43:20 -0400 |
| commit | 1ed45ebe9e39724ec70098abfcd267d8ac478511 (patch) | |
| tree | da133b7426dedaf0785f64be752aa0437de7e80a /inc/lib/Twig/Node/Module.php | |
| parent | dc84ccc6a8e0b6fd3fc626eb1f321e4cf2d05511 (diff) | |
| parent | 40ab2e0c1728fab613bc16a19ab8fcb3fa78c1d4 (diff) | |
Merge branch 'master' of https://github.com/savetheinternet/Tinyboard
Conflicts:
stylesheets/dark.css
stylesheets/futaba+vichan.css
stylesheets/miku.css
stylesheets/stripes.css
stylesheets/testorange.css
stylesheets/wasabi.css
Diffstat (limited to 'inc/lib/Twig/Node/Module.php')
| -rw-r--r-- | inc/lib/Twig/Node/Module.php | 127 |
1 files changed, 98 insertions, 29 deletions
diff --git a/inc/lib/Twig/Node/Module.php b/inc/lib/Twig/Node/Module.php index 9b8c55e3..585048b8 100644 --- a/inc/lib/Twig/Node/Module.php +++ b/inc/lib/Twig/Node/Module.php @@ -13,14 +13,19 @@ /** * Represents a module node. * - * @package twig - * @author Fabien Potencier <fabien@symfony.com> + * @author Fabien Potencier <fabien@symfony.com> */ class Twig_Node_Module extends Twig_Node { - public function __construct(Twig_NodeInterface $body, Twig_Node_Expression $parent = null, Twig_NodeInterface $blocks, Twig_NodeInterface $macros, Twig_NodeInterface $traits, $filename) + public function __construct(Twig_NodeInterface $body, Twig_Node_Expression $parent = null, Twig_NodeInterface $blocks, Twig_NodeInterface $macros, Twig_NodeInterface $traits, $embeddedTemplates, $filename) { - parent::__construct(array('parent' => $parent, 'body' => $body, 'blocks' => $blocks, 'macros' => $macros, 'traits' => $traits), array('filename' => $filename), 1); + // embedded templates are set as attributes so that they are only visited once by the visitors + parent::__construct(array('parent' => $parent, 'body' => $body, 'blocks' => $blocks, 'macros' => $macros, 'traits' => $traits), array('filename' => $filename, 'index' => null, 'embedded_templates' => $embeddedTemplates), 1); + } + + public function setIndex($index) + { + $this->setAttribute('index', $index); } /** @@ -31,13 +36,21 @@ class Twig_Node_Module extends Twig_Node public function compile(Twig_Compiler $compiler) { $this->compileTemplate($compiler); + + foreach ($this->getAttribute('embedded_templates') as $template) { + $compiler->subcompile($template); + } } protected function compileTemplate(Twig_Compiler $compiler) { + if (!$this->getAttribute('index')) { + $compiler->write('<?php'); + } + $this->compileClassHeader($compiler); - if (count($this->getNode('blocks')) || count($this->getNode('traits'))) { + if (count($this->getNode('blocks')) || count($this->getNode('traits')) || null === $this->getNode('parent') || $this->getNode('parent') instanceof Twig_Node_Expression_Constant) { $this->compileConstructor($compiler); } @@ -57,29 +70,31 @@ class Twig_Node_Module extends Twig_Node $this->compileIsTraitable($compiler); + $this->compileDebugInfo($compiler); + $this->compileClassFooter($compiler); } protected function compileGetParent(Twig_Compiler $compiler) { + if (null === $this->getNode('parent')) { + return; + } + $compiler ->write("protected function doGetParent(array \$context)\n", "{\n") ->indent() ->write("return ") ; - if (null === $this->getNode('parent')) { - $compiler->raw("false"); + if ($this->getNode('parent') instanceof Twig_Node_Expression_Constant) { + $compiler->subcompile($this->getNode('parent')); } else { - if ($this->getNode('parent') instanceof Twig_Node_Expression_Constant) { - $compiler->subcompile($this->getNode('parent')); - } else { - $compiler - ->raw("\$this->env->resolveTemplate(") - ->subcompile($this->getNode('parent')) - ->raw(")") - ; - } + $compiler + ->raw("\$this->env->resolveTemplate(") + ->subcompile($this->getNode('parent')) + ->raw(")") + ; } $compiler @@ -91,21 +106,25 @@ class Twig_Node_Module extends Twig_Node protected function compileDisplayBody(Twig_Compiler $compiler) { - $compiler->write("\$context = array_merge(\$this->env->getGlobals(), \$context);\n\n"); $compiler->subcompile($this->getNode('body')); if (null !== $this->getNode('parent')) { - $compiler->write("\$this->getParent(\$context)->display(\$context, array_merge(\$this->blocks, \$blocks));\n"); + if ($this->getNode('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 - ->write("<?php\n\n") + ->write("\n\n") // if the filename contains */, add a blank to avoid a PHP parse error ->write("/* ".str_replace('*/', '* /', $this->getAttribute('filename'))." */\n") - ->write('class '.$compiler->getEnvironment()->getTemplateClass($this->getAttribute('filename'))) + ->write('class '.$compiler->getEnvironment()->getTemplateClass($this->getAttribute('filename'), $this->getAttribute('index'))) ->raw(sprintf(" extends %s\n", $compiler->getEnvironment()->getBaseTemplateClass())) ->write("{\n") ->indent() @@ -120,6 +139,17 @@ class Twig_Node_Module extends Twig_Node ->write("parent::__construct(\$env);\n\n") ; + // parent + if (null === $this->getNode('parent')) { + $compiler->write("\$this->parent = false;\n\n"); + } elseif ($this->getNode('parent') instanceof Twig_Node_Expression_Constant) { + $compiler + ->write("\$this->parent = \$this->env->loadTemplate(") + ->subcompile($this->getNode('parent')) + ->raw(");\n\n") + ; + } + $countTraits = count($this->getNode('traits')); if ($countTraits) { // traits @@ -151,18 +181,32 @@ class Twig_Node_Module extends Twig_Node } } - $compiler - ->write("\$this->blocks = array_merge(\n") - ->indent() - ; + 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)) + ; + } - for ($i = 0; $i < $countTraits; $i++) { $compiler - ->write(sprintf("\$_trait_%s_blocks,\n", $i)) + ->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("array(\n") ; } else { @@ -250,11 +294,21 @@ class Twig_Node_Module extends Twig_Node // only contains blocks and use statements. $traitable = null === $this->getNode('parent') && 0 === count($this->getNode('macros')); if ($traitable) { - if (!count($nodes = $this->getNode('body'))) { - $nodes = new Twig_Node(array($this->getNode('body'))); + if ($this->getNode('body') instanceof Twig_Node_Body) { + $nodes = $this->getNode('body')->getNode(0); + } else { + $nodes = $this->getNode('body'); + } + + if (!count($nodes)) { + $nodes = new Twig_Node(array($nodes)); } foreach ($nodes as $node) { + if (!count($node)) { + continue; + } + if ($node instanceof Twig_Node_Text && ctype_space($node->getAttribute('data'))) { continue; } @@ -268,16 +322,31 @@ class Twig_Node_Module extends Twig_Node } } + if ($traitable) { + return; + } + $compiler ->write("public function isTraitable()\n", "{\n") ->indent() ->write(sprintf("return %s;\n", $traitable ? 'true' : 'false')) ->outdent() + ->write("}\n\n") + ; + } + + protected function compileDebugInfo(Twig_Compiler $compiler) + { + $compiler + ->write("public function getDebugInfo()\n", "{\n") + ->indent() + ->write(sprintf("return %s;\n", str_replace("\n", '', var_export(array_reverse($compiler->getDebugInfo(), true), true)))) + ->outdent() ->write("}\n") ; } - public function compileLoadTemplate(Twig_Compiler $compiler, $node, $var) + protected function compileLoadTemplate(Twig_Compiler $compiler, $node, $var) { if ($node instanceof Twig_Node_Expression_Constant) { $compiler |
