Hashing is not necessary to generate unique variable names during compilation

This commit is contained in:
Jérôme Tamarelle
2021-12-13 20:18:05 +01:00
committed by Fabien Potencier
parent a41a6cce62
commit ba2b4e6dfe
3 changed files with 10 additions and 16 deletions
+1 -1
View File
@@ -238,7 +238,7 @@ class Compiler
public function getVarName()
{
return sprintf('__internal_%s', hash('sha256', __METHOD__.$this->varNameSalt++));
return sprintf('__internal_compile_%d', $this->varNameSalt++);
}
}
+1 -1
View File
@@ -56,7 +56,7 @@ class Parser
public function getVarName()
{
return sprintf('__internal_%s', hash('sha256', __METHOD__.$this->stream->getSourceContext()->getCode().$this->varNameSalt++));
return sprintf('__internal_parse_%d', $this->varNameSalt++);
}
public function parse(TokenStream $stream, $test = null, $dropNeedle = false)
@@ -28,10 +28,12 @@ use Twig\Profiler\Profile;
final class ProfilerNodeVisitor extends AbstractNodeVisitor
{
private $extensionName;
private $varName;
public function __construct(string $extensionName)
{
$this->extensionName = $extensionName;
$this->varName = sprintf('__internal_%s', hash('sha256', $extensionName));
}
protected function doEnterNode(Node $node, Environment $env)
@@ -42,33 +44,25 @@ final class ProfilerNodeVisitor extends AbstractNodeVisitor
protected function doLeaveNode(Node $node, Environment $env)
{
if ($node instanceof ModuleNode) {
$varName = $this->getVarName();
$node->setNode('display_start', new Node([new EnterProfileNode($this->extensionName, Profile::TEMPLATE, $node->getTemplateName(), $varName), $node->getNode('display_start')]));
$node->setNode('display_end', new Node([new LeaveProfileNode($varName), $node->getNode('display_end')]));
$node->setNode('display_start', new Node([new EnterProfileNode($this->extensionName, Profile::TEMPLATE, $node->getTemplateName(), $this->varName), $node->getNode('display_start')]));
$node->setNode('display_end', new Node([new LeaveProfileNode($this->varName), $node->getNode('display_end')]));
} elseif ($node instanceof BlockNode) {
$varName = $this->getVarName();
$node->setNode('body', new BodyNode([
new EnterProfileNode($this->extensionName, Profile::BLOCK, $node->getAttribute('name'), $varName),
new EnterProfileNode($this->extensionName, Profile::BLOCK, $node->getAttribute('name'), $this->varName),
$node->getNode('body'),
new LeaveProfileNode($varName),
new LeaveProfileNode($this->varName),
]));
} elseif ($node instanceof MacroNode) {
$varName = $this->getVarName();
$node->setNode('body', new BodyNode([
new EnterProfileNode($this->extensionName, Profile::MACRO, $node->getAttribute('name'), $varName),
new EnterProfileNode($this->extensionName, Profile::MACRO, $node->getAttribute('name'), $this->varName),
$node->getNode('body'),
new LeaveProfileNode($varName),
new LeaveProfileNode($this->varName),
]));
}
return $node;
}
private function getVarName(): string
{
return sprintf('__internal_%s', hash('sha256', $this->extensionName));
}
public function getPriority()
{
return 0;