* updated Twig to the latest version in 1.x series (v1.35.0)

This commit is contained in:
slawkens1
2017-12-25 13:02:46 +01:00
parent 6528a4a60c
commit 8b4eccc064
198 changed files with 4132 additions and 1977 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.
@@ -21,11 +21,18 @@
*/
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 = '')
{
// embedded templates are set as attributes so that they are only visited once by the visitors
parent::__construct(array(
'parent' => $parent,
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,
@@ -35,11 +42,23 @@ class Twig_Node_Module extends Twig_Node
'constructor_start' => new Twig_Node(),
'constructor_end' => new Twig_Node(),
'class_end' => new Twig_Node(),
), array(
'filename' => $filename,
);
if (null !== $parent) {
$nodes['parent'] = $parent;
}
// embedded templates are set as attributes so that they are only visited once by the visitors
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)
@@ -47,11 +66,6 @@ class Twig_Node_Module extends Twig_Node
$this->setAttribute('index', $index);
}
/**
* Compiles the node to PHP.
*
* @param Twig_Compiler $compiler A Twig_Compiler instance
*/
public function compile(Twig_Compiler $compiler)
{
$this->compileTemplate($compiler);
@@ -72,7 +86,7 @@ class Twig_Node_Module extends Twig_Node
if (
count($this->getNode('blocks'))
|| count($this->getNode('traits'))
|| null === $this->getNode('parent')
|| !$this->hasNode('parent')
|| $this->getNode('parent') instanceof Twig_Node_Expression_Constant
|| count($this->getNode('constructor_start'))
|| count($this->getNode('constructor_end'))
@@ -94,14 +108,19 @@ 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 === $parent = $this->getNode('parent')) {
if (!$this->hasNode('parent')) {
return;
}
$parent = $this->getNode('parent');
$compiler
->write("protected function doGetParent(array \$context)\n", "{\n")
@@ -117,9 +136,9 @@ class Twig_Node_Module extends Twig_Node
->raw('$this->loadTemplate(')
->subcompile($parent)
->raw(', ')
->repr($compiler->getFilename())
->repr($this->source->getName())
->raw(', ')
->repr($this->getNode('parent')->getLine())
->repr($parent->getTemplateLine())
->raw(')')
;
}
@@ -135,9 +154,9 @@ class Twig_Node_Module extends Twig_Node
{
$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()
@@ -154,17 +173,17 @@ class Twig_Node_Module extends Twig_Node
;
// parent
if (null === $parent = $this->getNode('parent')) {
if (!$this->hasNode('parent')) {
$compiler->write("\$this->parent = false;\n\n");
} elseif ($parent instanceof Twig_Node_Expression_Constant) {
} elseif (($parent = $this->getNode('parent')) && $parent instanceof Twig_Node_Expression_Constant) {
$compiler
->addDebugInfo($parent)
->write('$this->parent = $this->loadTemplate(')
->subcompile($parent)
->raw(', ')
->repr($compiler->getFilename())
->repr($this->source->getName())
->raw(', ')
->repr($this->getNode('parent')->getLine())
->repr($parent->getTemplateLine())
->raw(");\n")
;
}
@@ -282,7 +301,8 @@ class Twig_Node_Module extends Twig_Node
->subcompile($this->getNode('body'))
;
if (null !== $parent = $this->getNode('parent')) {
if ($this->hasNode('parent')) {
$parent = $this->getNode('parent');
$compiler->addDebugInfo($parent);
if ($parent instanceof Twig_Node_Expression_Constant) {
$compiler->write('$this->parent');
@@ -319,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")
@@ -335,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);
@@ -385,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")
;
}
@@ -396,28 +447,15 @@ class Twig_Node_Module extends Twig_Node
->write(sprintf('%s = $this->loadTemplate(', $var))
->subcompile($node)
->raw(', ')
->repr($compiler->getFilename())
->repr($node->getTemplateName())
->raw(', ')
->repr($node->getLine())
->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->loadTemplate(%s')
->raw(', ')
->repr($compiler->getFilename())
->raw(', ')
->repr($node->getLine())
->raw(");\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);