1 | <?php |
||
10 | final class Provider implements ListenerProviderInterface |
||
11 | { |
||
12 | private $listeners = []; |
||
13 | |||
14 | public function getListenersForEvent(object $event): iterable |
||
35 | |||
36 | /** |
||
37 | * Attaches listener to corresponding event based on the type-hint used for the event argument. |
||
38 | * |
||
39 | * Method signature should be the following: |
||
40 | * |
||
41 | * ``` |
||
42 | * function (MyEvent $event): void |
||
43 | * ``` |
||
44 | * |
||
45 | * Any callable could be used be it a closure, invokable object or array referencing a class or object. |
||
46 | * |
||
47 | * @param callable $listener |
||
48 | */ |
||
49 | public function attach(callable $listener): void |
||
53 | |||
54 | /** |
||
55 | * Detach all event handlers registered for an interface |
||
56 | * |
||
57 | * @param string $interface |
||
58 | */ |
||
59 | public function detach(string $interface): void |
||
63 | |||
64 | /** |
||
65 | * Derives the class type of the first argument of a callable. |
||
66 | * |
||
67 | * @param callable $callable The callable for which we want the parameter type. |
||
68 | * @return string The class the parameter is type hinted on. |
||
69 | */ |
||
70 | private function getParameterType($callable): string |
||
109 | |||
110 | /** |
||
111 | * Determines if a callable represents a function. |
||
112 | * |
||
113 | * Or at least a reasonable approximation, since a function name may not be defined yet. |
||
114 | * |
||
115 | * @param callable $callable |
||
116 | * @return True if the callable represents a function, false otherwise. |
||
117 | */ |
||
118 | private function isFunctionCallable(callable $callable): bool |
||
123 | |||
124 | /** |
||
125 | * Determines if a callable represents a closure/anonymous function. |
||
126 | * |
||
127 | * @param callable $callable |
||
128 | * @return True if the callable represents a closure object, false otherwise. |
||
129 | */ |
||
130 | private function isClosureCallable(callable $callable): bool |
||
134 | |||
135 | /** |
||
136 | * @param callable $callable |
||
137 | * @return True if the callable represents an invokable object, false otherwise. |
||
138 | */ |
||
139 | private function isInvokable(callable $callable): bool |
||
140 | { |
||
141 | return is_object($callable); |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * Determines if a callable represents a method on an object. |
||
146 | * |
||
147 | * @param callable $callable |
||
148 | * @return True if the callable represents a method object, false otherwise. |
||
149 | */ |
||
150 | private function isObjectCallable(callable $callable): bool |
||
154 | |||
155 | /** |
||
156 | * Determines if a callable represents a static class method. |
||
157 | * |
||
158 | * The parameter here is untyped so that this method may be called with an |
||
159 | * array that represents a class name and a non-static method. The routine |
||
160 | * to determine the parameter type is identical to a static method, but such |
||
161 | * an array is still not technically callable. Omitting the parameter type here |
||
162 | * allows us to use this method to handle both cases. |
||
163 | * |
||
164 | * Note that this method must therefore be the first in the switch statement |
||
165 | * above, or else subsequent calls will break as the array is not going to satisfy |
||
166 | * the callable type hint but it would pass `is_callable()`. Because PHP. |
||
167 | * |
||
168 | * @param callable $callable |
||
169 | * @return True if the callable represents a static method, false otherwise. |
||
170 | */ |
||
171 | private function isClassCallable($callable): bool |
||
175 | } |
||
176 |