Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
79.59% |
39 / 49 |
|
60.00% |
3 / 5 |
CRAP | |
0.00% |
0 / 1 |
EventManager | |
79.59% |
39 / 49 |
|
60.00% |
3 / 5 |
30.31 | |
0.00% |
0 / 1 |
trigger | |
60.87% |
14 / 23 |
|
0.00% |
0 / 1 |
13.85 | |||
attach | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
6 | |||
removeListener | |
85.71% |
6 / 7 |
|
0.00% |
0 / 1 |
4.05 | |||
detach | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
4 | |||
getListeners | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | |
3 | /** |
4 | * This program is free software; you can redistribute it and/or |
5 | * modify it under the terms of the GNU General Public License |
6 | * as published by the Free Software Foundation; under version 2 |
7 | * of the License (non-upgradable). |
8 | * |
9 | * This program is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | * GNU General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU General Public License |
15 | * along with this program; if not, write to the Free Software |
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
17 | * |
18 | * Copyright (c) 2015-2022 (original work) Open Assessment Technologies SA; |
19 | * |
20 | */ |
21 | |
22 | namespace oat\oatbox\event; |
23 | |
24 | use common_Logger; |
25 | use oat\oatbox\service\ConfigurableService; |
26 | use oat\oatbox\service\ServiceNotFoundException; |
27 | use Throwable; |
28 | |
29 | /** |
30 | * The simple placeholder ServiceManager |
31 | * @author Joel Bout <joel@taotesting.com> |
32 | */ |
33 | class EventManager extends ConfigurableService |
34 | { |
35 | public const SERVICE_ID = 'generis/event'; |
36 | |
37 | /** |
38 | * @deprecated use SERVICE_ID |
39 | */ |
40 | public const CONFIG_ID = 'generis/event'; |
41 | |
42 | public const OPTION_LISTENERS = 'listeners'; |
43 | |
44 | /** |
45 | * Dispatch an event and trigger its listeners |
46 | * |
47 | * @param mixed $event either an Event object or a string |
48 | * @param array $params |
49 | */ |
50 | public function trigger($event, $params = []) |
51 | { |
52 | $container = $this->getServiceManager()->getContainer(); |
53 | |
54 | $event = is_object($event) ? $event : new GenericEvent($event, $params); |
55 | |
56 | foreach ($this->getListeners($event) as $callback) { |
57 | $callbackName = $callback; |
58 | |
59 | if (is_array($callback) && count($callback) == 2) { |
60 | [$key, $function] = $callback; |
61 | |
62 | if (is_object($key)) { |
63 | $callbackName = sprintf('%s::%s', get_class($key), $function); |
64 | } |
65 | |
66 | if (is_string($key)) { |
67 | $callbackName = sprintf('%s::%s', $key, $function); |
68 | |
69 | try { |
70 | $service = $container->get($key); |
71 | $callback = [$service, $function]; |
72 | } catch (ServiceNotFoundException $e) { |
73 | //do nothing |
74 | } |
75 | } |
76 | } |
77 | |
78 | if (!is_callable($callback)) { |
79 | common_Logger::w( |
80 | sprintf( |
81 | 'Event manager cannot call %s because it is not a callable. ' |
82 | . 'Notice, that classes registered in DI container cannot be executed during the installation', |
83 | $callbackName |
84 | ) |
85 | ); |
86 | |
87 | continue; |
88 | } |
89 | |
90 | call_user_func($callback, $event); |
91 | } |
92 | } |
93 | |
94 | /** |
95 | * Attach a Listener to one or multiple events |
96 | * |
97 | * @param mixed $event either an Event object or a string |
98 | * @param callable $callback |
99 | */ |
100 | public function attach($event, $callback) |
101 | { |
102 | $events = is_array($event) ? $event : [$event]; |
103 | $listeners = $this->getOption(self::OPTION_LISTENERS); |
104 | foreach ($events as $event) { |
105 | $eventObject = is_object($event) ? $event : new GenericEvent($event); |
106 | if (!isset($listeners[$eventObject->getName()])) { |
107 | $listeners[$eventObject->getName()] = []; |
108 | } |
109 | |
110 | if (!in_array($callback, $listeners[$eventObject->getName()], true)) { |
111 | $listeners[$eventObject->getName()][] = $callback; |
112 | } |
113 | } |
114 | $this->setOption(self::OPTION_LISTENERS, $listeners); |
115 | } |
116 | |
117 | /** |
118 | * remove listener from an event and delete event if it dosn't have any listeners |
119 | * @param array $listeners |
120 | * @param string $eventName |
121 | * @param callable $callback |
122 | * @return array |
123 | */ |
124 | protected function removeListener(array $listeners, $eventObject, $callback) |
125 | { |
126 | if (isset($listeners[$eventObject->getName()])) { |
127 | if (($index = array_search($callback, $listeners[$eventObject->getName()])) !== false) { |
128 | unset($listeners[$eventObject->getName()][$index]); |
129 | if (empty($listeners[$eventObject->getName()])) { |
130 | unset($listeners[$eventObject->getName()]); |
131 | } else { |
132 | $listeners[$eventObject->getName()] = array_values($listeners[$eventObject->getName()]); |
133 | } |
134 | } |
135 | } |
136 | return $listeners; |
137 | } |
138 | |
139 | |
140 | /** |
141 | * Detach a Listener from one or multiple events |
142 | * |
143 | * @param mixed $event either an Event object or a string |
144 | * @param Callable $callback |
145 | */ |
146 | public function detach($event, $callback) |
147 | { |
148 | $events = is_array($event) ? $event : [$event]; |
149 | $listeners = $this->getOption(self::OPTION_LISTENERS); |
150 | foreach ($events as $event) { |
151 | $eventObject = is_object($event) ? $event : new GenericEvent($event); |
152 | $listeners = $this->removeListener($listeners, $eventObject, $callback); |
153 | } |
154 | $this->setOption(self::OPTION_LISTENERS, $listeners); |
155 | } |
156 | |
157 | /** |
158 | * Get all Listeners listening to this kind of event |
159 | * |
160 | * @param Event $eventObject |
161 | * @return Callable[] listeners associated with this event |
162 | */ |
163 | protected function getListeners(Event $eventObject) |
164 | { |
165 | $listeners = $this->getOption(self::OPTION_LISTENERS); |
166 | return isset($listeners[$eventObject->getName()]) |
167 | ? $listeners[$eventObject->getName()] |
168 | : []; |
169 | } |
170 | } |