Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 68a5704

Browse filesBrowse files
committed
minor #57609 [TwigBridge]  Use constant var name to cache trans_default_domain expression result (GromNaN)
This PR was merged into the 7.2 branch. Discussion ---------- [TwigBridge]  Use constant var name to cache `trans_default_domain` expression result | Q | A | ------------- | --- | Branch? | 7.2 | Bug fix? | no | New feature? | no | Deprecations? | no | Issues | Part of #57588 | License | MIT When `trans_default_domain` is used with an expression, the result of the expression is cached into a variable and this variable is stored in the `Scope` to be used for each following usage of the `|trans` filter. This var name doesn't need to be random: - there is only 1 value in the scope, more than 1 variable at a time is never necessary. - if `trans_default_domain` is called a second time, the same variable can be reassigned. The only benefit of using a random var name would be to prevent usage in the template. The name `__internal_trans_default_domain` self-explains that it is not meant to be used. Commits ------- ab116bb Use constant var name to cache trans_default_domain expression result
2 parents 0f4cf9b + ab116bb commit 68a5704
Copy full SHA for 68a5704

File tree

1 file changed

+4
-8
lines changed
Filter options

1 file changed

+4
-8
lines changed

‎src/Symfony/Bridge/Twig/NodeVisitor/TranslationDefaultDomainNodeVisitor.php

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Twig/NodeVisitor/TranslationDefaultDomainNodeVisitor.php
+4-8Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
*/
3131
final class TranslationDefaultDomainNodeVisitor implements NodeVisitorInterface
3232
{
33+
private const INTERNAL_VAR_NAME = '__internal_trans_default_domain';
34+
3335
private Scope $scope;
3436

3537
public function __construct()
@@ -49,9 +51,8 @@ public function enterNode(Node $node, Environment $env): Node
4951

5052
return $node;
5153
} else {
52-
$var = $this->getVarName();
53-
$name = new AssignNameExpression($var, $node->getTemplateLine());
54-
$this->scope->set('domain', new NameExpression($var, $node->getTemplateLine()));
54+
$name = new AssignNameExpression(self::INTERNAL_VAR_NAME, $node->getTemplateLine());
55+
$this->scope->set('domain', new NameExpression(self::INTERNAL_VAR_NAME, $node->getTemplateLine()));
5556

5657
return new SetNode(false, new Node([$name]), new Node([$node->getNode('expr')]), $node->getTemplateLine());
5758
}
@@ -111,9 +112,4 @@ private function isNamedArguments(Node $arguments): bool
111112

112113
return false;
113114
}
114-
115-
private function getVarName(): string
116-
{
117-
return \sprintf('__internal_%s', hash('xxh128', uniqid(mt_rand(), true)));
118-
}
119115
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.