Skip to content

Commit

Permalink
work on event refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Jan 10, 2017
1 parent 965afe0 commit 0f76617
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 11 deletions.
42 changes: 31 additions & 11 deletions src/Illuminate/Events/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,22 +221,15 @@ public function fire($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
// payload to the handler, which makes object based events quite simple.
if (is_object($event)) {
list($payload, $event) = [[$event], get_class($event)];
}
list($event, $payload) = $this->parseEventAndPayload(
$event, $payload
);

$responses = [];

// If an array is not given to us as the payload, we will turn it into one so
// we can easily use call_user_func_array on the listeners, passing in the
// payload to each of them so that they receive each of these arguments.
if (! is_array($payload)) {
$payload = [$payload];
}

$this->firing[] = $event;

if (isset($payload[0]) && $payload[0] instanceof ShouldBroadcast) {
if ($this->shouldBroadcast($payload)) {
$this->broadcastEvent($payload[0]);
}

Expand Down Expand Up @@ -267,6 +260,33 @@ public function fire($event, $payload = [], $halt = false)
return $halt ? null : $responses;
}

/**
* Parse the given event and payload and prepare them for dispatching.
*
* @param mixed $event
* @param mixed $payload
* @return array
*/
protected function parseEventAndPayload($event, $payload)
{
if (is_object($event)) {
list($payload, $event) = [[$event], get_class($event)];
}

return [$event, array_wrap($payload)];
}

/**
* Determine if the payload has a broadcastable event.
*
* @param array $payload
* @return bool
*/
protected function shouldBroadcast(array $payload)
{
return isset($payload[0]) && $payload[0] instanceof ShouldBroadcast;
}

/**
* Broadcast the given event class.
*
Expand Down
13 changes: 13 additions & 0 deletions src/Illuminate/Support/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,19 @@ function array_where($array, callable $callback)
}
}

if (! function_exists('array_wrap')) {
/**
* If the given value is not an array, wrap it in one.
*
* @param mixed $value
* @return array
*/
function array_wrap($value)
{
return ! is_array($value) ? [$value] : $value;
}
}

if (! function_exists('camel_case')) {
/**
* Convert a value to camel case.
Expand Down

0 comments on commit 0f76617

Please sign in to comment.