|
1
|
|
|
<?php namespace nyx\events\interfaces; |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Event Emitter Interface |
|
5
|
|
|
* |
|
6
|
|
|
* Event synchronization point. Registers/removes listeners for events and emits events. Supports registering |
|
7
|
|
|
* listeners by priority and subscribers. |
|
8
|
|
|
* |
|
9
|
|
|
* @version 0.1.0 |
|
10
|
|
|
* @author Michal Chojnacki <[email protected]> |
|
11
|
|
|
* @copyright 2012-2017 Nyx Dev Team |
|
12
|
|
|
* @link https://github.com/unyx/nyx |
|
13
|
|
|
* @todo Decide: Optionally append the Emitter instance and event type to the payload when emitting? |
|
14
|
|
|
* @todo Decide: Event propagation stopping? |
|
15
|
|
|
* @todo Decide: Wildcard events / channel support? |
|
16
|
|
|
*/ |
|
17
|
|
|
interface Emitter |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* Triggers the listeners of a given event. |
|
21
|
|
|
* |
|
22
|
|
|
* @param string|Event $event The name of the event to emit or an object implementing the Event |
|
23
|
|
|
* interface, whose name will be used. |
|
24
|
|
|
* @param mixed ...$payload The data to pass to listeners, in the order given. If $event is an |
|
25
|
|
|
* instance of the Event interface, it will be prepended to the $payload. |
|
26
|
|
|
*/ |
|
27
|
|
|
public function emit($event, ...$payload); |
|
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Adds an event listener that listens to the specified event. |
|
31
|
|
|
* |
|
32
|
|
|
* @param string $event The event to listen for. |
|
33
|
|
|
* @param callable $listener The listener to add. |
|
34
|
|
|
* @param integer $priority The higher this value, the earlier an event listener will be triggered. |
|
35
|
|
|
* @return $this |
|
36
|
|
|
*/ |
|
37
|
|
|
public function on(string $event, callable $listener, int $priority = 0) : Emitter; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Adds an event listener that listens to the specified event but only gets fired once and then removed from |
|
41
|
|
|
* the stack. |
|
42
|
|
|
* |
|
43
|
|
|
* @param string $event The event to listen for. |
|
44
|
|
|
* @param callable $listener The listener to add. |
|
45
|
|
|
* @param integer $priority The higher this value, the earlier an event listener will be triggered |
|
46
|
|
|
* in the chain. |
|
47
|
|
|
* @return $this |
|
48
|
|
|
*/ |
|
49
|
|
|
public function once(string $event, callable $listener, int $priority = 0) : Emitter; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Removes the specified listener from the stack of a given event, OR |
|
53
|
|
|
* removes all listeners for a given event name when only $listener is null, OR |
|
54
|
|
|
* removes the given listener from all events it's listening to when only $name is null, OR |
|
55
|
|
|
* removes all listeners registered in this Emitter if both $name and $listener are null, |
|
56
|
|
|
* |
|
57
|
|
|
* @param string $event The event to remove the listener from. |
|
58
|
|
|
* @param callable $listener The listener to remove. |
|
59
|
|
|
* @return $this |
|
60
|
|
|
*/ |
|
61
|
|
|
public function off(string $event = null, callable $listener = null) : Emitter; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Registers an Event Subscriber with this Emitter. The Subscriber is queried for a list of events |
|
65
|
|
|
* he is interested in and afterwards registered as a listener for them. |
|
66
|
|
|
* |
|
67
|
|
|
* @param Subscriber $subscriber |
|
68
|
|
|
* @return $this |
|
69
|
|
|
*/ |
|
70
|
|
|
public function register(Subscriber $subscriber) : Emitter; |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Deregisters an Event Subscriber from this Emitter, removing all listeners it has registered |
|
74
|
|
|
* with. |
|
75
|
|
|
* |
|
76
|
|
|
* @param Subscriber $subscriber |
|
77
|
|
|
* @return $this |
|
78
|
|
|
*/ |
|
79
|
|
|
public function deregister(Subscriber $subscriber) : Emitter; |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Returns an array containing the priority-sorted listeners for the given event or for all events |
|
83
|
|
|
* if no event name is given. |
|
84
|
|
|
* |
|
85
|
|
|
* @param string $event The name of the event. |
|
86
|
|
|
* @return array The event listeners for the specified event. |
|
87
|
|
|
*/ |
|
88
|
|
|
public function getListeners(string $event = null) : array; |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Checks whether the given event has any listeners or when no event name is given whether any listeners |
|
92
|
|
|
* at all are registered. |
|
93
|
|
|
* |
|
94
|
|
|
* @param string $event The name of the event. |
|
95
|
|
|
* @return bool |
|
96
|
|
|
*/ |
|
97
|
|
|
public function hasListeners(string $event = null) : bool; |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Returns the count of the listeners for the given event or the count of all registered listeners if |
|
101
|
|
|
* no event name is given. |
|
102
|
|
|
* |
|
103
|
|
|
* @param string $event The name of the event. |
|
104
|
|
|
* @return int |
|
105
|
|
|
*/ |
|
106
|
|
|
public function countListeners(string $event = null) : int; |
|
107
|
|
|
} |
|
108
|
|
|
|
For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a
@returndoc comment to communicate to implementors of these methods what they are expected to return.