Project

General

Profile

1
<?php
2

    
3
/*
4
 * This file is part of Twig.
5
 *
6
 * (c) 2015 Fabien Potencier
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

    
12
/**
13
 * @author Fabien Potencier <fabien@symfony.com>
14
 */
15
class Twig_Profiler_NodeVisitor_Profiler extends Twig_BaseNodeVisitor
16
{
17
    private $extensionName;
18

    
19
    public function __construct($extensionName)
20
    {
21
        $this->extensionName = $extensionName;
22
    }
23

    
24
    /**
25
     * {@inheritdoc}
26
     */
27
    protected function doEnterNode(Twig_Node $node, Twig_Environment $env)
28
    {
29
        return $node;
30
    }
31

    
32
    /**
33
     * {@inheritdoc}
34
     */
35
    protected function doLeaveNode(Twig_Node $node, Twig_Environment $env)
36
    {
37
        if ($node instanceof Twig_Node_Module) {
38
            $varName = $this->getVarName();
39
            $node->setNode('display_start', new Twig_Node(array(new Twig_Profiler_Node_EnterProfile($this->extensionName, Twig_Profiler_Profile::TEMPLATE, $node->getAttribute('filename'), $varName), $node->getNode('display_start'))));
40
            $node->setNode('display_end', new Twig_Node(array(new Twig_Profiler_Node_LeaveProfile($varName), $node->getNode('display_end'))));
41
        } elseif ($node instanceof Twig_Node_Block) {
42
            $varName = $this->getVarName();
43
            $node->setNode('body', new Twig_Node_Body(array(
44
                new Twig_Profiler_Node_EnterProfile($this->extensionName, Twig_Profiler_Profile::BLOCK, $node->getAttribute('name'), $varName),
45
                $node->getNode('body'),
46
                new Twig_Profiler_Node_LeaveProfile($varName),
47
            )));
48
        } elseif ($node instanceof Twig_Node_Macro) {
49
            $varName = $this->getVarName();
50
            $node->setNode('body', new Twig_Node_Body(array(
51
                new Twig_Profiler_Node_EnterProfile($this->extensionName, Twig_Profiler_Profile::MACRO, $node->getAttribute('name'), $varName),
52
                $node->getNode('body'),
53
                new Twig_Profiler_Node_LeaveProfile($varName),
54
            )));
55
        }
56

    
57
        return $node;
58
    }
59

    
60
    private function getVarName()
61
    {
62
        return sprintf('__internal_%s', hash('sha256', uniqid(mt_rand(), true), false));
63
    }
64

    
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function getPriority()
69
    {
70
        return 0;
71
    }
72
}
    (1-1/1)