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;
13
14/**
15 * @author Fabien Potencier <fabien@symfony.com>
16 *
17 * @final since Twig 2.4.0
18 */
19class Profile implements \IteratorAggregate, \Serializable
20{
21    public const ROOT = 'ROOT';
22    public const BLOCK = 'block';
23    public const TEMPLATE = 'template';
24    public const MACRO = 'macro';
25
26    private $template;
27    private $name;
28    private $type;
29    private $starts = [];
30    private $ends = [];
31    private $profiles = [];
32
33    public function __construct(string $template = 'main', string $type = self::ROOT, string $name = 'main')
34    {
35        if (__CLASS__ !== static::class) {
36            @trigger_error('Overriding '.__CLASS__.' is deprecated since Twig 2.4.0 and the class will be final in 3.0.', \E_USER_DEPRECATED);
37        }
38
39        $this->template = $template;
40        $this->type = $type;
41        $this->name = 0 === strpos($name, '__internal_') ? 'INTERNAL' : $name;
42        $this->enter();
43    }
44
45    public function getTemplate()
46    {
47        return $this->template;
48    }
49
50    public function getType()
51    {
52        return $this->type;
53    }
54
55    public function getName()
56    {
57        return $this->name;
58    }
59
60    public function isRoot()
61    {
62        return self::ROOT === $this->type;
63    }
64
65    public function isTemplate()
66    {
67        return self::TEMPLATE === $this->type;
68    }
69
70    public function isBlock()
71    {
72        return self::BLOCK === $this->type;
73    }
74
75    public function isMacro()
76    {
77        return self::MACRO === $this->type;
78    }
79
80    public function getProfiles()
81    {
82        return $this->profiles;
83    }
84
85    public function addProfile(self $profile)
86    {
87        $this->profiles[] = $profile;
88    }
89
90    /**
91     * Returns the duration in microseconds.
92     *
93     * @return float
94     */
95    public function getDuration()
96    {
97        if ($this->isRoot() && $this->profiles) {
98            // for the root node with children, duration is the sum of all child durations
99            $duration = 0;
100            foreach ($this->profiles as $profile) {
101                $duration += $profile->getDuration();
102            }
103
104            return $duration;
105        }
106
107        return isset($this->ends['wt']) && isset($this->starts['wt']) ? $this->ends['wt'] - $this->starts['wt'] : 0;
108    }
109
110    /**
111     * Returns the memory usage in bytes.
112     *
113     * @return int
114     */
115    public function getMemoryUsage()
116    {
117        return isset($this->ends['mu']) && isset($this->starts['mu']) ? $this->ends['mu'] - $this->starts['mu'] : 0;
118    }
119
120    /**
121     * Returns the peak memory usage in bytes.
122     *
123     * @return int
124     */
125    public function getPeakMemoryUsage()
126    {
127        return isset($this->ends['pmu']) && isset($this->starts['pmu']) ? $this->ends['pmu'] - $this->starts['pmu'] : 0;
128    }
129
130    /**
131     * Starts the profiling.
132     */
133    public function enter()
134    {
135        $this->starts = [
136            'wt' => microtime(true),
137            'mu' => memory_get_usage(),
138            'pmu' => memory_get_peak_usage(),
139        ];
140    }
141
142    /**
143     * Stops the profiling.
144     */
145    public function leave()
146    {
147        $this->ends = [
148            'wt' => microtime(true),
149            'mu' => memory_get_usage(),
150            'pmu' => memory_get_peak_usage(),
151        ];
152    }
153
154    public function reset()
155    {
156        $this->starts = $this->ends = $this->profiles = [];
157        $this->enter();
158    }
159
160    #[\ReturnTypeWillChange]
161    public function getIterator(): \Traversable
162    {
163        return new \ArrayIterator($this->profiles);
164    }
165
166    public function serialize(): string
167    {
168        return serialize($this->__serialize());
169    }
170
171    public function unserialize($data): void
172    {
173        $this->__unserialize(unserialize($data));
174    }
175
176    /**
177     * @internal
178     */
179    public function __serialize(): array
180    {
181        return [$this->template, $this->name, $this->type, $this->starts, $this->ends, $this->profiles];
182    }
183
184    /**
185     * @internal
186     */
187    public function __unserialize(array $data): void
188    {
189        list($this->template, $this->name, $this->type, $this->starts, $this->ends, $this->profiles) = $data;
190    }
191}
192
193class_alias('Twig\Profiler\Profile', 'Twig_Profiler_Profile');
194