Skip to content

[TwigBridge] ensure compatibility with Twig 3.15 #58649

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions src/Symfony/Bridge/Twig/Node/DumpNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Twig\Attribute\FirstClassTwigCallableReady;
use Twig\Attribute\YieldReady;
use Twig\Compiler;
use Twig\Node\Expression\Variable\LocalVariable;
use Twig\Node\Node;

/**
Expand All @@ -22,10 +23,20 @@
#[YieldReady]
final class DumpNode extends Node
{
/**
* @var LocalVariable|string
*/
private $varPrefix;

public function __construct(string $varPrefix, ?Node $values, int $lineno, ?string $tag = null)
/**
* @param LocalVariable|string $varPrefix
*/
public function __construct($varPrefix, ?Node $values, int $lineno, ?string $tag = null)
{
if (!\is_string($varPrefix) && !$varPrefix instanceof LocalVariable) {
throw new \TypeError(sprintf('Expected a string or an instance of "%s", but got "%s".', LocalVariable::class, get_debug_type($varPrefix)));
}

$nodes = [];
if (null !== $values) {
$nodes['values'] = $values;
Expand All @@ -42,25 +53,31 @@ public function __construct(string $varPrefix, ?Node $values, int $lineno, ?stri

public function compile(Compiler $compiler): void
{
if ($this->varPrefix instanceof LocalVariable) {
$varPrefix = $this->varPrefix->getAttribute('name');
} else {
$varPrefix = $this->varPrefix;
}

$compiler
->write("if (\$this->env->isDebug()) {\n")
->indent();

if (!$this->hasNode('values')) {
// remove embedded templates (macros) from the context
$compiler
->write(sprintf('$%svars = [];'."\n", $this->varPrefix))
->write(sprintf('foreach ($context as $%1$skey => $%1$sval) {'."\n", $this->varPrefix))
->write(sprintf('$%svars = [];'."\n", $varPrefix))
->write(sprintf('foreach ($context as $%1$skey => $%1$sval) {'."\n", $varPrefix))
->indent()
->write(sprintf('if (!$%sval instanceof \Twig\Template) {'."\n", $this->varPrefix))
->write(sprintf('if (!$%sval instanceof \Twig\Template) {'."\n", $varPrefix))
->indent()
->write(sprintf('$%1$svars[$%1$skey] = $%1$sval;'."\n", $this->varPrefix))
->write(sprintf('$%1$svars[$%1$skey] = $%1$sval;'."\n", $varPrefix))
->outdent()
->write("}\n")
->outdent()
->write("}\n")
->addDebugInfo($this)
->write(sprintf('\Symfony\Component\VarDumper\VarDumper::dump($%svars);'."\n", $this->varPrefix));
->write(sprintf('\Symfony\Component\VarDumper\VarDumper::dump($%svars);'."\n", $varPrefix));
} elseif (($values = $this->getNode('values')) && 1 === $values->count()) {
$compiler
->addDebugInfo($this)
Expand Down
10 changes: 9 additions & 1 deletion src/Symfony/Bridge/Twig/Node/StopwatchNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Twig\Attribute\YieldReady;
use Twig\Compiler;
use Twig\Node\Expression\AssignNameExpression;
use Twig\Node\Expression\Variable\LocalVariable;
use Twig\Node\Node;

/**
Expand All @@ -25,8 +26,15 @@
#[YieldReady]
final class StopwatchNode extends Node
{
public function __construct(Node $name, Node $body, AssignNameExpression $var, int $lineno = 0, ?string $tag = null)
/**
* @param AssignNameExpression|LocalVariable $var
*/
public function __construct(Node $name, Node $body, $var, int $lineno = 0, ?string $tag = null)
{
if (!$var instanceof AssignNameExpression && !$var instanceof LocalVariable) {
throw new \TypeError(sprintf('Expected an instance of "%s" or "%s", but got "%s".', AssignNameExpression::class, LocalVariable::class, get_debug_type($var)));
}

if (class_exists(FirstClassTwigCallableReady::class)) {
parent::__construct(['body' => $body, 'name' => $name, 'var' => $var], [], $lineno);
} else {
Expand Down
3 changes: 2 additions & 1 deletion src/Symfony/Bridge/Twig/Node/TransNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Twig\Node\Expression\ArrayExpression;
use Twig\Node\Expression\ConstantExpression;
use Twig\Node\Expression\NameExpression;
use Twig\Node\Expression\Variable\ContextVariable;
use Twig\Node\Node;
use Twig\Node\TextNode;

Expand Down Expand Up @@ -126,7 +127,7 @@ private function compileString(Node $body, ArrayExpression $vars, bool $ignoreSt
if ('count' === $var && $this->hasNode('count')) {
$vars->addElement($this->getNode('count'), $key);
} else {
$varExpr = new NameExpression($var, $body->getTemplateLine());
$varExpr = class_exists(ContextVariable::class) ? new ContextVariable($var, $body->getTemplateLine()) : new NameExpression($var, $body->getTemplateLine());
$varExpr->setAttribute('ignore_strict_check', $ignoreStrictCheck);
$vars->addElement($varExpr, $key);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
use Twig\Node\Expression\ConstantExpression;
use Twig\Node\Expression\FilterExpression;
use Twig\Node\Expression\NameExpression;
use Twig\Node\Expression\Variable\AssignContextVariable;
use Twig\Node\Expression\Variable\ContextVariable;
use Twig\Node\ModuleNode;
use Twig\Node\Node;
use Twig\Node\Nodes;
Expand Down Expand Up @@ -51,8 +53,8 @@ public function enterNode(Node $node, Environment $env): Node
return $node;
} else {
$var = $this->getVarName();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just discovered that this node visitor breaks reproducible compilation here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point, let's fix this explicitly in a separate PR

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just noticed that this was fixed for 7.2 in #57609. Should we backport that PR?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that PR is actually broken

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's backport (and fix the broken part) in 5.4 yes
reproducible builds FTW

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A way to generate a reproducible name that does not clash with parent or child template could be to use a hash of the template name in the variable name (with the new hardcoded name as prefix)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see #58706

$name = new AssignNameExpression($var, $node->getTemplateLine());
$this->scope->set('domain', new NameExpression($var, $node->getTemplateLine()));
$name = class_exists(AssignContextVariable::class) ? new AssignContextVariable($var, $node->getTemplateLine()) : new AssignNameExpression($var, $node->getTemplateLine());
$this->scope->set('domain', class_exists(ContextVariable::class) ? new ContextVariable($var, $node->getTemplateLine()) : new NameExpression($var, $node->getTemplateLine()));

if (class_exists(Nodes::class)) {
return new SetNode(false, new Nodes([$name]), new Nodes([$node->getNode('expr')]), $node->getTemplateLine());
Expand Down
7 changes: 4 additions & 3 deletions src/Symfony/Bridge/Twig/Tests/Node/DumpNodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Twig\Environment;
use Twig\Loader\LoaderInterface;
use Twig\Node\Expression\NameExpression;
use Twig\Node\Expression\Variable\ContextVariable;
use Twig\Node\Node;
use Twig\Node\Nodes;

Expand Down Expand Up @@ -74,7 +75,7 @@ public function testOneVar()
{
if (class_exists(Nodes::class)) {
$vars = new Nodes([
new NameExpression('foo', 7),
new ContextVariable('foo', 7),
]);
} else {
$vars = new Node([
Expand Down Expand Up @@ -104,8 +105,8 @@ public function testMultiVars()
{
if (class_exists(Nodes::class)) {
$vars = new Nodes([
new NameExpression('foo', 7),
new NameExpression('bar', 7),
new ContextVariable('foo', 7),
new ContextVariable('bar', 7),
]);
} else {
$vars = new Node([
Expand Down
5 changes: 3 additions & 2 deletions src/Symfony/Bridge/Twig/Tests/Node/FormThemeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Twig\Node\Expression\ArrayExpression;
use Twig\Node\Expression\ConstantExpression;
use Twig\Node\Expression\NameExpression;
use Twig\Node\Expression\Variable\ContextVariable;
use Twig\Node\Node;
use Twig\Node\Nodes;

Expand All @@ -31,7 +32,7 @@ class FormThemeTest extends TestCase

public function testConstructor()
{
$form = new NameExpression('form', 0);
$form = class_exists(ContextVariable::class) ? new ContextVariable('form', 0) : new NameExpression('form', 0);
if (class_exists(Nodes::class)) {
$resources = new Nodes([
new ConstantExpression('tpl1', 0),
Expand All @@ -53,7 +54,7 @@ public function testConstructor()

public function testCompile()
{
$form = new NameExpression('form', 0);
$form = class_exists(ContextVariable::class) ? new ContextVariable('form', 0) : new NameExpression('form', 0);
$resources = new ArrayExpression([
new ConstantExpression(1, 0),
new ConstantExpression('tpl1', 0),
Expand Down
21 changes: 11 additions & 10 deletions src/Symfony/Bridge/Twig/Tests/Node/SearchAndRenderBlockNodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Twig\Node\Expression\ConditionalExpression;
use Twig\Node\Expression\ConstantExpression;
use Twig\Node\Expression\NameExpression;
use Twig\Node\Expression\Variable\ContextVariable;
use Twig\Node\Node;
use Twig\Node\Nodes;
use Twig\TwigFunction;
Expand All @@ -32,7 +33,7 @@ public function testCompileWidget()
{
if (class_exists(Nodes::class)) {
$arguments = new Nodes([
new NameExpression('form', 0),
new ContextVariable('form', 0),
]);
} else {
$arguments = new Node([
Expand Down Expand Up @@ -61,7 +62,7 @@ public function testCompileWidgetWithVariables()
{
if (class_exists(Nodes::class)) {
$arguments = new Nodes([
new NameExpression('form', 0),
new ContextVariable('form', 0),
new ArrayExpression([
new ConstantExpression('foo', 0),
new ConstantExpression('bar', 0),
Expand Down Expand Up @@ -98,7 +99,7 @@ public function testCompileLabelWithLabel()
{
if (class_exists(Nodes::class)) {
$arguments = new Nodes([
new NameExpression('form', 0),
new ContextVariable('form', 0),
new ConstantExpression('my label', 0),
]);
} else {
Expand Down Expand Up @@ -129,7 +130,7 @@ public function testCompileLabelWithNullLabel()
{
if (class_exists(Nodes::class)) {
$arguments = new Nodes([
new NameExpression('form', 0),
new ContextVariable('form', 0),
new ConstantExpression(null, 0),
]);
} else {
Expand Down Expand Up @@ -162,7 +163,7 @@ public function testCompileLabelWithEmptyStringLabel()
{
if (class_exists(Nodes::class)) {
$arguments = new Nodes([
new NameExpression('form', 0),
new ContextVariable('form', 0),
new ConstantExpression('', 0),
]);
} else {
Expand Down Expand Up @@ -195,7 +196,7 @@ public function testCompileLabelWithDefaultLabel()
{
if (class_exists(Nodes::class)) {
$arguments = new Nodes([
new NameExpression('form', 0),
new ContextVariable('form', 0),
]);
} else {
$arguments = new Node([
Expand Down Expand Up @@ -224,7 +225,7 @@ public function testCompileLabelWithAttributes()
{
if (class_exists(Nodes::class)) {
$arguments = new Nodes([
new NameExpression('form', 0),
new ContextVariable('form', 0),
new ConstantExpression(null, 0),
new ArrayExpression([
new ConstantExpression('foo', 0),
Expand Down Expand Up @@ -266,7 +267,7 @@ public function testCompileLabelWithLabelAndAttributes()
{
if (class_exists(Nodes::class)) {
$arguments = new Nodes([
new NameExpression('form', 0),
new ContextVariable('form', 0),
new ConstantExpression('value in argument', 0),
new ArrayExpression([
new ConstantExpression('foo', 0),
Expand Down Expand Up @@ -309,7 +310,7 @@ public function testCompileLabelWithLabelThatEvaluatesToNull()
{
if (class_exists(Nodes::class)) {
$arguments = new Nodes([
new NameExpression('form', 0),
new ContextVariable('form', 0),
new ConditionalExpression(
// if
new ConstantExpression(true, 0),
Expand Down Expand Up @@ -360,7 +361,7 @@ public function testCompileLabelWithLabelThatEvaluatesToNullAndAttributes()
{
if (class_exists(Nodes::class)) {
$arguments = new Nodes([
new NameExpression('form', 0),
new ContextVariable('form', 0),
new ConditionalExpression(
// if
new ConstantExpression(true, 0),
Expand Down
3 changes: 2 additions & 1 deletion src/Symfony/Bridge/Twig/Tests/Node/TransNodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Twig\Environment;
use Twig\Loader\LoaderInterface;
use Twig\Node\Expression\NameExpression;
use Twig\Node\Expression\Variable\ContextVariable;
use Twig\Node\TextNode;

/**
Expand All @@ -28,7 +29,7 @@ class TransNodeTest extends TestCase
public function testCompileStrict()
{
$body = new TextNode('trans %var%', 0);
$vars = new NameExpression('foo', 0);
$vars = class_exists(ContextVariable::class) ? new ContextVariable('foo', 0) : new NameExpression('foo', 0);
$node = new TransNode($body, null, null, $vars);

$env = new Environment($this->createMock(LoaderInterface::class), ['strict_variables' => true]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Twig\Node\Expression\ConstantExpression;
use Twig\Node\Expression\FilterExpression;
use Twig\Node\Expression\NameExpression;
use Twig\Node\Expression\Variable\ContextVariable;
use Twig\Node\Node;
use Twig\Node\Nodes;
use Twig\TwigFilter;
Expand All @@ -44,7 +45,7 @@ public function testMessageExtractionWithInvalidDomainNode()
if (class_exists(Nodes::class)) {
$n = new Nodes([
new ArrayExpression([], 0),
new NameExpression('variable', 0),
new ContextVariable('variable', 0),
]);
} else {
$n = new Node([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Twig\Node\Expression\ArrayExpression;
use Twig\Node\Expression\ConstantExpression;
use Twig\Node\Expression\NameExpression;
use Twig\Node\Expression\Variable\ContextVariable;
use Twig\Parser;
use Twig\Source;

Expand Down Expand Up @@ -51,7 +52,7 @@ public static function getTestsForFormTheme()
[
'{% form_theme form "tpl1" %}',
new FormThemeNode(
new NameExpression('form', 1),
class_exists(ContextVariable::class) ? new ContextVariable('form', 1) : new NameExpression('form', 1),
new ArrayExpression([
new ConstantExpression(0, 1),
new ConstantExpression('tpl1', 1),
Expand All @@ -63,7 +64,7 @@ public static function getTestsForFormTheme()
[
'{% form_theme form "tpl1" "tpl2" %}',
new FormThemeNode(
new NameExpression('form', 1),
class_exists(ContextVariable::class) ? new ContextVariable('form', 1) : new NameExpression('form', 1),
new ArrayExpression([
new ConstantExpression(0, 1),
new ConstantExpression('tpl1', 1),
Expand All @@ -77,7 +78,7 @@ public static function getTestsForFormTheme()
[
'{% form_theme form with "tpl1" %}',
new FormThemeNode(
new NameExpression('form', 1),
class_exists(ContextVariable::class) ? new ContextVariable('form', 1) : new NameExpression('form', 1),
new ConstantExpression('tpl1', 1),
1,
'form_theme'
Expand All @@ -86,7 +87,7 @@ public static function getTestsForFormTheme()
[
'{% form_theme form with ["tpl1"] %}',
new FormThemeNode(
new NameExpression('form', 1),
class_exists(ContextVariable::class) ? new ContextVariable('form', 1) : new NameExpression('form', 1),
new ArrayExpression([
new ConstantExpression(0, 1),
new ConstantExpression('tpl1', 1),
Expand All @@ -98,7 +99,7 @@ public static function getTestsForFormTheme()
[
'{% form_theme form with ["tpl1", "tpl2"] %}',
new FormThemeNode(
new NameExpression('form', 1),
class_exists(ContextVariable::class) ? new ContextVariable('form', 1) : new NameExpression('form', 1),
new ArrayExpression([
new ConstantExpression(0, 1),
new ConstantExpression('tpl1', 1),
Expand All @@ -112,7 +113,7 @@ public static function getTestsForFormTheme()
[
'{% form_theme form with ["tpl1", "tpl2"] only %}',
new FormThemeNode(
new NameExpression('form', 1),
class_exists(ContextVariable::class) ? new ContextVariable('form', 1) : new NameExpression('form', 1),
new ArrayExpression([
new ConstantExpression(0, 1),
new ConstantExpression('tpl1', 1),
Expand Down
3 changes: 2 additions & 1 deletion src/Symfony/Bridge/Twig/TokenParser/DumpTokenParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Bridge\Twig\TokenParser;

use Symfony\Bridge\Twig\Node\DumpNode;
use Twig\Node\Expression\Variable\LocalVariable;
use Twig\Node\Node;
use Twig\Token;
use Twig\TokenParser\AbstractTokenParser;
Expand Down Expand Up @@ -40,7 +41,7 @@ public function parse(Token $token): Node
}
$this->parser->getStream()->expect(Token::BLOCK_END_TYPE);

return new DumpNode($this->parser->getVarName(), $values, $token->getLine(), $this->getTag());
return new DumpNode(class_exists(LocalVariable::class) ? new LocalVariable(null, $token->getLine()) : $this->parser->getVarName(), $values, $token->getLine(), $this->getTag());
}

/**
Expand Down
Loading
Loading