Upgrade Twig library

This commit is contained in:
Michael Foster 2013-08-01 15:20:12 -04:00
parent 22f3a95e0e
commit 0fe5528574
133 changed files with 5080 additions and 1386 deletions

View file

@ -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()
;
for ($i = 0; $i < $countTraits; $i++) {
if ($countTraits > 1) {
$compiler
->write(sprintf("\$_trait_%s_blocks,\n", $i))
->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("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