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