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