Skip to content

Commit

Permalink
was overzealous removing halt :)
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Jan 10, 2017
1 parent ee71fb2 commit f83edc1
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 34 deletions.
40 changes: 25 additions & 15 deletions src/Illuminate/Contracts/Events/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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.
Expand Down
59 changes: 40 additions & 19 deletions src/Illuminate/Events/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -136,24 +147,39 @@ 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);
}

/**
* 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 = [])
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
Expand All @@ -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.
Expand All @@ -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;
}

/**
Expand Down

0 comments on commit f83edc1

Please sign in to comment.