-
-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Extend Laravel Events Dispatcher #6
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Check what it actually looks like once merged: <?php namespace Winter\Storm\Events;
use Exception;
use ReflectionClass;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\Container\Container;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
use Illuminate\Contracts\Events\Dispatcher as DispatcherContract;
use Illuminate\Contracts\Broadcasting\Factory as BroadcastFactory;
use Illuminate\Contracts\Container\Container as ContainerContract;
class Dispatcher extends \Illuminate\Events\Dispatcher
{
/**
* The sorted event listeners.
*
* @var array
*/
protected $sorted = [];
/**
* The event firing stack.
*
* @var array
*/
protected $firing = [];
/**
* Register an event listener with the dispatcher.
*
* @param string|array $events
* @param mixed $listener
* @param int $priority
* @return void
*/
public function listen($events, $listener, $priority = 0)
{
foreach ((array) $events as $event) {
if (Str::contains($event, '*')) {
$this->setupWildcardListen($event, $listener);
} else {
$this->listeners[$event][$priority][] = $this->makeListener($listener);
unset($this->sorted[$event]);
}
}
}
/**
* Get the event that is currently firing.
*
* @return string
*/
public function firing()
{
return last($this->firing);
}
/**
* Fire an event until the first non-null response is returned.
*
* @param string|object $event
* @param mixed $payload
* @return array|null
*/
public function until($event, $payload = [])
{
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 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
// payload to the handler, which makes object based events quite simple.
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) {
$this->broadcastEvent($payload[0]);
}
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) {
array_pop($this->firing);
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.
if ($response === false) {
break;
}
$responses[] = $response;
}
array_pop($this->firing);
return $halt ? null : $responses;
}
/**
* Get all of the listeners for a given event name.
*
* @param string $eventName
* @return array
*/
public function getListeners($eventName)
{
if (!isset($this->sorted[$eventName])) {
$this->sortListeners($eventName);
}
$listeners = $this->sorted[$eventName] ?? [];
$listeners = array_merge(
$listeners,
$this->wildcardsCache[$eventName] ?? $this->getWildcardListeners($eventName)
);
return class_exists($eventName, false)
? $this->addInterfaceListeners($eventName, $listeners)
: $listeners;
}
/**
* Sort the listeners for a given event by priority.
*
* @param string $eventName
* @return array
*/
protected function sortListeners($eventName)
{
$this->sorted[$eventName] = [];
// If listeners exist for the given event, we will sort them by the priority
// so that we can call them in the correct order. We will cache off these
// sorted event listeners so we do not have to re-sort on every events.
if (isset($this->listeners[$eventName])) {
krsort($this->listeners[$eventName]);
$this->sorted[$eventName] = call_user_func_array(
'array_merge',
$this->listeners[$eventName]
);
}
}
/**
* Create a callable for putting an event handler on the queue.
*
* @param string $class
* @param string $method
* @return \Closure
*/
protected function createQueuedHandlerCallable($class, $method)
{
return function () use ($class, $method) {
$arguments = $this->cloneArgumentsForQueueing(func_get_args());
if (method_exists($class, 'queue')) {
$this->callQueueMethodOnHandler($class, $method, $arguments);
} else {
$this->resolveQueue()->push('Winter\Storm\Events\CallQueuedHandler@call', [
'class' => $class, 'method' => $method, 'data' => serialize($arguments),
]);
}
};
}
/**
* Clone the given arguments for queueing.
*
* @param array $arguments
* @return array
*/
protected function cloneArgumentsForQueueing(array $arguments)
{
return array_map(function ($a) {
return is_object($a) ? clone $a : $a;
}, $arguments);
}
/**
* Call the queue method on the handler class.
*
* @param string $class
* @param string $method
* @param array $arguments
* @return void
*/
protected function callQueueMethodOnHandler($class, $method, $arguments)
{
$handler = (new ReflectionClass($class))->newInstanceWithoutConstructor();
$handler->queue($this->resolveQueue(), 'Winter\Storm\Events\CallQueuedHandler@call', [
'class' => $class, 'method' => $method, 'data' => serialize($arguments),
]);
}
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Reuses 90% of Laravel's Events Dispatcher.