Skip to content

[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

Merged
merged 1 commit into from
Nov 6, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()
{
Expand All @@ -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());
Copy link
Member

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.

$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;
Copy link
Member

Choose a reason for hiding this comment

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

I updated the implementation to rely on a counter instead

Copy link
Member

Choose a reason for hiding this comment

The 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 {% trans_default_domain %}

Copy link
Member Author

Choose a reason for hiding this comment

The 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());
}

Expand Down Expand Up @@ -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;
}

Expand Down