1 | <?php |
||
13 | class Mediator implements Observable |
||
14 | { |
||
15 | /** |
||
16 | * @api |
||
17 | * |
||
18 | * @var array Holds any published events to which no handler has yet subscribed |
||
19 | * |
||
20 | * @since 1.0 |
||
21 | */ |
||
22 | public $held = []; |
||
23 | |||
24 | /** |
||
25 | * @internal |
||
26 | * |
||
27 | * @var bool Whether we should put published events for which there are no subscribers onto the list. |
||
28 | * |
||
29 | * @since 1.0 |
||
30 | */ |
||
31 | protected $holdingUnheardEvents = false; |
||
32 | |||
33 | /** |
||
34 | * @internal |
||
35 | * |
||
36 | * @var Manager |
||
37 | * |
||
38 | * @since 1.0 |
||
39 | */ |
||
40 | protected $mgr; |
||
41 | |||
42 | /** |
||
43 | * |
||
44 | */ |
||
45 | public function __construct(Manager $mgr) |
||
49 | |||
50 | /** |
||
51 | * Registers event handler(s) to event name(s). |
||
52 | * |
||
53 | * @api |
||
54 | * |
||
55 | * @throws BadMethodCallException if validation of any handler fails |
||
56 | * |
||
57 | * @param array $eventHandlers Associative array of event names & handlers |
||
58 | * |
||
59 | * @return array The results of firing any held events |
||
60 | * |
||
61 | * @since 1.0 |
||
62 | * |
||
63 | * @version 1.0 |
||
64 | */ |
||
65 | public function subscribe(array $eventHandlers) |
||
91 | |||
92 | /** |
||
93 | * Let any relevant subscribers know an event needs to be handled. |
||
94 | * |
||
95 | * Note: The event object can be used to share information to other similar event handlers. |
||
96 | * |
||
97 | * @api |
||
98 | * |
||
99 | * @param Event $event An event object, usually freshly created |
||
100 | * |
||
101 | * @return mixed Result of the event |
||
102 | * |
||
103 | * @since 1.0 |
||
104 | * |
||
105 | * @version 1.0 |
||
106 | */ |
||
107 | public function publish(Event $event) |
||
127 | |||
128 | /** |
||
129 | * Detach a given handler (or all) from an event name. |
||
130 | * |
||
131 | * @api |
||
132 | * |
||
133 | * @param array $eventHandlers Associative array of event names & handlers |
||
134 | * |
||
135 | * @return self This object |
||
136 | * |
||
137 | * @since 1.0 |
||
138 | * |
||
139 | * @version 1.0 |
||
140 | */ |
||
141 | public function unsubscribe(array $eventHandlers) |
||
167 | |||
168 | /** |
||
169 | * Get or set the value of the holdingUnheardEvents property. |
||
170 | * |
||
171 | * @api |
||
172 | * |
||
173 | * @param bool|null $val true or false to set the value, omit to retrieve |
||
174 | * |
||
175 | * @return bool the value of the property |
||
176 | * |
||
177 | * @since 1.0 |
||
178 | * |
||
179 | * @version 1.0 |
||
180 | */ |
||
181 | public function holdUnheardEvents($val = null) |
||
194 | |||
195 | /** |
||
196 | * If any events are held for $eventName, re-publish them now. |
||
197 | * |
||
198 | * @internal |
||
199 | * |
||
200 | * @param string $eventName The event name to check for |
||
201 | * |
||
202 | * @since 1.0 |
||
203 | * |
||
204 | * @version 1.0 |
||
205 | */ |
||
206 | protected function fireHeldEvents($eventName) |
||
220 | |||
221 | /** |
||
222 | * |
||
223 | */ |
||
224 | protected function formatCallback($eventName, $callback) |
||
243 | |||
244 | /** |
||
245 | * Puts an event on the held list if enabled and not a timer. |
||
246 | * |
||
247 | * @internal |
||
248 | * |
||
249 | * @param Event $event The event object to be held |
||
250 | * |
||
251 | * @since 1.0 |
||
252 | * |
||
253 | * @version 1.0 |
||
254 | */ |
||
255 | protected function tryHolding(Event $event) |
||
261 | |||
262 | /** |
||
263 | * |
||
264 | */ |
||
265 | protected static function isValidHandler($handler) |
||
272 | } |
||
273 |