8000 [TwigBridge] generate conflict-free variable names by xabbuh · Pull Request #59059 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[TwigBridge] generate conflict-free variable names #59059

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
Dec 2, 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
generate conflict-free variable names
  • Loading branch information
xabbuh committed Dec 2, 2024
commit 1a38acac242991a789c170ce28cfe74d0e28e9af
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