ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
EventEmitterTrait.php
Go to the documentation of this file.
1<?php
2
3namespace Sabre\Event;
4
19
25 protected $listeners = [];
26
35 function on($eventName, callable $callBack, $priority = 100) {
36
37 if (!isset($this->listeners[$eventName])) {
38 $this->listeners[$eventName] = [
39 true, // If there's only one item, it's sorted
40 [$priority],
41 [$callBack]
42 ];
43 } else {
44 $this->listeners[$eventName][0] = false; // marked as unsorted
45 $this->listeners[$eventName][1][] = $priority;
46 $this->listeners[$eventName][2][] = $callBack;
47 }
48
49 }
50
59 function once($eventName, callable $callBack, $priority = 100) {
60
61 $wrapper = null;
62 $wrapper = function() use ($eventName, $callBack, &$wrapper) {
63
64 $this->removeListener($eventName, $wrapper);
65 return call_user_func_array($callBack, func_get_args());
66
67 };
68
69 $this->on($eventName, $wrapper, $priority);
70
71 }
72
99 function emit($eventName, array $arguments = [], callable $continueCallBack = null) {
100
101 if (is_null($continueCallBack)) {
102
103 foreach ($this->listeners($eventName) as $listener) {
104
105 $result = call_user_func_array($listener, $arguments);
106 if ($result === false) {
107 return false;
108 }
109 }
110
111 } else {
112
113 $listeners = $this->listeners($eventName);
114 $counter = count($listeners);
115
116 foreach ($listeners as $listener) {
117
118 $counter--;
119 $result = call_user_func_array($listener, $arguments);
120 if ($result === false) {
121 return false;
122 }
123
124 if ($counter > 0) {
125 if (!$continueCallBack()) break;
126 }
127
128 }
129
130 }
131
132 return true;
133
134 }
135
145 function listeners($eventName) {
146
147 if (!isset($this->listeners[$eventName])) {
148 return [];
149 }
150
151 // The list is not sorted
152 if (!$this->listeners[$eventName][0]) {
153
154 // Sorting
155 array_multisort($this->listeners[$eventName][1], SORT_NUMERIC, $this->listeners[$eventName][2]);
156
157 // Marking the listeners as sorted
158 $this->listeners[$eventName][0] = true;
159 }
160
161 return $this->listeners[$eventName][2];
162
163 }
164
175 function removeListener($eventName, callable $listener) {
176
177 if (!isset($this->listeners[$eventName])) {
178 return false;
179 }
180 foreach ($this->listeners[$eventName][2] as $index => $check) {
181 if ($check === $listener) {
182 unset($this->listeners[$eventName][1][$index]);
183 unset($this->listeners[$eventName][2][$index]);
184 return true;
185 }
186 }
187 return false;
188
189 }
190
201 function removeAllListeners($eventName = null) {
202
203 if (!is_null($eventName)) {
204 unset($this->listeners[$eventName]);
205 } else {
206 $this->listeners = [];
207 }
208
209 }
210
211}
$result
An exception for terminatinating execution or to throw for unit testing.
$index
Definition: metadata.php:60
removeListener($eventName, callable $listener)
Removes a specific listener from an event.
emit($eventName, array $arguments=[], callable $continueCallBack=null)
Emits an event.
trait EventEmitterTrait
Event Emitter Trait.
once($eventName, callable $callBack, $priority=100)
Subscribe to an event exactly once.
on($eventName, callable $callBack, $priority=100)
Subscribe to an event.
removeAllListeners($eventName=null)
Removes all listeners.
listeners($eventName)
Returns the list of listeners for an event.