Skip to content

Commit

Permalink
feature #4408 Improve ImportNode impl (fabpot)
Browse files Browse the repository at this point in the history
This PR was merged into the 3.x branch.

Discussion
----------

Improve ImportNode impl

Commits
-------

612c7a1 Improve ImportNode impl
  • Loading branch information
fabpot committed Oct 24, 2024
2 parents 2735f81 + 612c7a1 commit b0017ad
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,33 @@

use Twig\Compiler;

final class GlobalTemplateVariable extends TemplateVariable
final class AssignTemplateVariable extends TemplateVariable
{
public function __construct(string|int|null $name, int $lineno, bool $global = true)
{
parent::__construct($name, $lineno);

$this->setAttribute('global', $global);
}

public function compile(Compiler $compiler): void
{
if (null === $this->getAttribute('name')) {
$this->setAttribute('name', \sprintf('_l%d', $compiler->getVarName()));
}

if ('_self' === $this->getAttribute('name')) {
$compiler->raw('$this');
} else {
$compiler
->addDebugInfo($this)
->write('$macros[')
->string($this->getAttribute('name'))
->raw('] = ')
;

if ($this->getAttribute('global')) {
$compiler
->raw('$this->macros[')
->string($this->getAttribute('name'))
->raw(']')
->raw('] = ')
;
}
}
Expand Down
3 changes: 0 additions & 3 deletions src/Node/Expression/Variable/TemplateVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
use Twig\Compiler;
use Twig\Node\Expression\TempNameExpression;

/**
* @final
*/
class TemplateVariable extends TempNameExpression
{
public function compile(Compiler $compiler): void
Expand Down
34 changes: 9 additions & 25 deletions src/Node/ImportNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
use Twig\Compiler;
use Twig\Node\Expression\AbstractExpression;
use Twig\Node\Expression\NameExpression;
use Twig\Node\Expression\Variable\GlobalTemplateVariable;
use Twig\Node\Expression\Variable\TemplateVariable;
use Twig\Node\Expression\Variable\AssignTemplateVariable;

/**
* Represents an import node.
Expand All @@ -29,39 +28,24 @@ class ImportNode extends Node
/**
* @param bool $global
*/
public function __construct(AbstractExpression $expr, AbstractExpression|TemplateVariable $var, int $lineno, $global = true)
public function __construct(AbstractExpression $expr, AbstractExpression|AssignTemplateVariable $var, int $lineno)
{
if (null === $global || \is_string($global)) {
trigger_deprecation('twig/twig', '3.12', 'Passing a tag to %s() is deprecated.', __METHOD__);
$global = \func_num_args() > 4 ? func_get_arg(4) : true;
} elseif (!\is_bool($global)) {
throw new \TypeError(\sprintf('Argument 4 passed to "%s()" must be a boolean, "%s" given.', __METHOD__, get_debug_type($global)));
if (!\is_bool(\func_num_args() > 3)) {
trigger_deprecation('twig/twig', '3.15', \sprintf('Passing more than 3 arguments to "%s()" is deprecated.', __METHOD__));
}

if (!$var instanceof TemplateVariable) {
trigger_deprecation('twig/twig', '3.15', \sprintf('Passing a "%s" instance as the second argument of "%s" is deprecated, pass a "%s" instead.', $var::class, __CLASS__, TemplateVariable::class));
if (!$var instanceof AssignTemplateVariable) {
trigger_deprecation('twig/twig', '3.15', \sprintf('Passing a "%s" instance as the second argument of "%s" is deprecated, pass a "%s" instead.', $var::class, __CLASS__, AssignTemplateVariable::class));

$var = new TemplateVariable($var->getAttribute('name'), $lineno);
$var = new AssignTemplateVariable($var->getAttribute('name'), $lineno);
}

parent::__construct(['expr' => $expr, 'var' => $var], ['global' => $global], $lineno);
parent::__construct(['expr' => $expr, 'var' => $var], [], $lineno);
}

public function compile(Compiler $compiler): void
{
$compiler
->addDebugInfo($this)
->write('')
->subcompile($this->getNode('var'))
->raw(' = ')
;

if ($this->getAttribute('global')) {
$compiler
->subcompile(new GlobalTemplateVariable($this->getNode('var')->getAttribute('name'), $this->getTemplateLine()))
->raw(' = ')
;
}
$compiler->subcompile($this->getNode('var'));

if ($this->getNode('expr') instanceof NameExpression && '_self' === $this->getNode('expr')->getAttribute('name')) {
$compiler->raw('$this');
Expand Down
6 changes: 3 additions & 3 deletions src/TokenParser/FromTokenParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace Twig\TokenParser;

use Twig\Node\Expression\Variable\AssignContextVariable;
use Twig\Node\Expression\Variable\TemplateVariable;
use Twig\Node\Expression\Variable\AssignTemplateVariable;
use Twig\Node\ImportNode;
use Twig\Node\Node;
use Twig\Token;
Expand Down Expand Up @@ -51,8 +51,8 @@ public function parse(Token $token): Node

$stream->expect(Token::BLOCK_END_TYPE);

$internalRef = new TemplateVariable(null, $token->getLine());
$node = new ImportNode($macro, $internalRef, $token->getLine(), $this->parser->isMainScope());
$internalRef = new AssignTemplateVariable(null, $token->getLine(), $this->parser->isMainScope());
$node = new ImportNode($macro, $internalRef, $token->getLine());

foreach ($targets as $name => $alias) {
$this->parser->addImportedSymbol('function', $alias->getAttribute('name'), 'macro_'.$name, $internalRef);
Expand Down
6 changes: 3 additions & 3 deletions src/TokenParser/ImportTokenParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Twig\TokenParser;

use Twig\Node\Expression\Variable\TemplateVariable;
use Twig\Node\Expression\Variable\AssignTemplateVariable;
use Twig\Node\ImportNode;
use Twig\Node\Node;
use Twig\Token;
Expand All @@ -29,11 +29,11 @@ public function parse(Token $token): Node
{
$macro = $this->parser->getExpressionParser()->parseExpression();
$this->parser->getStream()->expect(Token::NAME_TYPE, 'as');
$var = new TemplateVariable($this->parser->getStream()->expect(Token::NAME_TYPE)->getValue(), $token->getLine());
$var = new AssignTemplateVariable($this->parser->getStream()->expect(Token::NAME_TYPE)->getValue(), $token->getLine(), $this->parser->isMainScope());
$this->parser->getStream()->expect(Token::BLOCK_END_TYPE);
$this->parser->addImportedSymbol('template', $var->getAttribute('name'));

return new ImportNode($macro, $var, $token->getLine(), $this->parser->isMainScope());
return new ImportNode($macro, $var, $token->getLine());
}

public function getTag(): string
Expand Down
6 changes: 3 additions & 3 deletions tests/Node/ImportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/

use Twig\Node\Expression\ConstantExpression;
use Twig\Node\Expression\Variable\TemplateVariable;
use Twig\Node\Expression\Variable\AssignTemplateVariable;
use Twig\Node\ImportNode;
use Twig\Test\NodeTestCase;

Expand All @@ -21,7 +21,7 @@ class ImportTest extends NodeTestCase
public function testConstructor()
{
$macro = new ConstantExpression('foo.twig', 1);
$node = new ImportNode($macro, new TemplateVariable('macro', 1), 1);
$node = new ImportNode($macro, new AssignTemplateVariable('macro', 1), 1);

$this->assertEquals($macro, $node->getNode('expr'));
$this->assertEquals('macro', $node->getNode('var')->getAttribute('name'));
Expand All @@ -32,7 +32,7 @@ public static function provideTests(): iterable
$tests = [];

$macro = new ConstantExpression('foo.twig', 1);
$node = new ImportNode($macro, new TemplateVariable('macro', 1), 1);
$node = new ImportNode($macro, new AssignTemplateVariable('macro', 1), 1);

$tests[] = [$node, <<<EOF
// line 1
Expand Down
4 changes: 2 additions & 2 deletions tests/Node/ModuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use Twig\Node\Expression\ConditionalExpression;
use Twig\Node\Expression\ConstantExpression;
use Twig\Node\Expression\Variable\AssignContextVariable;
use Twig\Node\Expression\Variable\TemplateVariable;
use Twig\Node\Expression\Variable\AssignTemplateVariable;
use Twig\Node\ImportNode;
use Twig\Node\ModuleNode;
use Twig\Node\Nodes;
Expand Down Expand Up @@ -130,7 +130,7 @@ public function getSourceContext(): Source
EOF
, $twig, true];

$import = new ImportNode(new ConstantExpression('foo.twig', 1), new TemplateVariable('macro', 2), 2);
$import = new ImportNode(new ConstantExpression('foo.twig', 1), new AssignTemplateVariable('macro', 2), 2);

$body = new BodyNode([$import]);
$extends = new ConstantExpression('layout.twig', 1);
Expand Down

0 comments on commit b0017ad

Please sign in to comment.