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
12namespace Twig\Profiler\Dumper;
13
14use Twig\Profiler\Profile;
15
16/**
17 * @author Fabien Potencier <fabien@symfony.com>
18 */
19abstract class BaseDumper
20{
21    private $root;
22
23    public function dump(Profile $profile)
24    {
25        return $this->dumpProfile($profile);
26    }
27
28    abstract protected function formatTemplate(Profile $profile, $prefix);
29
30    abstract protected function formatNonTemplate(Profile $profile, $prefix);
31
32    abstract protected function formatTime(Profile $profile, $percent);
33
34    private function dumpProfile(Profile $profile, $prefix = '', $sibling = false)
35    {
36        if ($profile->isRoot()) {
37            $this->root = $profile->getDuration();
38            $start = $profile->getName();
39        } else {
40            if ($profile->isTemplate()) {
41                $start = $this->formatTemplate($profile, $prefix);
42            } else {
43                $start = $this->formatNonTemplate($profile, $prefix);
44            }
45            $prefix .= $sibling ? '│ ' : '  ';
46        }
47
48        $percent = $this->root ? $profile->getDuration() / $this->root * 100 : 0;
49
50        if ($profile->getDuration() * 1000 < 1) {
51            $str = $start."\n";
52        } else {
53            $str = sprintf("%s %s\n", $start, $this->formatTime($profile, $percent));
54        }
55
56        $nCount = \count($profile->getProfiles());
57        foreach ($profile as $i => $p) {
58            $str .= $this->dumpProfile($p, $prefix, $i + 1 !== $nCount);
59        }
60
61        return $str;
62    }
63}
64
65class_alias('Twig\Profiler\Dumper\BaseDumper', 'Twig_Profiler_Dumper_Base');
66