This repository has been archived by the owner on Jan 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 130
/
Profiler.php
226 lines (194 loc) · 5.09 KB
/
Profiler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<?php
/**
* @see https://github.com/zendframework/zend-developer-tools for the canonical source repository
* @copyright Copyright (c) 2011-2018 Zend Technologies USA Inc. (https://www.zend.com)
* @license https://github.com/zendframework/zend-developer-tools/blob/master/LICENSE.md New BSD License
*/
namespace ZendDeveloperTools;
use DateTime;
use DateTimezone;
use Zend\EventManager\EventInterface;
use Zend\EventManager\EventManagerAwareInterface;
use Zend\EventManager\EventManagerInterface;
use Zend\Mvc\MvcEvent;
use Zend\Stdlib\PriorityQueue;
class Profiler implements EventManagerAwareInterface
{
/**
* Event collector listener priority.
*
* @var int
*/
const PRIORITY_EVENT_COLLECTOR = PHP_INT_MAX;
/**
* FirePHP listener priority.
*
* @var int
*/
const PRIORITY_FIREPHP = 500;
/**
* Flush listener priority.
* Note: The Priority must be lower than PRIORITY_PROFILER!
*
* @var int
*/
const PRIORITY_FLUSH = -9400;
/**
* Profiler listener priority.
*
* @var int
*/
const PRIORITY_PROFILER = -9500;
/**
* Storage listener priority.
*
* @var int
*/
const PRIORITY_STORAGE = 100;
/**
* Toolbar listener priority.
*
* @var int
*/
const PRIORITY_TOOLBAR = 500;
/**
* @var bool
*/
protected $strict;
/**
* @var ProfilerEvent
*/
protected $event;
/**
* @var ReportInterface
*/
protected $report;
/**
* @var PriorityQueue
*/
protected $collectors;
/**
* @var EventManagerInterface
*/
protected $eventManager;
/**
* Constructor.
*
* @param ReportInterface $report
*/
public function __construct(ReportInterface $report)
{
$this->report = $report;
}
/**
* Set the error mode.
*
* @param bool $mode
* @return self
*/
public function setErrorMode($mode)
{
$this->strict = $mode;
return $this;
}
/**
* Set the profiler event object.
*
* @param EventInterface $event
* @return self
*/
public function setEvent(EventInterface $event)
{
$this->event = $event;
return $this;
}
/**
* Returns the profiler event object.
*
* @return self
*/
public function getEvent()
{
if (! isset($this->event)) {
$this->event = new ProfilerEvent();
$this->event->setTarget($this);
$this->event->setProfiler($this);
}
return $this->event;
}
/**
* Set the event manager instance
*
* @param EventManagerInterface $eventManager
* @return self
*/
public function setEventManager(EventManagerInterface $eventManager)
{
$eventManager->addIdentifiers([__CLASS__, get_called_class(), 'profiler']);
$this->eventManager = $eventManager;
return $this;
}
/**
* Get the event manager instance
*
* @return EventManagerInterface
*/
public function getEventManager()
{
return $this->eventManager;
}
/**
* Adds a collector.
*
* @param Collector\CollectorInterface $collector
* @return self
* @throws Exception\CollectorException
*/
public function addCollector($collector)
{
if (! isset($this->collectors)) {
$this->collectors = new PriorityQueue();
}
if ($collector instanceof Collector\CollectorInterface) {
$this->collectors->insert($collector, $collector->getPriority());
return $this;
}
$error = sprintf('%s must implement CollectorInterface.', get_class($collector));
if ($this->strict === true) {
throw new Exception\CollectorException($error);
}
$this->report->addError($error);
return $this;
}
/**
* Runs all collectors.
*
* @triggers ProfilerEvent::EVENT_COLLECTED
* @param MvcEvent $mvcEvent
* @return Profiler
* @throws Exception\ProfilerException
*/
public function collect(MvcEvent $mvcEvent)
{
$this->report->setToken(uniqid('zdt'))
->setUri($mvcEvent->getRequest()->getUriString())
->setMethod($mvcEvent->getRequest()->getMethod())
->setTime(new DateTime('now', new DateTimeZone('UTC')))
->setIp($mvcEvent->getRequest()->getServer()->get('REMOTE_ADDR'));
if (isset($this->collectors)) {
foreach ($this->collectors as $collector) {
$collector->collect($mvcEvent);
$this->report->addCollector($collector);
}
$event = $this->getEvent();
$event->setName(ProfilerEvent::EVENT_COLLECTED);
$this->eventManager->triggerEvent($event);
return $this;
}
if ($this->strict === true) {
throw new Exception\ProfilerException('There is nothing to collect.');
}
$this->report->addError('There is nothing to collect.');
return $this;
}
}