diff --git a/src/Potter/Event/DispatcherAwareInterface.php b/src/Potter/Event/DispatcherAwareInterface.php index bb3fbfc..a349e01 100644 --- a/src/Potter/Event/DispatcherAwareInterface.php +++ b/src/Potter/Event/DispatcherAwareInterface.php @@ -10,6 +10,7 @@ use \Potter\Aware\AwareInterface; interface DispatcherAwareInterface extends BaseDispatcherAwareInterface, AwareInterface { - public function useEventDispatcher(BaseDispatcher $dispatcher): void; public function eventDispatcher(): BaseDispatcher; + public function hasEventDispatcher(): bool; + public function useEventDispatcher(BaseDispatcher $dispatcher): void; } diff --git a/src/Potter/Event/DispatcherAwareTrait.php b/src/Potter/Event/DispatcherAwareTrait.php index 0cbe8bd..46b9838 100644 --- a/src/Potter/Event/DispatcherAwareTrait.php +++ b/src/Potter/Event/DispatcherAwareTrait.php @@ -4,5 +4,34 @@ declare(strict_types=1); namespace Potter\Event; +use \League\Event\EventDispatcher as BaseDispatcher; + trait DispatcherAwareTrait -{ } +{ + private const string DISPATCHER = 'dispatcher'; + + final public function useEventDispatcher(BaseDispatcher $dispatcher): void + { + $this->set(self::DISPATCHER, $dispatcher); + } + + final public function eventDispatcher(): BaseDispatcher + { + if (!$this->hasEventDispatcher()) { + $this->useEventDispatcher(new Dispatcher); + } + return $this->get(self::DISPATCHER); + } + + final public function hasEventDispatcher(): bool + { + return $this->has(self::DISPATCHER); + } + + abstract public function get(string $key): mixed; + abstract public function has(string $key): mixed; + abstract protected function set(string $key, mixed $value): mixed; + abstract protected function unset(string $key): null; + abstract public function with(string $key, mixed $value): static; + abstract public function without(string $key): static; +}