composer package updates
[openemr.git] / vendor / twig / twig / lib / Twig / Profiler / Profile.php
blob3fdc1a8ae2dbeff29142264e187dae0646ceed41
1 <?php
3 /*
4 * This file is part of Twig.
6 * (c) Fabien Potencier
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 /**
13 * @author Fabien Potencier <fabien@symfony.com>
15 * @final
17 class Twig_Profiler_Profile implements IteratorAggregate, Serializable
19 const ROOT = 'ROOT';
20 const BLOCK = 'block';
21 const TEMPLATE = 'template';
22 const MACRO = 'macro';
24 private $template;
25 private $name;
26 private $type;
27 private $starts = array();
28 private $ends = array();
29 private $profiles = array();
31 public function __construct($template = 'main', $type = self::ROOT, $name = 'main')
33 $this->template = $template;
34 $this->type = $type;
35 $this->name = 0 === strpos($name, '__internal_') ? 'INTERNAL' : $name;
36 $this->enter();
39 public function getTemplate()
41 return $this->template;
44 public function getType()
46 return $this->type;
49 public function getName()
51 return $this->name;
54 public function isRoot()
56 return self::ROOT === $this->type;
59 public function isTemplate()
61 return self::TEMPLATE === $this->type;
64 public function isBlock()
66 return self::BLOCK === $this->type;
69 public function isMacro()
71 return self::MACRO === $this->type;
74 public function getProfiles()
76 return $this->profiles;
79 public function addProfile(Twig_Profiler_Profile $profile)
81 $this->profiles[] = $profile;
84 /**
85 * Returns the duration in microseconds.
87 * @return int
89 public function getDuration()
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();
98 return $duration;
101 return isset($this->ends['wt']) && isset($this->starts['wt']) ? $this->ends['wt'] - $this->starts['wt'] : 0;
105 * Returns the memory usage in bytes.
107 * @return int
109 public function getMemoryUsage()
111 return isset($this->ends['mu']) && isset($this->starts['mu']) ? $this->ends['mu'] - $this->starts['mu'] : 0;
115 * Returns the peak memory usage in bytes.
117 * @return int
119 public function getPeakMemoryUsage()
121 return isset($this->ends['pmu']) && isset($this->starts['pmu']) ? $this->ends['pmu'] - $this->starts['pmu'] : 0;
125 * Starts the profiling.
127 public function enter()
129 $this->starts = array(
130 'wt' => microtime(true),
131 'mu' => memory_get_usage(),
132 'pmu' => memory_get_peak_usage(),
137 * Stops the profiling.
139 public function leave()
141 $this->ends = array(
142 'wt' => microtime(true),
143 'mu' => memory_get_usage(),
144 'pmu' => memory_get_peak_usage(),
148 public function reset()
150 $this->starts = $this->ends = $this->profiles = array();
151 $this->enter();
154 public function getIterator()
156 return new ArrayIterator($this->profiles);
159 public function serialize()
161 return serialize(array($this->template, $this->name, $this->type, $this->starts, $this->ends, $this->profiles));
164 public function unserialize($data)
166 list($this->template, $this->name, $this->type, $this->starts, $this->ends, $this->profiles) = unserialize($data);
170 class_alias('Twig_Profiler_Profile', 'Twig\Profiler\Profile', false);