Version 1
[yaffs-website] / vendor / twig / twig / lib / Twig / Profiler / Dumper / Text.php
1 <?php
2
3 /*
4  * This file is part of Twig.
5  *
6  * (c) 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  * @final
16  */
17 class Twig_Profiler_Dumper_Text
18 {
19     private $root;
20
21     public function dump(Twig_Profiler_Profile $profile)
22     {
23         return $this->dumpProfile($profile);
24     }
25
26     protected function formatTemplate(Twig_Profiler_Profile $profile, $prefix)
27     {
28         return sprintf('%sā”” %s', $prefix, $profile->getTemplate());
29     }
30
31     protected function formatNonTemplate(Twig_Profiler_Profile $profile, $prefix)
32     {
33         return sprintf('%sā”” %s::%s(%s)', $prefix, $profile->getTemplate(), $profile->getType(), $profile->getName());
34     }
35
36     protected function formatTime(Twig_Profiler_Profile $profile, $percent)
37     {
38         return sprintf('%.2fms/%.0f%%', $profile->getDuration() * 1000, $percent);
39     }
40
41     private function dumpProfile(Twig_Profiler_Profile $profile, $prefix = '', $sibling = false)
42     {
43         if ($profile->isRoot()) {
44             $this->root = $profile->getDuration();
45             $start = $profile->getName();
46         } else {
47             if ($profile->isTemplate()) {
48                 $start = $this->formatTemplate($profile, $prefix);
49             } else {
50                 $start = $this->formatNonTemplate($profile, $prefix);
51             }
52             $prefix .= $sibling ? 'ā”‚ ' : '  ';
53         }
54
55         $percent = $this->root ? $profile->getDuration() / $this->root * 100 : 0;
56
57         if ($profile->getDuration() * 1000 < 1) {
58             $str = $start."\n";
59         } else {
60             $str = sprintf("%s %s\n", $start, $this->formatTime($profile, $percent));
61         }
62
63         $nCount = count($profile->getProfiles());
64         foreach ($profile as $i => $p) {
65             $str .= $this->dumpProfile($p, $prefix, $i + 1 !== $nCount);
66         }
67
68         return $str;
69     }
70 }