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_Dumper_Text
16
{
17
    private $root;
18

    
19
    public function dump(Twig_Profiler_Profile $profile)
20
    {
21
        return $this->dumpProfile($profile);
22
    }
23

    
24
    protected function formatTemplate(Twig_Profiler_Profile $profile, $prefix)
25
    {
26
        return sprintf('%s└ %s', $prefix, $profile->getTemplate());
27
    }
28

    
29
    protected function formatNonTemplate(Twig_Profiler_Profile $profile, $prefix)
30
    {
31
        return sprintf('%s└ %s::%s(%s)', $prefix, $profile->getTemplate(), $profile->getType(), $profile->getName());
32
    }
33

    
34
    protected function formatTime(Twig_Profiler_Profile $profile, $percent)
35
    {
36
        return sprintf('%.2fms/%.0f%%', $profile->getDuration() * 1000, $percent);
37
    }
38

    
39
    private function dumpProfile(Twig_Profiler_Profile $profile, $prefix = '', $sibling = false)
40
    {
41
        if ($profile->isRoot()) {
42
            $this->root = $profile->getDuration();
43
            $start = $profile->getName();
44
        } else {
45
            if ($profile->isTemplate()) {
46
                $start = $this->formatTemplate($profile, $prefix);
47
            } else {
48
                $start = $this->formatNonTemplate($profile, $prefix);
49
            }
50
            $prefix .= $sibling ? '│ ' : '  ';
51
        }
52

    
53
        $percent = $this->root ? $profile->getDuration() / $this->root * 100 : 0;
54

    
55
        if ($profile->getDuration() * 1000 < 1) {
56
            $str = $start."\n";
57
        } else {
58
            $str = sprintf("%s %s\n", $start, $this->formatTime($profile, $percent));
59
        }
60

    
61
        $nCount = count($profile->getProfiles());
62
        foreach ($profile as $i => $p) {
63
            $str .= $this->dumpProfile($p, $prefix, $i + 1 !== $nCount);
64
        }
65

    
66
        return $str;
67
    }
68
}
(3-3/3)