25 protected $listeners = [];
35 function on($eventName, callable $callBack, $priority = 100) {
37 if (!isset($this->
listeners[$eventName])) {
45 $this->
listeners[$eventName][1][] = $priority;
46 $this->
listeners[$eventName][2][] = $callBack;
59 function once($eventName, callable $callBack, $priority = 100) {
62 $wrapper =
function() use ($eventName, $callBack, &$wrapper) {
65 return call_user_func_array($callBack, func_get_args());
69 $this->
on($eventName, $wrapper, $priority);
99 function emit($eventName, array $arguments = [], callable $continueCallBack = null) {
101 if (is_null($continueCallBack)) {
103 foreach ($this->
listeners($eventName) as $listener) {
105 $result = call_user_func_array($listener, $arguments);
113 $listeners = $this->
listeners($eventName);
114 $counter = count($listeners);
116 foreach ($listeners as $listener) {
119 $result = call_user_func_array($listener, $arguments);
125 if (!$continueCallBack())
break;
147 if (!isset($this->
listeners[$eventName])) {
155 array_multisort($this->
listeners[$eventName][1], SORT_NUMERIC, $this->
listeners[$eventName][2]);
177 if (!isset($this->
listeners[$eventName])) {
181 if ($check === $listener) {
183 unset($this->
listeners[$eventName][2][$index]);
203 if (!is_null($eventName)) {
on($eventName, callable $callBack, $priority=100)
Subscribe to an event.
removeListener($eventName, callable $listener)
Removes a specific listener from an event.
removeAllListeners($eventName=null)
Removes all listeners.
once($eventName, callable $callBack, $priority=100)
Subscribe to an event exactly once.
emit($eventName, array $arguments=[], callable $continueCallBack=null)
Emits an event.
listeners($eventName)
Returns the list of listeners for an event.