Skip to content

Commit

Permalink
🚑 Merge pull request #11 from lorisleiva/lab/decorate_event_dispatcher
Browse files Browse the repository at this point in the history
Decorate event dispatcher
  • Loading branch information
lorisleiva authored Jun 10, 2019
2 parents 7f12cd6 + b2b3600 commit e9835f6
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 23 deletions.
4 changes: 1 addition & 3 deletions src/ActionServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ class ActionServiceProvider extends ServiceProvider
public function boot()
{
$this->app->extend('events', function ($dispatcher, $app) {
return (new EventDispatcher($app))->setQueueResolver(function () use ($app) {
return $app->make(QueueFactoryContract::class);
});
return new EventDispatcherDecorator($dispatcher, $app);
});

$this->app->extend(IlluminateBusDispatcher::class, function ($dispatcher, $app) {
Expand Down
20 changes: 0 additions & 20 deletions src/EventDispatcher.php

This file was deleted.

88 changes: 88 additions & 0 deletions src/EventDispatcherDecorator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

namespace LorisLeiva\Actions;

use Illuminate\Support\Str;
use Illuminate\Contracts\Events\Dispatcher as DispatcherContract;
use Illuminate\Contracts\Container\Container as ContainerContract;

class EventDispatcherDecorator implements DispatcherContract
{
protected $dispatcher;
protected $container;

public function __construct(DispatcherContract $dispatcher, ContainerContract $container)
{
$this->dispatcher = $dispatcher;
$this->container = $container;
}

public function listen($events, $listener)
{
if ($this->isActionFullyQualifiedName($listener)) {
$listener = $listener . '@runAsListener';
}

return $this->dispatcher->listen($events, $listener);
}

public function isActionFullyQualifiedName($listener)
{
if (! is_string($listener)) {
return false;
}

[$class, $method] = Str::parseCallback($listener);

if (! is_null($method)) {
return false;
}

return $this->container->make($class) instanceof Action;
}

public function hasListeners($eventName)
{
return $this->dispatcher->hasListeners($eventName);
}

public function subscribe($subscriber)
{
return $this->dispatcher->subscribe($subscriber);
}

public function until($event, $payload = [])
{
return $this->dispatcher->until($event, $payload);
}

public function dispatch($event, $payload = [], $halt = false)
{
return $this->dispatcher->dispatch($event, $payload, $halt);
}

public function push($event, $payload = [])
{
return $this->dispatcher->push($event, $payload);
}

public function flush($event)
{
return $this->dispatcher->flush($event);
}

public function forget($event)
{
return $this->dispatcher->forget($event);
}

public function forgetPushed()
{
return $this->dispatcher->forgetPushed();
}

public function __call($method, $parameters)
{
return $this->dispatcher->{$method}(...$parameters);
}
}

0 comments on commit e9835f6

Please sign in to comment.