-
-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🚑 Merge pull request #11 from lorisleiva/lab/decorate_event_dispatcher
Decorate event dispatcher
- Loading branch information
Showing
3 changed files
with
89 additions
and
23 deletions.
There are no files selected for viewing
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
This file was deleted.
Oops, something went wrong.
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
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); | ||
} | ||
} |