1 | <?php |
||
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 = []) |
||
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) |
||
104 | |||
105 | /** |
||
106 | * add event configuration into event dispatcher |
||
107 | * |
||
108 | * @param array $events |
||
109 | * @return $this |
||
110 | */ |
||
111 | public function setEventConfiguration(array $events) |
||
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 = []) |
||
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) |
||
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) |
||
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) |
||
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) |
||
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) |
||
246 | |||
247 | /** |
||
248 | * log given events by given name |
||
249 | * |
||
250 | * @param array $events |
||
251 | * @return $this |
||
252 | */ |
||
253 | public function logEvent(array $events = []) |
||
263 | |||
264 | /** |
||
265 | * enable or disable log all events |
||
266 | * |
||
267 | * @param bool $log |
||
268 | * @return $this |
||
269 | */ |
||
270 | public function logAllEvents($log = true) |
||
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) |
||
290 | |||
291 | /** |
||
292 | * return list of all events to log |
||
293 | * |
||
294 | * @return array |
||
295 | */ |
||
296 | public function getAllEventsToLog() |
||
300 | |||
301 | /** |
||
302 | * return current event configuration |
||
303 | * |
||
304 | * @return array |
||
305 | */ |
||
306 | public function getEventConfiguration() |
||
310 | |||
311 | /** |
||
312 | * return all event dispatcher errors |
||
313 | * |
||
314 | * @return array |
||
315 | */ |
||
316 | public function getErrors() |
||
320 | |||
321 | /** |
||
322 | * return information that event dispatcher has some errors |
||
323 | * |
||
324 | * @return bool |
||
325 | */ |
||
326 | public function hasErrors() |
||
330 | |||
331 | /** |
||
332 | * clear all event dispatcher errors |
||
333 | * |
||
334 | * @return $this |
||
335 | */ |
||
336 | public function clearErrors() |
||
343 | |||
344 | /** |
||
345 | * add new error to list |
||
346 | * |
||
347 | * @param \Exception $exception |
||
348 | * @return $this |
||
349 | */ |
||
350 | protected function addError(\Exception $exception) |
||
362 | } |
||
363 |