|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Nip Framework |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
use Nip\Profiler\Adapters\AbstractAdapter; |
|
8
|
|
|
use Nip\Profiler\Profile; |
|
9
|
|
|
|
|
10
|
|
|
class Nip_Profiler |
|
|
|
|
|
|
11
|
|
|
{ |
|
12
|
|
|
|
|
13
|
|
|
public $enabled = false; |
|
14
|
|
|
|
|
15
|
|
|
public $profiles = []; |
|
16
|
|
|
public $runningProfiles = []; |
|
17
|
|
|
public $filterElapsedSecs = null; |
|
18
|
|
|
/** |
|
19
|
|
|
* @var AbstractAdapter[] |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $writers = []; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Singleton |
|
25
|
|
|
* |
|
26
|
|
|
* @return Nip_Profiler |
|
27
|
|
|
*/ |
|
28
|
|
|
public static function instance() |
|
29
|
|
|
{ |
|
30
|
|
|
static $instance; |
|
31
|
|
|
if (!($instance instanceof self)) { |
|
32
|
|
|
$instance = new self(); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
return $instance; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param bool $enabled |
|
40
|
|
|
* @return $this |
|
41
|
|
|
*/ |
|
42
|
|
|
public function setEnabled($enabled = false) |
|
43
|
|
|
{ |
|
44
|
|
|
$this->enabled = (boolean)$enabled; |
|
45
|
|
|
return $this; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function clear() |
|
49
|
|
|
{ |
|
50
|
|
|
$this->profiles = []; |
|
51
|
|
|
|
|
52
|
|
|
return $this; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
public function start($profileName = false) |
|
57
|
|
|
{ |
|
58
|
|
|
if (!$this->checkEnabled()) { |
|
59
|
|
|
return; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$profileID = $this->newProfileID($profileName); |
|
63
|
|
|
|
|
64
|
|
|
$profile = $this->newProfile($profileID); |
|
65
|
|
|
$profile->setName($profileName); |
|
66
|
|
|
|
|
67
|
|
|
$this->profiles[$profileID] = $profile; |
|
68
|
|
|
$this->addRunningProces($profileID); |
|
69
|
|
|
return $profile; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function checkEnabled() |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->enabled; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function newProfileID($name) |
|
78
|
|
|
{ |
|
79
|
|
|
if ($name) { |
|
80
|
|
|
return $name; |
|
81
|
|
|
} |
|
82
|
|
|
$profilesCount = count($this->getProfiles(null, true)); |
|
83
|
|
|
return 'profile' . $profilesCount; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function getProfiles($type = null, $showUnfinished = false) |
|
87
|
|
|
{ |
|
88
|
|
|
|
|
89
|
|
|
$profiles = []; |
|
90
|
|
|
foreach ($this->profiles as $key => $profile) { |
|
91
|
|
|
if ($type === null) { |
|
92
|
|
|
$condition = true; |
|
93
|
|
|
} else { |
|
94
|
|
|
$condition = ($profile->type && $type); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
if (($profile->hasEnded() || $showUnfinished) && $condition) { |
|
98
|
|
|
$profiles[$key] = $profile; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
if (empty($profiles)) { |
|
103
|
|
|
$profiles = false; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
return $profiles; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public function newProfile($id) |
|
110
|
|
|
{ |
|
111
|
|
|
return new Profile($id); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
public function addRunningProces($profileID) |
|
115
|
|
|
{ |
|
116
|
|
|
$this->runningProfiles[] = $profileID; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
public function end($profileID = false) |
|
120
|
|
|
{ |
|
121
|
|
|
$profileID = $this->endPreckeck($profileID); |
|
122
|
|
|
if ($profileID) { |
|
123
|
|
|
$profile = $this->endProfile($profileID); |
|
124
|
|
|
if ($this->applyFilters($profile)) { |
|
125
|
|
|
$this->outputWriters($profile); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
return; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
protected function endPreckeck($profileID) |
|
132
|
|
|
{ |
|
133
|
|
|
if (!$this->checkEnabled()) { |
|
134
|
|
|
return; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
if ($profileID == false) { |
|
138
|
|
|
$profileID = $this->getLastRunningProces(); |
|
139
|
|
|
} |
|
140
|
|
|
return $profileID; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
public function getLastRunningProces() |
|
144
|
|
|
{ |
|
145
|
|
|
return array_pop($this->runningProfiles); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
public function endProfile($profileID) |
|
149
|
|
|
{ |
|
150
|
|
|
$profile = $this->getProfile($profileID); |
|
151
|
|
|
$profile->end(); |
|
152
|
|
|
|
|
153
|
|
|
$key = array_search($profileID, $this->runningProfiles); |
|
154
|
|
|
if (is_numeric($key)) { |
|
155
|
|
|
unset($this->runningProfiles[$key]); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
return $profile; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
public function getProfile($profileID) |
|
162
|
|
|
{ |
|
163
|
|
|
if (is_object($profileID)) { |
|
164
|
|
|
return $profileID; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
if (!array_key_exists($profileID, $this->profiles)) { |
|
168
|
|
|
trigger_error("Profile handle '$profileID' not found in profiler log.", E_USER_ERROR); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
$profile = $this->profiles[$profileID]; |
|
172
|
|
|
|
|
173
|
|
|
return $profile; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
protected function applyFilters($profile) |
|
177
|
|
|
{ |
|
178
|
|
|
return $this->secondsFilter($profile); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
public function secondsFilter($profile) |
|
182
|
|
|
{ |
|
183
|
|
|
if ($profile && null !== $this->filterElapsedSecs && $profile->getElapsedSecs() < $this->filterElapsedSecs) { |
|
184
|
|
|
$this->deleteProfile($profile); |
|
185
|
|
|
return false; |
|
186
|
|
|
} |
|
187
|
|
|
return true; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
public function deleteProfile($profile) |
|
191
|
|
|
{ |
|
192
|
|
|
if (!array_key_exists($profile->profileID, $this->profiles)) { |
|
193
|
|
|
trigger_error("Query handle '{$profile->profileID}' not found in profiler log.", E_USER_ERROR); |
|
194
|
|
|
} |
|
195
|
|
|
unset($this->profiles[$profile->profileID]); |
|
196
|
|
|
return; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
public function outputWriters($profile) |
|
200
|
|
|
{ |
|
201
|
|
|
foreach ($this->writers as $writer) { |
|
202
|
|
|
$writer->write($profile); |
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
public function lastProcessID() |
|
207
|
|
|
{ |
|
208
|
|
|
end($this->profiles); |
|
209
|
|
|
|
|
210
|
|
|
return key($this->profiles); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
public function setFilterElapsedSecs($minimumSeconds = null) |
|
214
|
|
|
{ |
|
215
|
|
|
if (null === $minimumSeconds) { |
|
216
|
|
|
$this->filterElapsedSecs = null; |
|
217
|
|
|
} else { |
|
218
|
|
|
$this->filterElapsedSecs = (integer)$minimumSeconds; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
return $this; |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* @param AbstractAdapter $writer |
|
226
|
|
|
*/ |
|
227
|
|
|
public function addWriter(AbstractAdapter $writer) |
|
228
|
|
|
{ |
|
229
|
|
|
$this->writers[] = $writer; |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* @param $name |
|
234
|
|
|
* @return AbstractAdapter |
|
235
|
|
|
*/ |
|
236
|
|
|
public function newWriter($name) |
|
237
|
|
|
{ |
|
238
|
|
|
$class = $this->newWriterClass($name); |
|
239
|
|
|
$writer = new $class(); |
|
240
|
|
|
return $writer; |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
/** |
|
244
|
|
|
* @param $name |
|
245
|
|
|
* @return string |
|
246
|
|
|
*/ |
|
247
|
|
|
public function newWriterClass($name) |
|
248
|
|
|
{ |
|
249
|
|
|
return 'Nip\Profiler\Adapters\\' . ucfirst($name); |
|
250
|
|
|
} |
|
251
|
|
|
} |
|
252
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.