Merge branch '2.x' into 3.x

* 2.x:
  macros imported "globally" in a template are now available in macros without re-importing them
  added more tests
  added some tests
This commit is contained in:
Fabien Potencier
2019-05-17 14:16:15 +02:00
24 changed files with 205 additions and 41 deletions
+13 -5
View File
@@ -22,19 +22,27 @@ use Twig\Node\Expression\NameExpression;
*/
class ImportNode extends Node
{
public function __construct(AbstractExpression $expr, AbstractExpression $var, int $lineno, string $tag = null)
public function __construct(AbstractExpression $expr, AbstractExpression $var, int $lineno, string $tag = null, bool $global = true)
{
parent::__construct(['expr' => $expr, 'var' => $var], [], $lineno, $tag);
parent::__construct(['expr' => $expr, 'var' => $var], ['global' => $global], $lineno, $tag);
}
public function compile(Compiler $compiler): void
{
$compiler
->addDebugInfo($this)
->write('')
->subcompile($this->getNode('var'))
->raw(' = ')
->write('$macros[')
->repr($this->getNode('var')->getAttribute('name'))
->raw('] = ')
;
if ($this->getAttribute('global')) {
$compiler
->raw('$this->macros[')
->repr($this->getNode('var')->getAttribute('name'))
->raw('] = ')
;
}
if ($this->getNode('expr') instanceof NameExpression && '_self' === $this->getNode('expr')->getAttribute('name')) {
$compiler->raw('$this');