1 | <?php declare(strict_types = 1); |
||
14 | class EventDispatcher implements EventDispatcherContract |
||
15 | { |
||
16 | /** |
||
17 | * Array of events, being dispatched now. |
||
18 | * |
||
19 | * @var array |
||
20 | */ |
||
21 | private $dispatching = []; |
||
22 | |||
23 | /** |
||
24 | * Array of defined events and it listeners. |
||
25 | * |
||
26 | * @var array |
||
27 | */ |
||
28 | private $listeners = []; |
||
29 | |||
30 | /** |
||
31 | * Sorted listeners array cache. |
||
32 | * |
||
33 | * @var array |
||
34 | */ |
||
35 | private $sortedListeners = []; |
||
36 | |||
37 | /** |
||
38 | * @inheritDoc |
||
39 | * @throws InvalidListenerException |
||
40 | */ |
||
41 | 3 | public function addListener(string $eventName, $listener, int $priority = 0) |
|
50 | |||
51 | /** |
||
52 | * @inheritDoc |
||
53 | */ |
||
54 | 3 | public function dispatch(Event $event) |
|
73 | |||
74 | /** |
||
75 | * Performs call of the listener. |
||
76 | * |
||
77 | * @param mixed $listener |
||
78 | * @param Event $event |
||
79 | */ |
||
80 | 3 | protected function callListener($listener, Event $event) |
|
84 | |||
85 | /** |
||
86 | * Defines, if listener can be called. |
||
87 | * |
||
88 | * @param mixed $listener |
||
89 | * @return bool |
||
90 | */ |
||
91 | 3 | protected function canBeCalled($listener): bool |
|
95 | |||
96 | /** |
||
97 | * Returns array of event listeners. |
||
98 | * |
||
99 | * @param string $eventName |
||
100 | * @return array |
||
101 | */ |
||
102 | 3 | protected function getListeners(string $eventName): array |
|
114 | |||
115 | /** |
||
116 | * Merges all event listeners for particular event. |
||
117 | * |
||
118 | * @param string $eventName |
||
119 | * @return array |
||
120 | */ |
||
121 | 3 | protected function sortEventListeners(string $eventName): array |
|
136 | |||
137 | /** |
||
138 | * Returns event name. |
||
139 | * |
||
140 | * @param Event $event |
||
141 | * @return string |
||
142 | */ |
||
143 | 3 | private function getEventName(Event $event) |
|
147 | } |