| 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; |
| 12 |
|
| 13 |
/** |
| 14 |
* @author Fabien Potencier <fabien@symfony.com> |
| 15 |
*/ |
| 16 |
final class Profile implements \IteratorAggregate, \Serializable |
| 17 |
{ |
| 18 |
public const ROOT = 'ROOT'; |
| 19 |
public const BLOCK = 'block'; |
| 20 |
public const TEMPLATE = 'template'; |
| 21 |
public const MACRO = 'macro'; |
| 22 |
private $template; |
| 23 |
private $name; |
| 24 |
private $type; |
| 25 |
private $starts = []; |
| 26 |
private $ends = []; |
| 27 |
private $profiles = []; |
| 28 |
public function __construct(string $template = 'main', string $type = self::ROOT, string $name = 'main') |
| 29 |
{ |
| 30 |
$this->template = $template; |
| 31 |
$this->type = $type; |
| 32 |
$this->name = \str_starts_with($name, '__internal_') ? 'INTERNAL' : $name; |
| 33 |
$this->enter(); |
| 34 |
} |
| 35 |
public function getTemplate() : string |
| 36 |
{ |
| 37 |
return $this->template; |
| 38 |
} |
| 39 |
public function getType() : string |
| 40 |
{ |
| 41 |
return $this->type; |
| 42 |
} |
| 43 |
public function getName() : string |
| 44 |
{ |
| 45 |
return $this->name; |
| 46 |
} |
| 47 |
public function isRoot() : bool |
| 48 |
{ |
| 49 |
return self::ROOT === $this->type; |
| 50 |
} |
| 51 |
public function isTemplate() : bool |
| 52 |
{ |
| 53 |
return self::TEMPLATE === $this->type; |
| 54 |
} |
| 55 |
public function isBlock() : bool |
| 56 |
{ |
| 57 |
return self::BLOCK === $this->type; |
| 58 |
} |
| 59 |
public function isMacro() : bool |
| 60 |
{ |
| 61 |
return self::MACRO === $this->type; |
| 62 |
} |
| 63 |
/** |
| 64 |
* @return Profile[] |
| 65 |
*/ |
| 66 |
public function getProfiles() : array |
| 67 |
{ |
| 68 |
return $this->profiles; |
| 69 |
} |
| 70 |
public function addProfile(self $profile) : void |
| 71 |
{ |
| 72 |
$this->profiles[] = $profile; |
| 73 |
} |
| 74 |
/** |
| 75 |
* Returns the duration in microseconds. |
| 76 |
*/ |
| 77 |
public function getDuration() : float |
| 78 |
{ |
| 79 |
if ($this->isRoot() && $this->profiles) { |
| 80 |
// for the root node with children, duration is the sum of all child durations |
| 81 |
$duration = 0; |
| 82 |
foreach ($this->profiles as $profile) { |
| 83 |
$duration += $profile->getDuration(); |
| 84 |
} |
| 85 |
return $duration; |
| 86 |
} |
| 87 |
return isset($this->ends['wt']) && isset($this->starts['wt']) ? $this->ends['wt'] - $this->starts['wt'] : 0; |
| 88 |
} |
| 89 |
/** |
| 90 |
* Returns the memory usage in bytes. |
| 91 |
*/ |
| 92 |
public function getMemoryUsage() : int |
| 93 |
{ |
| 94 |
return isset($this->ends['mu']) && isset($this->starts['mu']) ? $this->ends['mu'] - $this->starts['mu'] : 0; |
| 95 |
} |
| 96 |
/** |
| 97 |
* Returns the peak memory usage in bytes. |
| 98 |
*/ |
| 99 |
public function getPeakMemoryUsage() : int |
| 100 |
{ |
| 101 |
return isset($this->ends['pmu']) && isset($this->starts['pmu']) ? $this->ends['pmu'] - $this->starts['pmu'] : 0; |
| 102 |
} |
| 103 |
/** |
| 104 |
* Starts the profiling. |
| 105 |
*/ |
| 106 |
public function enter() : void |
| 107 |
{ |
| 108 |
$this->starts = ['wt' => \microtime(\true), 'mu' => \memory_get_usage(), 'pmu' => \memory_get_peak_usage()]; |
| 109 |
} |
| 110 |
/** |
| 111 |
* Stops the profiling. |
| 112 |
*/ |
| 113 |
public function leave() : void |
| 114 |
{ |
| 115 |
$this->ends = ['wt' => \microtime(\true), 'mu' => \memory_get_usage(), 'pmu' => \memory_get_peak_usage()]; |
| 116 |
} |
| 117 |
public function reset() : void |
| 118 |
{ |
| 119 |
$this->starts = $this->ends = $this->profiles = []; |
| 120 |
$this->enter(); |
| 121 |
} |
| 122 |
public function getIterator() : \Traversable |
| 123 |
{ |
| 124 |
return new \ArrayIterator($this->profiles); |
| 125 |
} |
| 126 |
public function serialize() : string |
| 127 |
{ |
| 128 |
return \serialize($this->__serialize()); |
| 129 |
} |
| 130 |
public function unserialize($data) : void |
| 131 |
{ |
| 132 |
$this->__unserialize(\unserialize($data)); |
| 133 |
} |
| 134 |
/** |
| 135 |
* @internal |
| 136 |
*/ |
| 137 |
public function __serialize() : array |
| 138 |
{ |
| 139 |
return [$this->template, $this->name, $this->type, $this->starts, $this->ends, $this->profiles]; |
| 140 |
} |
| 141 |
/** |
| 142 |
* @internal |
| 143 |
*/ |
| 144 |
public function __unserialize(array $data) : void |
| 145 |
{ |
| 146 |
[$this->template, $this->name, $this->type, $this->starts, $this->ends, $this->profiles] = $data; |
| 147 |
} |
| 148 |
} |
| 149 |
|