mirror of
https://github.com/twigphp/Twig.git
synced 2026-08-31 12:37:15 +00:00
feature #4408 Improve ImportNode impl (fabpot)
This PR was merged into the 3.x branch.
Discussion
----------
Improve ImportNode impl
Commits
-------
612c7a14be Improve ImportNode impl
This commit is contained in:
+17
-5
@@ -13,21 +13,33 @@ namespace Twig\Node\Expression\Variable;
|
||||
|
||||
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('] = ')
|
||||
;
|
||||
}
|
||||
}
|
||||
@@ -14,9 +14,6 @@ namespace Twig\Node\Expression\Variable;
|
||||
use Twig\Compiler;
|
||||
use Twig\Node\Expression\TempNameExpression;
|
||||
|
||||
/**
|
||||
* @final
|
||||
*/
|
||||
class TemplateVariable extends TempNameExpression
|
||||
{
|
||||
public function compile(Compiler $compiler): void
|
||||
|
||||
+9
-25
@@ -15,8 +15,7 @@ use Twig\Attribute\YieldReady;
|
||||
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.
|
||||
@@ -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');
|
||||
|
||||
@@ -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;
|
||||
@@ -51,8 +51,8 @@ final class FromTokenParser extends AbstractTokenParser
|
||||
|
||||
$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);
|
||||
|
||||
@@ -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;
|
||||
@@ -29,11 +29,11 @@ final class ImportTokenParser extends AbstractTokenParser
|
||||
{
|
||||
$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
|
||||
|
||||
@@ -12,7 +12,7 @@ namespace Twig\Tests\Node;
|
||||
*/
|
||||
|
||||
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;
|
||||
|
||||
@@ -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'));
|
||||
@@ -32,7 +32,7 @@ class ImportTest extends NodeTestCase
|
||||
$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
|
||||
|
||||
@@ -18,7 +18,7 @@ use Twig\Node\EmptyNode;
|
||||
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;
|
||||
@@ -130,7 +130,7 @@ class __TwigTemplate_%x extends Template
|
||||
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);
|
||||
|
||||
Reference in New Issue
Block a user