|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Event Dispatcher Class |
|
4
|
|
|
* |
|
5
|
|
|
* @package BlueEvent |
|
6
|
|
|
* @author Michał Adamiak <[email protected]> |
|
7
|
|
|
* @copyright chajr/bluetree |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace BlueEvent\Event\Base; |
|
11
|
|
|
|
|
12
|
|
|
use BlueEvent\Event\Base\Interfaces\EventDispatcherInterface; |
|
13
|
|
|
use BlueEvent\Event\Base\Interfaces\EventInterface; |
|
14
|
|
|
use BlueEvent\Event\BaseEvent; |
|
15
|
|
|
|
|
16
|
|
|
class EventDispatcher implements EventDispatcherInterface |
|
17
|
|
|
{ |
|
18
|
|
|
const EVENT_STATUS_OK = 'ok'; |
|
19
|
|
|
const EVENT_STATUS_ERROR = 'error'; |
|
20
|
|
|
const EVENT_STATUS_BREAK = 'propagation_stop'; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var bool |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $hasErrors = false; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* store all errors |
|
29
|
|
|
* |
|
30
|
|
|
* @var |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $errorList = []; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* store logger instance |
|
36
|
|
|
* |
|
37
|
|
|
* @var EventLog |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $loggerInstance; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* store default options for event dispatcher |
|
43
|
|
|
* |
|
44
|
|
|
* @var array |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $options = [ |
|
47
|
|
|
'type' => 'array', |
|
48
|
|
|
'log_events' => false, |
|
49
|
|
|
'log_all_events' => true, |
|
50
|
|
|
'from_file' => false, |
|
51
|
|
|
'log_object' => false, |
|
52
|
|
|
'log_config' => [ |
|
53
|
|
|
'log_path' => './log', |
|
54
|
|
|
'level' => 'debug', |
|
55
|
|
|
'storage' => \SimpleLog\Storage\File::class, |
|
56
|
|
|
], |
|
57
|
|
|
'events' => [], |
|
58
|
|
|
]; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* create manage instance |
|
62
|
|
|
* |
|
63
|
|
|
* @param array $options |
|
64
|
|
|
* @throws \InvalidArgumentException |
|
65
|
|
|
*/ |
|
66
|
|
|
public function __construct(array $options = []) |
|
67
|
|
|
{ |
|
68
|
|
|
$this->options = array_replace_recursive($this->options, $options); |
|
69
|
|
|
|
|
70
|
|
|
if ($this->options['from_file']) { |
|
71
|
|
|
$this->readEventConfiguration( |
|
72
|
|
|
$this->options['from_file'], |
|
73
|
|
|
$this->options['type'] |
|
74
|
|
|
); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$this->loggerInstance = new EventLog($this->options); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* return event object or create it if not exist |
|
82
|
|
|
* |
|
83
|
|
|
* @param string $eventName |
|
84
|
|
|
* @param array $data |
|
85
|
|
|
* @return EventInterface |
|
86
|
|
|
* @throws \LogicException |
|
87
|
|
|
* @throws \InvalidArgumentException |
|
88
|
|
|
*/ |
|
89
|
|
|
protected function createEventObject($eventName, array $data) |
|
90
|
|
|
{ |
|
91
|
|
|
if (!array_key_exists($eventName, $this->options['events'])) { |
|
92
|
|
|
throw new \InvalidArgumentException('Event is not defined.'); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
$namespace = $this->options['events'][$eventName]['object']; |
|
96
|
|
|
$instance = new $namespace($eventName, $data); |
|
97
|
|
|
|
|
98
|
|
|
if (!($instance instanceof EventInterface)) { |
|
99
|
|
|
throw new \LogicException('Invalid interface of event object'); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return $instance; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* add event configuration into event dispatcher |
|
107
|
|
|
* |
|
108
|
|
|
* @param array $events |
|
109
|
|
|
* @return $this |
|
110
|
|
|
*/ |
|
111
|
|
|
public function setEventConfiguration(array $events) |
|
112
|
|
|
{ |
|
113
|
|
|
$this->options['events'] = array_merge_recursive( |
|
114
|
|
|
$this->options['events'], |
|
115
|
|
|
$events |
|
116
|
|
|
); |
|
117
|
|
|
|
|
118
|
|
|
return $this; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* trigger new event with automatic call all subscribed listeners |
|
123
|
|
|
* |
|
124
|
|
|
* @param string $name |
|
125
|
|
|
* @param array $data |
|
126
|
|
|
* @return $this |
|
127
|
|
|
* @throws \LogicException |
|
128
|
|
|
*/ |
|
129
|
|
|
public function triggerEvent($name, array $data = []) |
|
130
|
|
|
{ |
|
131
|
|
|
try { |
|
132
|
|
|
/** @var EventInterface $event */ |
|
133
|
|
|
$event = $this->createEventObject($name, $data); |
|
134
|
|
|
} catch (\InvalidArgumentException $exception) { |
|
135
|
|
|
return $this; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
foreach ($this->options['events'][$name]['listeners'] as $eventListener) { |
|
139
|
|
|
if ($event->isPropagationStopped()) { |
|
140
|
|
|
$this->loggerInstance->makeLogEvent($name, $eventListener, self::EVENT_STATUS_BREAK); |
|
141
|
|
|
break; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
$this->executeListener($eventListener, $event, $name); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
return $this; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* @param mixed $eventListener |
|
152
|
|
|
* @param EventInterface $event |
|
153
|
|
|
* @param string $name |
|
154
|
|
|
* @return $this |
|
155
|
|
|
*/ |
|
156
|
|
|
protected function executeListener($eventListener, EventInterface $event, $name) |
|
157
|
|
|
{ |
|
158
|
|
|
try { |
|
159
|
|
|
$this->callFunction($eventListener, $event); |
|
160
|
|
|
$status = self::EVENT_STATUS_OK; |
|
161
|
|
|
} catch (\Exception $e) { |
|
162
|
|
|
$this->addError($e); |
|
163
|
|
|
$status = self::EVENT_STATUS_ERROR; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
$this->loggerInstance->makeLogEvent($name, $eventListener, $status); |
|
167
|
|
|
|
|
168
|
|
|
return $this; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* dynamically add new listener or listeners for given event name |
|
173
|
|
|
* listeners are added at end of the list |
|
174
|
|
|
* |
|
175
|
|
|
* @param string $eventName |
|
176
|
|
|
* @param array $listeners |
|
177
|
|
|
* @return $this |
|
178
|
|
|
*/ |
|
179
|
|
|
public function addEventListener($eventName, array $listeners) |
|
180
|
|
|
{ |
|
181
|
|
|
if (!array_key_exists($eventName, $this->options['events'])) { |
|
182
|
|
|
$this->options['events'][$eventName] = [ |
|
183
|
|
|
'object' =>BaseEvent::class, |
|
184
|
|
|
'listeners' => $listeners, |
|
185
|
|
|
]; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
$this->options['events'][$eventName]['listeners'] = array_merge( |
|
189
|
|
|
$this->options['events'][$eventName]['listeners'], |
|
190
|
|
|
$listeners |
|
191
|
|
|
); |
|
192
|
|
|
|
|
193
|
|
|
return $this; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* allow to call event listeners functions |
|
198
|
|
|
* |
|
199
|
|
|
* @param mixed $listener |
|
200
|
|
|
* @param EventInterface $event |
|
201
|
|
|
*/ |
|
202
|
|
|
protected function callFunction($listener, EventInterface $event) |
|
203
|
|
|
{ |
|
204
|
|
|
if (is_callable($listener)) { |
|
205
|
|
|
call_user_func($listener, $event); |
|
206
|
|
|
} |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* read configuration from file |
|
211
|
|
|
* |
|
212
|
|
|
* @param mixed $path |
|
213
|
|
|
* @param string $type |
|
214
|
|
|
* @return $this |
|
215
|
|
|
* @throws \InvalidArgumentException |
|
216
|
|
|
*/ |
|
217
|
|
|
public function readEventConfiguration($path, $type) |
|
218
|
|
|
{ |
|
219
|
|
|
if (!file_exists($path)) { |
|
220
|
|
|
throw new \InvalidArgumentException('File ' . $path . 'don\'t exists.'); |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
$name = '\BlueEvent\Event\Config\\' . ucfirst($type) . 'Config'; |
|
224
|
|
|
|
|
225
|
|
|
if (!class_exists($name)) { |
|
226
|
|
|
throw new \InvalidArgumentException('Incorrect configuration type: ' . $type); |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** @var \BlueEvent\Event\Config\ConfigReader $reader */ |
|
230
|
|
|
$reader = new $name; |
|
231
|
|
|
|
|
232
|
|
|
return $this->setEventConfiguration($reader->readConfig($path)); |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
/** |
|
236
|
|
|
* disable or enable event logging (true to enable) |
|
237
|
|
|
* |
|
238
|
|
|
* @param bool $logEvents |
|
239
|
|
|
* @return $this |
|
240
|
|
|
*/ |
|
241
|
|
|
public function setEventLog($logEvents) |
|
242
|
|
|
{ |
|
243
|
|
|
$this->options['log_events'] = (bool)$logEvents; |
|
244
|
|
|
return $this; |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* log given events by given name |
|
249
|
|
|
* |
|
250
|
|
|
* @param array $events |
|
251
|
|
|
* @return $this |
|
252
|
|
|
*/ |
|
253
|
|
|
public function logEvent(array $events = []) |
|
254
|
|
|
{ |
|
255
|
|
|
foreach ($events as $event) { |
|
256
|
|
|
if (!in_array($event, $this->loggerInstance->logEvents, true)) { |
|
257
|
|
|
$this->loggerInstance->logEvents[] = $event; |
|
258
|
|
|
} |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
return $this; |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
/** |
|
265
|
|
|
* enable or disable log all events |
|
266
|
|
|
* |
|
267
|
|
|
* @param bool $log |
|
268
|
|
|
* @return $this |
|
269
|
|
|
*/ |
|
270
|
|
|
public function logAllEvents($log = true) |
|
271
|
|
|
{ |
|
272
|
|
|
$this->options['log_all_events'] = (bool)$log; |
|
273
|
|
|
return $this; |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
/** |
|
277
|
|
|
* get complete object configuration or value of single option |
|
278
|
|
|
* |
|
279
|
|
|
* @param $option string|null |
|
280
|
|
|
* @return mixed |
|
281
|
|
|
*/ |
|
282
|
|
|
public function getConfiguration($option = null) |
|
283
|
|
|
{ |
|
284
|
|
|
if (!is_null($option)) { |
|
285
|
|
|
return $this->options[$option]; |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
return $this->options; |
|
289
|
|
|
} |
|
290
|
|
|
|
|
291
|
|
|
/** |
|
292
|
|
|
* return list of all events to log |
|
293
|
|
|
* |
|
294
|
|
|
* @return array |
|
295
|
|
|
*/ |
|
296
|
|
|
public function getAllEventsToLog() |
|
297
|
|
|
{ |
|
298
|
|
|
return $this->loggerInstance->logEvents; |
|
299
|
|
|
} |
|
300
|
|
|
|
|
301
|
|
|
/** |
|
302
|
|
|
* return current event configuration |
|
303
|
|
|
* |
|
304
|
|
|
* @return array |
|
305
|
|
|
*/ |
|
306
|
|
|
public function getEventConfiguration() |
|
307
|
|
|
{ |
|
308
|
|
|
return $this->options['events']; |
|
309
|
|
|
} |
|
310
|
|
|
|
|
311
|
|
|
/** |
|
312
|
|
|
* return all event dispatcher errors |
|
313
|
|
|
* |
|
314
|
|
|
* @return array |
|
315
|
|
|
*/ |
|
316
|
|
|
public function getErrors() |
|
317
|
|
|
{ |
|
318
|
|
|
return $this->errorList; |
|
319
|
|
|
} |
|
320
|
|
|
|
|
321
|
|
|
/** |
|
322
|
|
|
* return information that event dispatcher has some errors |
|
323
|
|
|
* |
|
324
|
|
|
* @return bool |
|
325
|
|
|
*/ |
|
326
|
|
|
public function hasErrors() |
|
327
|
|
|
{ |
|
328
|
|
|
return $this->hasErrors; |
|
329
|
|
|
} |
|
330
|
|
|
|
|
331
|
|
|
/** |
|
332
|
|
|
* clear all event dispatcher errors |
|
333
|
|
|
* |
|
334
|
|
|
* @return $this |
|
335
|
|
|
*/ |
|
336
|
|
|
public function clearErrors() |
|
337
|
|
|
{ |
|
338
|
|
|
$this->errorList = []; |
|
339
|
|
|
$this->hasErrors = false; |
|
340
|
|
|
|
|
341
|
|
|
return $this; |
|
342
|
|
|
} |
|
343
|
|
|
|
|
344
|
|
|
/** |
|
345
|
|
|
* add new error to list |
|
346
|
|
|
* |
|
347
|
|
|
* @param \Exception $exception |
|
348
|
|
|
* @return $this |
|
349
|
|
|
*/ |
|
350
|
|
|
protected function addError(\Exception $exception) |
|
351
|
|
|
{ |
|
352
|
|
|
$this->errorList[$exception->getCode()] = [ |
|
353
|
|
|
'message' => $exception->getMessage(), |
|
354
|
|
|
'line' => $exception->getLine(), |
|
355
|
|
|
'file' => $exception->getFile(), |
|
356
|
|
|
'trace' => $exception->getTraceAsString(), |
|
357
|
|
|
]; |
|
358
|
|
|
$this->hasErrors = true; |
|
359
|
|
|
|
|
360
|
|
|
return $this; |
|
361
|
|
|
} |
|
362
|
|
|
} |
|
363
|
|
|
|