Twig update to latest Twig 1.x legacy as per vichan

This commit is contained in:
Benjamin Southall 2019-02-26 10:11:12 +10:00
parent 4ecd84f81d
commit e6c07544da
198 changed files with 6150 additions and 2506 deletions

View file

@ -3,8 +3,8 @@
/*
* This file is part of Twig.
*
* (c) 2009 Fabien Potencier
* (c) 2009 Armin Ronacher
* (c) Fabien Potencier
* (c) Armin Ronacher
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
@ -13,14 +13,52 @@
/**
* 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
{
public function __construct(Twig_NodeInterface $body, Twig_Node_Expression $parent = null, Twig_NodeInterface $blocks, Twig_NodeInterface $macros, Twig_NodeInterface $traits, $embeddedTemplates, $filename)
private $source;
public function __construct(Twig_NodeInterface $body, Twig_Node_Expression $parent = null, Twig_NodeInterface $blocks, Twig_NodeInterface $macros, Twig_NodeInterface $traits, $embeddedTemplates, $name, $source = '')
{
if (!$name instanceof Twig_Source) {
@trigger_error(sprintf('Passing a string as the $name argument of %s() is deprecated since version 1.27. Pass a Twig_Source instance instead.', __METHOD__), E_USER_DEPRECATED);
$this->source = new Twig_Source($source, $name);
} else {
$this->source = $name;
}
$nodes = array(
'body' => $body,
'blocks' => $blocks,
'macros' => $macros,
'traits' => $traits,
'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(),
);
if (null !== $parent) {
$nodes['parent'] = $parent;
}
// 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);
parent::__construct($nodes, array(
// source to be remove in 2.0
'source' => $this->source->getCode(),
// filename to be remove in 2.0 (use getTemplateName() instead)
'filename' => $this->source->getName(),
'index' => null,
'embedded_templates' => $embeddedTemplates,
), 1);
// populate the template name of all node children
$this->setTemplateName($this->source->getName());
}
public function setIndex($index)
@ -28,11 +66,6 @@ class Twig_Node_Module extends Twig_Node
$this->setAttribute('index', $index);
}
/**
* Compiles the node to PHP.
*
* @param Twig_Compiler A Twig_Compiler instance
*/
public function compile(Twig_Compiler $compiler)
{
$this->compileTemplate($compiler);
@ -50,17 +83,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'))
|| !$this->hasNode('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'));
@ -72,28 +108,38 @@ class Twig_Node_Module extends Twig_Node
$this->compileDebugInfo($compiler);
$this->compileGetSource($compiler);
$this->compileGetSourceContext($compiler);
$this->compileClassFooter($compiler);
}
protected function compileGetParent(Twig_Compiler $compiler)
{
if (null === $this->getNode('parent')) {
if (!$this->hasNode('parent')) {
return;
}
$parent = $this->getNode('parent');
$compiler
->write("protected function doGetParent(array \$context)\n", "{\n")
->indent()
->write("return ")
->addDebugInfo($parent)
->write('return ')
;
if ($this->getNode('parent') instanceof Twig_Node_Expression_Constant) {
$compiler->subcompile($this->getNode('parent'));
if ($parent instanceof Twig_Node_Expression_Constant) {
$compiler->subcompile($parent);
} else {
$compiler
->raw("\$this->env->resolveTemplate(")
->subcompile($this->getNode('parent'))
->raw(")")
->raw('$this->loadTemplate(')
->subcompile($parent)
->raw(', ')
->repr($this->source->getName())
->raw(', ')
->repr($parent->getTemplateLine())
->raw(')')
;
}
@ -104,27 +150,13 @@ class Twig_Node_Module extends Twig_Node
;
}
protected function compileDisplayBody(Twig_Compiler $compiler)
{
$compiler->subcompile($this->getNode('body'));
if (null !== $this->getNode('parent')) {
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("\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'), $this->getAttribute('index')))
// if the template name contains */, add a blank to avoid a PHP parse error
->write('/* '.str_replace('*/', '* /', $this->source->getName())." */\n")
->write('class '.$compiler->getEnvironment()->getTemplateClass($this->source->getName(), $this->getAttribute('index')))
->raw(sprintf(" extends %s\n", $compiler->getEnvironment()->getBaseTemplateClass()))
->write("{\n")
->indent()
@ -136,17 +168,23 @@ 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")
;
// parent
if (null === $this->getNode('parent')) {
if (!$this->hasNode('parent')) {
$compiler->write("\$this->parent = false;\n\n");
} elseif ($this->getNode('parent') instanceof Twig_Node_Expression_Constant) {
} elseif (($parent = $this->getNode('parent')) && $parent instanceof Twig_Node_Expression_Constant) {
$compiler
->write("\$this->parent = \$this->env->loadTemplate(")
->subcompile($this->getNode('parent'))
->raw(");\n\n")
->addDebugInfo($parent)
->write('$this->parent = $this->loadTemplate(')
->subcompile($parent)
->raw(', ')
->repr($this->source->getName())
->raw(', ')
->repr($parent->getTemplateLine())
->raw(");\n")
;
}
@ -170,11 +208,23 @@ class Twig_Node_Module extends Twig_Node
foreach ($trait->getNode('targets') as $key => $value) {
$compiler
->write(sprintf("\$_trait_%s_blocks[", $i))
->subcompile($value)
->raw(sprintf("] = \$_trait_%s_blocks[", $i))
->write(sprintf('if (!isset($_trait_%s_blocks[', $i))
->string($key)
->raw(sprintf("]; unset(\$_trait_%s_blocks[", $i))
->raw("])) {\n")
->indent()
->write("throw new Twig_Error_Runtime(sprintf('Block ")
->string($key)
->raw(' is not defined in trait ')
->subcompile($trait->getNode('template'))
->raw(".'));\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("]);\n\n")
;
@ -187,9 +237,9 @@ class Twig_Node_Module extends Twig_Node
->indent()
;
for ($i = 0; $i < $countTraits; $i++) {
for ($i = 0; $i < $countTraits; ++$i) {
$compiler
->write(sprintf("\$_trait_%s_blocks".($i == $countTraits - 1 ? '' : ',')."\n", $i))
->write(sprintf('$_trait_%s_blocks'.($i == $countTraits - 1 ? '' : ',')."\n", $i))
;
}
@ -233,57 +283,37 @@ class Twig_Node_Module extends Twig_Node
;
}
$compiler
->outdent()
->write(");\n\n")
;
// macro information
$compiler
->write("\$this->macros = array(\n")
->indent()
;
foreach ($this->getNode('macros') as $name => $node) {
$compiler
->addIndentation()->repr($name)->raw(" => array(\n")
->indent()
->write("'method' => ")->repr($node->getAttribute('method'))->raw(",\n")
->write("'arguments' => array(\n")
->indent()
;
foreach ($node->getNode('arguments') as $argument => $value) {
$compiler->addIndentation()->repr($argument)->raw (' => ')->subcompile($value)->raw(",\n");
}
$compiler
->outdent()
->write("),\n")
->outdent()
->write("),\n")
;
}
$compiler
->outdent()
->write(");\n")
;
$compiler
->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_start'))
->subcompile($this->getNode('body'))
;
}
protected function compileDisplayFooter(Twig_Compiler $compiler)
{
if ($this->hasNode('parent')) {
$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_end'))
->outdent()
->write("}\n\n")
;
@ -292,6 +322,7 @@ class Twig_Node_Module extends Twig_Node
protected function compileClassFooter(Twig_Compiler $compiler)
{
$compiler
->subcompile($this->getNode('class_end'))
->outdent()
->write("}\n")
;
@ -308,7 +339,7 @@ class Twig_Node_Module extends Twig_Node
->write("public function getTemplateName()\n", "{\n")
->indent()
->write('return ')
->repr($this->getAttribute('filename'))
->repr($this->source->getName())
->raw(";\n")
->outdent()
->write("}\n\n")
@ -324,7 +355,7 @@ class Twig_Node_Module extends Twig_Node
//
// Put another way, a template can be used as a trait if it
// only contains blocks and use statements.
$traitable = null === $this->getNode('parent') && 0 === count($this->getNode('macros'));
$traitable = !$this->hasNode('parent') && 0 === count($this->getNode('macros'));
if ($traitable) {
if ($this->getNode('body') instanceof Twig_Node_Body) {
$nodes = $this->getNode('body')->getNode(0);
@ -374,6 +405,37 @@ class Twig_Node_Module extends Twig_Node
->indent()
->write(sprintf("return %s;\n", str_replace("\n", '', var_export(array_reverse($compiler->getDebugInfo(), true), true))))
->outdent()
->write("}\n\n")
;
}
protected function compileGetSource(Twig_Compiler $compiler)
{
$compiler
->write("/** @deprecated since 1.27 (to be removed in 2.0). Use getSourceContext() instead */\n")
->write("public function getSource()\n", "{\n")
->indent()
->write("@trigger_error('The '.__METHOD__.' method is deprecated since version 1.27 and will be removed in 2.0. Use getSourceContext() instead.', E_USER_DEPRECATED);\n\n")
->write('return $this->getSourceContext()->getCode();')
->raw("\n")
->outdent()
->write("}\n\n")
;
}
protected function compileGetSourceContext(Twig_Compiler $compiler)
{
$compiler
->write("public function getSourceContext()\n", "{\n")
->indent()
->write('return new Twig_Source(')
->string($compiler->getEnvironment()->isDebug() ? $this->source->getCode() : '')
->raw(', ')
->string($this->source->getName())
->raw(', ')
->string($this->source->getPath())
->raw(");\n")
->outdent()
->write("}\n")
;
}
@ -382,22 +444,18 @@ class Twig_Node_Module extends Twig_Node
{
if ($node instanceof Twig_Node_Expression_Constant) {
$compiler
->write(sprintf("%s = \$this->env->loadTemplate(", $var))
->write(sprintf('%s = $this->loadTemplate(', $var))
->subcompile($node)
->raw(', ')
->repr($node->getTemplateName())
->raw(', ')
->repr($node->getTemplateLine())
->raw(");\n")
;
} else {
$compiler
->write(sprintf("%s = ", $var))
->subcompile($node)
->raw(";\n")
->write(sprintf("if (!%s", $var))
->raw(" instanceof Twig_Template) {\n")
->indent()
->write(sprintf("%s = \$this->env->loadTemplate(%s);\n", $var, $var))
->outdent()
->write("}\n")
;
throw new LogicException('Trait templates can only be constant nodes.');
}
}
}
class_alias('Twig_Node_Module', 'Twig\Node\ModuleNode', false);