Skip to content

Commit

Permalink
bug #59059 [TwigBridge] generate conflict-free variable names (xabbuh)
Browse files Browse the repository at this point in the history
This PR was merged into the 7.2 branch.

Discussion
----------

[TwigBridge] generate conflict-free variable names

| Q             | A
| ------------- | ---
| Branch?       | 7.2
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Issues        | Fix symfony/symfony#58706 (comment), Fix EasyCorp/EasyAdminBundle#6605, Fix twigphp/Twig#4480
| License       | MIT

Commits
-------

1a38acac24 generate conflict-free variable names
  • Loading branch information
xabbuh committed Dec 2, 2024
2 parents d6e5496 + 4b51036 commit 1343696
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions NodeVisitor/TranslationDefaultDomainNodeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
use Twig\Node\Expression\ConstantExpression;
use Twig\Node\Expression\FilterExpression;
use Twig\Node\Expression\NameExpression;
use Twig\Node\Expression\Variable\LocalVariable;
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 All @@ -34,7 +35,6 @@
final class TranslationDefaultDomainNodeVisitor implements NodeVisitorInterface
{
private Scope $scope;
private int $nestingLevel = 0;

public function __construct()
{
Expand All @@ -48,22 +48,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;
}

if (null === $templateName = $node->getTemplateName()) {
throw new \LogicException('Cannot traverse a node without a template name.');
}

$var = '__internal_trans_default_domain'.hash('xxh128', $templateName);

if (class_exists(Nodes::class)) {
$name = new LocalVariable(null, $node->getTemplateLine());
$this->scope->set('domain', $name);
$name = new AssignContextVariable($var, $node->getTemplateLine());
$this->scope->set('domain', new ContextVariable($var, $node->getTemplateLine()));

return new SetNode(false, new Nodes([$name]), new Nodes([$node->getNode('expr')]), $node->getTemplateLine());
}

$var = '__internal_trans_default_domain_'.$this->nestingLevel;
$name = new AssignNameExpression($var, $node->getTemplateLine());
$this->scope->set('domain', new NameExpression($var, $node->getTemplateLine()));

Expand Down Expand Up @@ -105,8 +108,6 @@ 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

0 comments on commit 1343696

Please sign in to comment.