@@ -34,20 +34,6 @@ class Dispatcher implements DispatcherContract
3434 */
3535 protected $ wildcards = [];
3636
37- /**
38- * The sorted event listeners.
39- *
40- * @var array
41- */
42- protected $ sorted = [];
43-
44- /**
45- * The event firing stack.
46- *
47- * @var array
48- */
49- protected $ firing = [];
50-
5137 /**
5238 * The queue resolver instance.
5339 *
@@ -71,18 +57,15 @@ public function __construct(ContainerContract $container = null)
7157 *
7258 * @param string|array $events
7359 * @param mixed $listener
74- * @param int $priority
7560 * @return void
7661 */
77- public function listen ($ events , $ listener, $ priority = 0 )
62+ public function listen ($ events , $ listener )
7863 {
7964 foreach ((array ) $ events as $ event ) {
8065 if (Str::contains ($ event , '* ' )) {
8166 $ this ->setupWildcardListen ($ event , $ listener );
8267 } else {
83- $ this ->listeners [$ event ][$ priority ][] = $ this ->makeListener ($ listener );
84-
85- unset($ this ->sorted [$ event ]);
68+ $ this ->listeners [$ event ][] = $ this ->makeListener ($ event , $ listener );
8669 }
8770 }
8871 }
@@ -96,7 +79,7 @@ public function listen($events, $listener, $priority = 0)
9679 */
9780 protected function setupWildcardListen ($ event , $ listener )
9881 {
99- $ this ->wildcards [$ event ][] = $ this ->makeListener ($ listener );
82+ $ this ->wildcards [$ event ][] = $ this ->makeListener ($ event , $ listener, true );
10083 }
10184
10285 /**
@@ -152,18 +135,6 @@ protected function resolveSubscriber($subscriber)
152135 return $ subscriber ;
153136 }
154137
155- /**
156- * Fire an event until the first non-null response is returned.
157- *
158- * @param string|object $event
159- * @param array $payload
160- * @return mixed
161- */
162- public function until ($ event , $ payload = [])
163- {
164- return $ this ->fire ($ event , $ payload , true );
165- }
166-
167138 /**
168139 * Flush a set of pushed events.
169140 *
@@ -175,48 +146,26 @@ public function flush($event)
175146 $ this ->fire ($ event .'_pushed ' );
176147 }
177148
178- /**
179- * Get the event that is currently firing.
180- *
181- * @return string
182- */
183- public function dispatching ()
184- {
185- return $ this ->firing ();
186- }
187-
188- /**
189- * Get the event that is currently firing.
190- *
191- * @return string
192- */
193- public function firing ()
194- {
195- return last ($ this ->firing );
196- }
197-
198149 /**
199150 * Fire an event and call the listeners.
200151 *
201152 * @param string|object $event
202153 * @param mixed $payload
203- * @param bool $halt
204154 * @return array|null
205155 */
206- public function dispatch ($ event , $ payload = [], $ halt = false )
156+ public function dispatch ($ event , $ payload = [])
207157 {
208- return $ this ->fire ($ event , $ payload, $ halt );
158+ return $ this ->fire ($ event , $ payload );
209159 }
210160
211161 /**
212162 * Fire an event and call the listeners.
213163 *
214164 * @param string|object $event
215165 * @param mixed $payload
216- * @param bool $halt
217166 * @return array|null
218167 */
219- public function fire ($ event , $ payload = [], $ halt = false )
168+ public function fire ($ event , $ payload = [])
220169 {
221170 // When the given "event" is actually an object we will assume it is an event
222171 // object and use the class as the event name and this event itself as the
@@ -225,25 +174,14 @@ public function fire($event, $payload = [], $halt = false)
225174 $ event , $ payload
226175 );
227176
228- $ responses = [];
229-
230- $ this ->firing [] = $ event ;
231-
232177 if ($ this ->shouldBroadcast ($ payload )) {
233178 $ this ->broadcastEvent ($ payload [0 ]);
234179 }
235180
236- foreach ($ this ->getListeners ($ event ) as $ listener ) {
237- $ response = call_user_func_array ($ listener , $ payload );
238-
239- // If a response is returned from the listener and event halting is enabled
240- // we will just return this response, and not call the rest of the event
241- // listeners. Otherwise we will add the response on the response list.
242- if (! is_null ($ response ) && $ halt ) {
243- array_pop ($ this ->firing );
181+ $ responses = [];
244182
245- return $ response ;
246- }
183+ foreach ( $ this -> getListeners ( $ event ) as $ listener ) {
184+ $ response = $ listener ( $ event , $ payload );
247185
248186 // If a boolean false is returned from a listener, we will stop propagating
249187 // the event to any further listeners down in the chain, else we keep on
@@ -255,9 +193,7 @@ public function fire($event, $payload = [], $halt = false)
255193 $ responses [] = $ response ;
256194 }
257195
258- array_pop ($ this ->firing );
259-
260- return $ halt ? null : $ responses ;
196+ return $ responses ;
261197 }
262198
263199 /**
@@ -306,13 +242,15 @@ protected function broadcastEvent($event)
306242 */
307243 public function getListeners ($ eventName )
308244 {
309- $ wildcards = $ this ->getWildcardListeners ( $ eventName) ;
245+ $ listeners = isset ( $ this ->listeners [ $ eventName]) ? $ this -> listeners [ $ eventName ] : [] ;
310246
311- if (! isset ( $ this -> sorted [ $ eventName ])) {
312- $ this ->sortListeners ($ eventName );
313- }
247+ $ listeners = array_merge (
248+ $ listeners , $ this ->getWildcardListeners ($ eventName )
249+ );
314250
315- return array_merge ($ this ->sorted [$ eventName ], $ wildcards );
251+ return class_exists ($ eventName , false )
252+ ? $ this ->addInterfaceListeners ($ eventName , $ listeners )
253+ : $ listeners ;
316254 }
317255
318256 /**
@@ -334,33 +272,6 @@ protected function getWildcardListeners($eventName)
334272 return $ wildcards ;
335273 }
336274
337- /**
338- * Sort the listeners for a given event by priority.
339- *
340- * @param string $eventName
341- * @return void
342- */
343- protected function sortListeners ($ eventName )
344- {
345- // If listeners exist for the given event, we will sort them by the priority
346- // so that we can call them in the correct order. We will cache off these
347- // sorted event listeners so we do not have to re-sort on every events.
348- $ listeners = isset ($ this ->listeners [$ eventName ])
349- ? $ this ->listeners [$ eventName ] : [];
350-
351- if (class_exists ($ eventName , false )) {
352- $ listeners = $ this ->addInterfaceListeners ($ eventName , $ listeners );
353- }
354-
355- if ($ listeners ) {
356- krsort ($ listeners );
357-
358- $ this ->sorted [$ eventName ] = call_user_func_array ('array_merge ' , $ listeners );
359- } else {
360- $ this ->sorted [$ eventName ] = [];
361- }
362- }
363-
364275 /**
365276 * Add the listeners for the event's interfaces to the given array.
366277 *
@@ -372,12 +283,8 @@ protected function addInterfaceListeners($eventName, array $listeners = [])
372283 {
373284 foreach (class_implements ($ eventName ) as $ interface ) {
374285 if (isset ($ this ->listeners [$ interface ])) {
375- foreach ($ this ->listeners [$ interface ] as $ priority => $ names ) {
376- if (isset ($ listeners [$ priority ])) {
377- $ listeners [$ priority ] = array_merge ($ listeners [$ priority ], $ names );
378- } else {
379- $ listeners [$ priority ] = $ names ;
380- }
286+ foreach ($ this ->listeners [$ interface ] as $ names ) {
287+ $ listeners = array_merge ($ listeners , (array ) $ names );
381288 }
382289 }
383290 }
@@ -391,9 +298,19 @@ protected function addInterfaceListeners($eventName, array $listeners = [])
391298 * @param string|\Closure $listener
392299 * @return mixed
393300 */
394- public function makeListener ($ listener )
301+ public function makeListener ($ event , $ listener, $ wildcard = false )
395302 {
396- return is_string ($ listener ) ? $ this ->createClassListener ($ listener ) : $ listener ;
303+ if (is_string ($ listener )) {
304+ return $ this ->createClassListener ($ listener , $ wildcard );
305+ }
306+
307+ return function ($ event , $ payload ) use ($ listener , $ wildcard ) {
308+ if ($ wildcard ) {
309+ return $ listener ($ event , $ payload );
310+ } else {
311+ return $ listener (...array_values ($ payload ));
312+ }
313+ };
397314 }
398315
399316 /**
@@ -402,12 +319,16 @@ public function makeListener($listener)
402319 * @param string $listener
403320 * @return \Closure
404321 */
405- public function createClassListener ($ listener )
322+ public function createClassListener ($ listener, $ wildcard = false )
406323 {
407- return function () use ($ listener ) {
408- return call_user_func_array (
409- $ this ->createClassCallable ($ listener ), func_get_args ()
410- );
324+ return function ($ event , $ payload ) use ($ listener , $ wildcard ) {
325+ if ($ wildcard ) {
326+ return call_user_func ($ this ->createClassCallable ($ listener ), $ event , $ payload );
327+ } else {
328+ return call_user_func_array (
329+ $ this ->createClassCallable ($ listener ), $ payload
330+ );
331+ }
411332 };
412333 }
413334
@@ -533,7 +454,7 @@ public function forget($event)
533454 if (Str::contains ($ event , '* ' )) {
534455 unset($ this ->wildcards [$ event ]);
535456 } else {
536- unset($ this ->listeners [$ event ], $ this -> sorted [ $ event ] );
457+ unset($ this ->listeners [$ event ]);
537458 }
538459 }
539460
0 commit comments