-
-
Notifications
You must be signed in to change notification settings - Fork 333
Expand file tree
/
Copy pathClockwork.php
More file actions
338 lines (280 loc) · 8 KB
/
Clockwork.php
File metadata and controls
338 lines (280 loc) · 8 KB
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
<?php namespace Clockwork;
use Clockwork\Authentication\AuthenticatorInterface;
use Clockwork\Authentication\NullAuthenticator;
use Clockwork\DataSource\DataSourceInterface;
use Clockwork\Request\Log;
use Clockwork\Request\Request;
use Clockwork\Request\Timeline;
use Clockwork\Storage\StorageInterface;
use Psr\Log\LogLevel;
use Psr\Log\LoggerInterface;
/**
* Main Clockwork class
*/
class Clockwork implements LoggerInterface
{
/**
* Clockwork version
*/
const VERSION = '3.0.2';
/**
* Array of data sources, these objects provide data to be stored in a request object
*/
protected $dataSources = [];
/**
* Request object, data structure which stores data about current application request
*/
protected $request;
/**
* Storage object, provides implementation for storing and retrieving request objects
*/
protected $storage;
// Authenticator implementation, authenticates requests for clockwork metadata
protected $authenticator;
/**
* Request\Log instance, data structure which stores data for the log view
*/
protected $log;
/**
* Request\Timeline instance, data structure which stores data for the timeline view
*/
protected $timeline;
/**
* Create a new Clockwork instance with default request object
*/
public function __construct()
{
$this->request = new Request;
$this->log = new Log;
$this->timeline = new Timeline;
$this->authenticator = new NullAuthenticator;
}
/**
* Add a new data source
*/
public function addDataSource(DataSourceInterface $dataSource)
{
$this->dataSources[] = $dataSource;
return $this;
}
/**
* Return array of all added data sources
*/
public function getDataSources()
{
return $this->dataSources;
}
/**
* Return the request object
*/
public function getRequest()
{
return $this->request;
}
/**
* Set a custom request object
*/
public function setRequest(Request $request)
{
$this->request = $request;
return $this;
}
/**
* Add data from all data sources to request
*/
public function resolveRequest()
{
foreach ($this->dataSources as $dataSource) {
$dataSource->resolve($this->request);
}
// merge global log and timeline data with data collected from data sources
$this->request->log = array_merge($this->request->log, $this->log->toArray());
$this->request->timelineData = array_merge($this->request->timelineData, $this->timeline->finalize($this->request->time));
// sort log and timeline data by time
uasort($this->request->log, function($a, $b) {
if ($a['time'] == $b['time']) return 0;
return $a['time'] < $b['time'] ? -1 : 1;
});
uasort($this->request->timelineData, function($a, $b) {
if ($a['start'] == $b['start']) return 0;
return $a['start'] < $b['start'] ? -1 : 1;
});
return $this;
}
// Extends the request with additional data form all data sources when being shown in the Clockwork app
public function extendRequest(Request $request = null)
{
foreach ($this->dataSources as $dataSource) {
$dataSource->extend($request ?: $this->request);
}
return $this;
}
/**
* Store request via storage object
*/
public function storeRequest()
{
return $this->storage->store($this->request);
}
/**
* Return the storage object
*/
public function getStorage()
{
return $this->storage;
}
/**
* Set a custom storage object
*/
public function setStorage(StorageInterface $storage)
{
$this->storage = $storage;
return $this;
}
/**
* Return the authenticator object
*/
public function getAuthenticator()
{
return $this->authenticator;
}
/**
* Set a custom authenticator object
*/
public function setAuthenticator(AuthenticatorInterface $authenticator)
{
$this->authenticator = $authenticator;
return $this;
}
/**
* Return the log instance
*/
public function getLog()
{
return $this->log;
}
/**
* Set a custom log instance
*/
public function setLog(Log $log)
{
$this->log = $log;
return $this;
}
/**
* Return the timeline instance
*/
public function getTimeline()
{
return $this->timeline;
}
/**
* Set a custom timeline instance
*/
public function setTimeline(Timeline $timeline)
{
$this->timeline = $timeline;
return $this;
}
/**
* Shortcut methods for the current log instance
*/
public function log($level = LogLevel::INFO, $message, array $context = [])
{
return $this->getLog()->log($level, $message, $context);
}
public function emergency($message, array $context = [])
{
return $this->getLog()->log(LogLevel::EMERGENCY, $message, $context);
}
public function alert($message, array $context = [])
{
return $this->getLog()->log(LogLevel::ALERT, $message, $context);
}
public function critical($message, array $context = [])
{
return $this->getLog()->log(LogLevel::CRITICAL, $message, $context);
}
public function error($message, array $context = [])
{
return $this->getLog()->log(LogLevel::ERROR, $message, $context);
}
public function warning($message, array $context = [])
{
return $this->getLog()->log(LogLevel::WARNING, $message, $context);
}
public function notice($message, array $context = [])
{
return $this->getLog()->log(LogLevel::NOTICE, $message, $context);
}
public function info($message, array $context = [])
{
return $this->getLog()->log(LogLevel::INFO, $message, $context);
}
public function debug($message, array $context = [])
{
return $this->getLog()->log(LogLevel::DEBUG, $message, $context);
}
/**
* Shortcut methods for the current timeline instance
*/
public function startEvent($name, $description, $time = null)
{
return $this->getTimeline()->startEvent($name, $description, $time);
}
public function endEvent($name)
{
return $this->getTimeline()->endEvent($name);
}
// Shortcut methods for the Request object
// Add database query, takes query, bindings, duration and additional data - connection (connection name), file
// (caller file name), line (caller line number), trace (serialized trace), model (associated ORM model)
public function addDatabaseQuery($query, $bindings = [], $duration = null, $data = [])
{
return $this->getRequest()->addDatabaseQuery($query, $bindings, $duration, $data);
}
// Add cache query, takes type, key, value and additional data - connection (connection name), file
// (caller file name), line (caller line number), trace (serialized trace), expiration
public function addCacheQuery($type, $key, $value = null, $duration = null, $data = [])
{
return $this->getRequest()->addCacheQuery($type, $key, $value, $duration, $data);
}
// Add event, takes event name, data, time and additional data - listeners, file (caller file name), line (caller
// line number), trace (serialized trace)
public function addEvent($event, $eventData = null, $time = null, $data = [])
{
return $this->getRequest()->addEvent($event, $eventData, $time, $data);
}
// Add route, takes method, uri, action and additional data - name, middleware, before (before filters), after
// (after filters)
public function addRoute($method, $uri, $action, $data = [])
{
return $this->getRequest()->addRoute($method, $uri, $action, $data);
}
// Add route, takes method, uri, action and additional data - name, middleware, before (before filters), after
// (after filters)
public function addEmail($subject, $to, $from = null, $headers = [])
{
return $this->getRequest()->addEmail($subject, $to, $from, $headers);
}
// Add view, takes view name and data
public function addView($name, $data = [])
{
return $this->getRequest()->addView($name, $data);
}
// Record executed subrequest, takes the requested url, returned Clockwork ID and optional path if non-default
public function addSubrequest($url, $id, $path = null)
{
return $this->getRequest()->addSubrequest($url, $id, $path);
}
// DEPRECATED Use addSubrequest method
public function subrequest($url, $id, $path = null)
{
return $this->getRequest()->addSubrequest($url, $id, $path);
}
// Add custom user data (presented as additional tabs in the official app)
public function userData($key = null)
{
return $this->getRequest()->userData($key);
}
}