4 * This file is part of Twig.
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
13 * @author Fabien Potencier <fabien@symfony.com>
17 class Twig_Profiler_NodeVisitor_Profiler extends Twig_BaseNodeVisitor
19 private $extensionName;
21 public function __construct($extensionName)
23 $this->extensionName = $extensionName;
26 protected function doEnterNode(Twig_Node $node, Twig_Environment $env)
31 protected function doLeaveNode(Twig_Node $node, Twig_Environment $env)
33 if ($node instanceof Twig_Node_Module) {
34 $varName = $this->getVarName();
35 $node->setNode('display_start', new Twig_Node(array(new Twig_Profiler_Node_EnterProfile($this->extensionName, Twig_Profiler_Profile::TEMPLATE, $node->getTemplateName(), $varName), $node->getNode('display_start'))));
36 $node->setNode('display_end', new Twig_Node(array(new Twig_Profiler_Node_LeaveProfile($varName), $node->getNode('display_end'))));
37 } elseif ($node instanceof Twig_Node_Block) {
38 $varName = $this->getVarName();
39 $node->setNode('body', new Twig_Node_Body(array(
40 new Twig_Profiler_Node_EnterProfile($this->extensionName, Twig_Profiler_Profile::BLOCK, $node->getAttribute('name'), $varName),
41 $node->getNode('body'),
42 new Twig_Profiler_Node_LeaveProfile($varName),
44 } elseif ($node instanceof Twig_Node_Macro) {
45 $varName = $this->getVarName();
46 $node->setNode('body', new Twig_Node_Body(array(
47 new Twig_Profiler_Node_EnterProfile($this->extensionName, Twig_Profiler_Profile::MACRO, $node->getAttribute('name'), $varName),
48 $node->getNode('body'),
49 new Twig_Profiler_Node_LeaveProfile($varName),
56 private function getVarName()
58 return sprintf('__internal_%s', hash('sha256', $this->extensionName));
61 public function getPriority()
67 class_alias('Twig_Profiler_NodeVisitor_Profiler', 'Twig\Profiler\NodeVisitor\ProfilerNodeVisitor', false);