mirror of
https://github.com/twigphp/Twig.git
synced 2026-08-30 12:06:56 +00:00
Hashing is not necessary to generate unique variable names during compilation
This commit is contained in:
committed by
Fabien Potencier
parent
a41a6cce62
commit
ba2b4e6dfe
+1
-1
@@ -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
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user