mirror of
https://github.com/twigphp/Twig.git
synced 2026-10-25 13:16:31 +00:00
Refactor code
This commit is contained in:
@@ -533,7 +533,7 @@ class ExpressionParser
|
||||
public function getFunctionNode($name, $line)
|
||||
{
|
||||
if (null !== $alias = $this->parser->getImportedSymbol('function', $name)) {
|
||||
return new MacroReferenceExpression(new TemplateVariable($alias['node']->getAttribute('name'), $line), $alias['name'], $this->createArguments($line), $line);
|
||||
return new MacroReferenceExpression($alias['node']->getNode('var'), $alias['name'], $this->createArguments($line), $line);
|
||||
}
|
||||
|
||||
$args = $this->parseOnlyArguments();
|
||||
@@ -750,7 +750,7 @@ class ExpressionParser
|
||||
}
|
||||
|
||||
if ('defined' === $test->getName() && $node instanceof NameExpression && null !== $alias = $this->parser->getImportedSymbol('function', $node->getAttribute('name'))) {
|
||||
$node = new MacroReferenceExpression(new TemplateVariable($alias['node']->getAttribute('name'), $node->getTemplateLine()), $alias['name'], new ArrayExpression([], $node->getTemplateLine()), $node->getTemplateLine());
|
||||
$node = new MacroReferenceExpression($alias['node']->getNode('var'), $alias['name'], new ArrayExpression([], $node->getTemplateLine()), $node->getTemplateLine());
|
||||
}
|
||||
|
||||
$ready = $test instanceof TwigTest;
|
||||
@@ -957,7 +957,7 @@ class ExpressionParser
|
||||
$node instanceof NameExpression
|
||||
&&
|
||||
(
|
||||
null !== $this->parser->getImportedSymbol('template', (new TemplateVariable($node->getAttribute('name'), $node->getTemplateLine()))->getAttribute('name'))
|
||||
null !== $this->parser->getImportedSymbol('template', $node->getAttribute('name'))
|
||||
||
|
||||
'_self' === $node->getAttribute('name') && $attribute instanceof ConstantExpression
|
||||
)
|
||||
|
||||
@@ -12,33 +12,28 @@
|
||||
namespace Twig\Node\Expression\Variable;
|
||||
|
||||
use Twig\Compiler;
|
||||
use Twig\Node\Expression\AbstractExpression;
|
||||
|
||||
final class AssignTemplateVariable extends TemplateVariable
|
||||
final class AssignTemplateVariable extends AbstractExpression
|
||||
{
|
||||
public function __construct(string|int|null $name, int $lineno, bool $global = true)
|
||||
public function __construct(TemplateVariable $var, bool $global = true)
|
||||
{
|
||||
parent::__construct($name, $lineno);
|
||||
|
||||
$this->setAttribute('global', $global);
|
||||
parent::__construct(['var' => $var], ['global' => $global], $var->getTemplateLine());
|
||||
}
|
||||
|
||||
public function compile(Compiler $compiler): void
|
||||
{
|
||||
if (null === $this->getAttribute('name')) {
|
||||
$this->setAttribute('name', \sprintf('_l%d', $compiler->getVarName()));
|
||||
}
|
||||
|
||||
$compiler
|
||||
->addDebugInfo($this)
|
||||
->write('$macros[')
|
||||
->string($this->getAttribute('name'))
|
||||
->string($this->nodes['var']->getName($compiler))
|
||||
->raw('] = ')
|
||||
;
|
||||
|
||||
if ($this->getAttribute('global')) {
|
||||
$compiler
|
||||
->raw('$this->macros[')
|
||||
->string($this->getAttribute('name'))
|
||||
->string($this->nodes['var']->getName($compiler))
|
||||
->raw('] = ')
|
||||
;
|
||||
}
|
||||
|
||||
@@ -16,18 +16,25 @@ use Twig\Node\Expression\TempNameExpression;
|
||||
|
||||
class TemplateVariable extends TempNameExpression
|
||||
{
|
||||
public function compile(Compiler $compiler): void
|
||||
public function getName(Compiler $compiler): string
|
||||
{
|
||||
if (null === $this->getAttribute('name')) {
|
||||
$this->setAttribute('name', \sprintf('_l%d', $compiler->getVarName()));
|
||||
$this->setAttribute('name', $compiler->getVarName());
|
||||
}
|
||||
|
||||
if ('_self' === $this->getAttribute('name')) {
|
||||
return $this->getAttribute('name');
|
||||
}
|
||||
|
||||
public function compile(Compiler $compiler): void
|
||||
{
|
||||
$name = $this->getName($compiler);
|
||||
|
||||
if ('_self' === $name) {
|
||||
$compiler->raw('$this');
|
||||
} else {
|
||||
$compiler
|
||||
->raw('$macros[')
|
||||
->string($this->getAttribute('name'))
|
||||
->string($name)
|
||||
->raw(']')
|
||||
;
|
||||
}
|
||||
|
||||
@@ -61,7 +61,7 @@ final class EscaperNodeVisitor implements NodeVisitorInterface
|
||||
} elseif ($node instanceof BlockNode) {
|
||||
$this->statusStack[] = $this->blocks[$node->getAttribute('name')] ?? $this->needEscaping();
|
||||
} elseif ($node instanceof ImportNode) {
|
||||
$this->safeVars[] = $node->getNode('var')->getAttribute('name');
|
||||
$this->safeVars[] = $node->getNode('var')->getNode('var')->getAttribute('name');
|
||||
}
|
||||
|
||||
return $node;
|
||||
|
||||
+5
-4
@@ -18,6 +18,7 @@ use Twig\Node\BlockReferenceNode;
|
||||
use Twig\Node\BodyNode;
|
||||
use Twig\Node\EmptyNode;
|
||||
use Twig\Node\Expression\AbstractExpression;
|
||||
use Twig\Node\Expression\Variable\AssignTemplateVariable;
|
||||
use Twig\Node\Expression\Variable\TemplateVariable;
|
||||
use Twig\Node\MacroNode;
|
||||
use Twig\Node\ModuleNode;
|
||||
@@ -295,12 +296,12 @@ class Parser
|
||||
$this->embeddedTemplates[] = $template;
|
||||
}
|
||||
|
||||
public function addImportedSymbol(string $type, string $alias, ?string $name = null, AbstractExpression|TemplateVariable|null $internalRef = null): void
|
||||
public function addImportedSymbol(string $type, string $alias, ?string $name = null, AbstractExpression|AssignTemplateVariable|null $internalRef = null): void
|
||||
{
|
||||
if ($internalRef && !$internalRef instanceof TemplateVariable) {
|
||||
trigger_deprecation('twig/twig', '3.15', 'Not passing a "%s" instance as an internal reference is deprecated ("%s" given).', __METHOD__, TemplateVariable::class, $internalRef::class);
|
||||
if ($internalRef && !$internalRef instanceof AssignTemplateVariable) {
|
||||
trigger_deprecation('twig/twig', '3.15', 'Not passing a "%s" instance as an internal reference is deprecated ("%s" given).', __METHOD__, AssignTemplateVariable::class, $internalRef::class);
|
||||
|
||||
$internalRef = new TemplateVariable($internalRef->getAttribute('name'), $internalRef->getTemplateLine());
|
||||
$internalRef = new AssignTemplateVariable(new TemplateVariable($internalRef->getAttribute('name'), $internalRef->getTemplateLine()), $internalRef->getAttribute('global'));
|
||||
}
|
||||
|
||||
$this->importedSymbols[0][$type][$alias] = ['name' => $name, 'node' => $internalRef];
|
||||
|
||||
@@ -13,6 +13,7 @@ namespace Twig\TokenParser;
|
||||
|
||||
use Twig\Node\Expression\Variable\AssignContextVariable;
|
||||
use Twig\Node\Expression\Variable\AssignTemplateVariable;
|
||||
use Twig\Node\Expression\Variable\TemplateVariable;
|
||||
use Twig\Node\ImportNode;
|
||||
use Twig\Node\Node;
|
||||
use Twig\Token;
|
||||
@@ -51,7 +52,7 @@ final class FromTokenParser extends AbstractTokenParser
|
||||
|
||||
$stream->expect(Token::BLOCK_END_TYPE);
|
||||
|
||||
$internalRef = new AssignTemplateVariable(null, $token->getLine(), $this->parser->isMainScope());
|
||||
$internalRef = new AssignTemplateVariable(new TemplateVariable(null, $token->getLine()), $this->parser->isMainScope());
|
||||
$node = new ImportNode($macro, $internalRef, $token->getLine());
|
||||
|
||||
foreach ($targets as $name => $alias) {
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
namespace Twig\TokenParser;
|
||||
|
||||
use Twig\Node\Expression\Variable\AssignTemplateVariable;
|
||||
use Twig\Node\Expression\Variable\TemplateVariable;
|
||||
use Twig\Node\ImportNode;
|
||||
use Twig\Node\Node;
|
||||
use Twig\Token;
|
||||
@@ -29,9 +30,10 @@ final class ImportTokenParser extends AbstractTokenParser
|
||||
{
|
||||
$macro = $this->parser->getExpressionParser()->parseExpression();
|
||||
$this->parser->getStream()->expect(Token::NAME_TYPE, 'as');
|
||||
$var = new AssignTemplateVariable($this->parser->getStream()->expect(Token::NAME_TYPE)->getValue(), $token->getLine(), $this->parser->isMainScope());
|
||||
$name = $this->parser->getStream()->expect(Token::NAME_TYPE)->getValue();
|
||||
$var = new AssignTemplateVariable(new TemplateVariable($name, $token->getLine()), $this->parser->isMainScope());
|
||||
$this->parser->getStream()->expect(Token::BLOCK_END_TYPE);
|
||||
$this->parser->addImportedSymbol('template', $var->getAttribute('name'));
|
||||
$this->parser->addImportedSymbol('template', $name);
|
||||
|
||||
return new ImportNode($macro, $var, $token->getLine());
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ namespace Twig\Tests\Node;
|
||||
|
||||
use Twig\Node\Expression\ConstantExpression;
|
||||
use Twig\Node\Expression\Variable\AssignTemplateVariable;
|
||||
use Twig\Node\Expression\Variable\TemplateVariable;
|
||||
use Twig\Node\ImportNode;
|
||||
use Twig\Test\NodeTestCase;
|
||||
|
||||
@@ -21,10 +22,10 @@ class ImportTest extends NodeTestCase
|
||||
public function testConstructor()
|
||||
{
|
||||
$macro = new ConstantExpression('foo.twig', 1);
|
||||
$node = new ImportNode($macro, new AssignTemplateVariable('macro', 1), 1);
|
||||
$node = new ImportNode($macro, new AssignTemplateVariable(new TemplateVariable('macro', 1), true), 1);
|
||||
|
||||
$this->assertEquals($macro, $node->getNode('expr'));
|
||||
$this->assertEquals('macro', $node->getNode('var')->getAttribute('name'));
|
||||
$this->assertEquals('macro', $node->getNode('var')->getNode('var')->getAttribute('name'));
|
||||
}
|
||||
|
||||
public static function provideTests(): iterable
|
||||
@@ -32,7 +33,7 @@ class ImportTest extends NodeTestCase
|
||||
$tests = [];
|
||||
|
||||
$macro = new ConstantExpression('foo.twig', 1);
|
||||
$node = new ImportNode($macro, new AssignTemplateVariable('macro', 1), 1);
|
||||
$node = new ImportNode($macro, new AssignTemplateVariable(new TemplateVariable('macro', 1), true), 1);
|
||||
|
||||
$tests[] = [$node, <<<EOF
|
||||
// line 1
|
||||
|
||||
@@ -19,6 +19,7 @@ use Twig\Node\Expression\ConditionalExpression;
|
||||
use Twig\Node\Expression\ConstantExpression;
|
||||
use Twig\Node\Expression\Variable\AssignContextVariable;
|
||||
use Twig\Node\Expression\Variable\AssignTemplateVariable;
|
||||
use Twig\Node\Expression\Variable\TemplateVariable;
|
||||
use Twig\Node\ImportNode;
|
||||
use Twig\Node\ModuleNode;
|
||||
use Twig\Node\Nodes;
|
||||
@@ -131,7 +132,7 @@ class __TwigTemplate_%x extends Template
|
||||
EOF
|
||||
, $twig, true];
|
||||
|
||||
$import = new ImportNode(new ConstantExpression('foo.twig', 1), new AssignTemplateVariable('macro', 2), 2);
|
||||
$import = new ImportNode(new ConstantExpression('foo.twig', 1), new AssignTemplateVariable(new TemplateVariable('macro', 2), true), 2);
|
||||
|
||||
$body = new BodyNode([$import]);
|
||||
$extends = new ConstantExpression('layout.twig', 1);
|
||||
|
||||
Reference in New Issue
Block a user