From f83edc1cb820523fd933c3d3c0430a1f63a073ec Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Tue, 10 Jan 2017 13:58:58 -0600 Subject: [PATCH] was overzealous removing halt :) --- .../Contracts/Events/Dispatcher.php | 40 ++++++++----- src/Illuminate/Events/Dispatcher.php | 59 +++++++++++++------ 2 files changed, 65 insertions(+), 34 deletions(-) diff --git a/src/Illuminate/Contracts/Events/Dispatcher.php b/src/Illuminate/Contracts/Events/Dispatcher.php index ac61f0188a5f..5850beb32392 100644 --- a/src/Illuminate/Contracts/Events/Dispatcher.php +++ b/src/Illuminate/Contracts/Events/Dispatcher.php @@ -21,15 +21,6 @@ public function listen($events, $listener); */ public function hasListeners($eventName); - /** - * Register an event and payload to be fired later. - * - * @param string $event - * @param array $payload - * @return void - */ - public function push($event, $payload = []); - /** * Register an event subscriber with the dispatcher. * @@ -39,21 +30,40 @@ public function push($event, $payload = []); public function subscribe($subscriber); /** - * Flush a set of pushed events. + * Dispatch an event and call the listeners. * - * @param string $event - * @return void + * @param string|object $event + * @param mixed $payload + * @return array|null */ - public function flush($event); + public function until($event, $payload = []); /** - * Dispatch an event and call the listeners. + * Fire an event until the first non-null response is returned. * * @param string|object $event * @param mixed $payload + * @param bool $halt * @return array|null */ - public function dispatch($event, $payload = []); + public function dispatch($event, $payload = [], $halt = false); + + /** + * Register an event and payload to be fired later. + * + * @param string $event + * @param array $payload + * @return void + */ + public function push($event, $payload = []); + + /** + * Flush a set of pushed events. + * + * @param string $event + * @return void + */ + public function flush($event); /** * Remove a set of listeners from the dispatcher. diff --git a/src/Illuminate/Events/Dispatcher.php b/src/Illuminate/Events/Dispatcher.php index 5c734b1a92c5..02acced78eeb 100755 --- a/src/Illuminate/Events/Dispatcher.php +++ b/src/Illuminate/Events/Dispatcher.php @@ -107,6 +107,17 @@ public function push($event, $payload = []) }); } + /** + * Flush a set of pushed events. + * + * @param string $event + * @return void + */ + public function flush($event) + { + $this->dispatch($event.'_pushed'); + } + /** * Register an event subscriber with the dispatcher. * @@ -136,14 +147,15 @@ protected function resolveSubscriber($subscriber) } /** - * Flush a set of pushed events. + * Fire an event until the first non-null response is returned. * - * @param string $event - * @return void + * @param string|object $event + * @param mixed $payload + * @return array|null */ - public function flush($event) + public function until($event, $payload = []) { - $this->dispatch($event.'_pushed'); + return $this->dispatch($event, $payload, true); } /** @@ -151,9 +163,23 @@ public function flush($event) * * @param string|object $event * @param mixed $payload + * @param bool $halt * @return array|null */ - public function dispatch($event, $payload = []) + public function fire($event, $payload = [], $halt = false) + { + return $this->dispatch($event, $payload, $halt); + } + + /** + * Fire an event and call the listeners. + * + * @param string|object $event + * @param mixed $payload + * @param bool $halt + * @return array|null + */ + public function dispatch($event, $payload = [], $halt = false) { // When the given "event" is actually an object we will assume it is an event // object and use the class as the event name and this event itself as the @@ -171,6 +197,13 @@ public function dispatch($event, $payload = []) foreach ($this->getListeners($event) as $listener) { $response = $listener($event, $payload); + // If a response is returned from the listener and event halting is enabled + // we will just return this response, and not call the rest of the event + // listeners. Otherwise we will add the response on the response list. + if (! is_null($response) && $halt) { + return $response; + } + // If a boolean false is returned from a listener, we will stop propagating // the event to any further listeners down in the chain, else we keep on // looping through the listeners and firing every one in our sequence. @@ -181,19 +214,7 @@ public function dispatch($event, $payload = []) $responses[] = $response; } - return $responses; - } - - /** - * Fire an event and call the listeners. - * - * @param string|object $event - * @param mixed $payload - * @return array|null - */ - public function fire($event, $payload = []) - { - return $this->dispatch($event, $payload); + return $halt ? null : $responses; } /**