Skip to content

Commit

Permalink
use reproducible variable names in the default domain node visitor
Browse files Browse the repository at this point in the history
  • Loading branch information
xabbuh authored and nicolas-grekas committed Nov 6, 2024
1 parent f2f472a commit e34839a
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions NodeVisitor/TranslationDefaultDomainNodeVisitor.php
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());
$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;
$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

0 comments on commit e34839a

Please sign in to comment.