-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[TwigBridge] use reproducible variable names in the default domain node visitor #58706
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,8 +20,7 @@ | |
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\Expression\Variable\LocalVariable; | ||
use Twig\Node\ModuleNode; | ||
use Twig\Node\Node; | ||
use Twig\Node\Nodes; | ||
|
@@ -33,9 +32,8 @@ | |
*/ | ||
final class TranslationDefaultDomainNodeVisitor implements NodeVisitorInterface | ||
{ | ||
private const INTERNAL_VAR_NAME = '__internal_trans_default_domain'; | ||
|
||
private Scope $scope; | ||
private int $nestingLevel = 0; | ||
|
||
public function __construct() | ||
{ | ||
|
@@ -49,19 +47,25 @@ public function enterNode(Node $node, Environment $env): Node | |
} | ||
|
||
if ($node instanceof TransDefaultDomainNode) { | ||
++$this->nestingLevel; | ||
|
||
if ($node->getNode('expr') instanceof ConstantExpression) { | ||
$this->scope->set('domain', $node->getNode('expr')); | ||
|
||
return $node; | ||
} | ||
|
||
$name = class_exists(AssignContextVariable::class) ? new AssignContextVariable(self::INTERNAL_VAR_NAME, $node->getTemplateLine()) : new AssignNameExpression(self::INTERNAL_VAR_NAME, $node->getTemplateLine()); | ||
$this->scope->set('domain', class_exists(ContextVariable::class) ? new ContextVariable(self::INTERNAL_VAR_NAME, $node->getTemplateLine()) : new NameExpression(self::INTERNAL_VAR_NAME, $node->getTemplateLine())); | ||
|
||
if (class_exists(Nodes::class)) { | ||
$name = new LocalVariable(null, $node->getTemplateLine()); | ||
$this->scope->set('domain', $name); | ||
|
||
return new SetNode(false, new Nodes([$name]), new Nodes([$node->getNode('expr')]), $node->getTemplateLine()); | ||
} | ||
|
||
$var = '__internal_trans_default_domain_'.$this->nestingLevel; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I updated the implementation to rely on a counter instead There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @nicolas-grekas using a counter for the nesting level inside the current template does not solve the issue that this PR tries to fix, meaning that my comment in #57609 (comment) is not solved and that Symfony 7.2 still breaks the case where both a parent template and its child template use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FTR, this was fixed in #58706 |
||
$name = new AssignNameExpression($var, $node->getTemplateLine()); | ||
$this->scope->set('domain', new NameExpression($var, $node->getTemplateLine())); | ||
|
||
return new SetNode(false, new Node([$name]), new Node([$node->getNode('expr')]), $node->getTemplateLine()); | ||
} | ||
|
||
|
@@ -94,6 +98,8 @@ public function enterNode(Node $node, Environment $env): Node | |
public function leaveNode(Node $node, Environment $env): ?Node | ||
{ | ||
if ($node instanceof TransDefaultDomainNode) { | ||
--$this->nestingLevel; | ||
|
||
return null; | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@xabbuh changing this to a local variable is likely the root cause of EasyCorp/EasyAdminBundle#6605 because a local variable will not be available in blocks (it won't stay available in the Twig child scopes that are totally separate PHP scopes in the compiled template).
To me, we should keep assigning a context variable, as done before your PR.