From b9e14d5972d403a669a23e11b4b1359995d65ccc Mon Sep 17 00:00:00 2001 From: Christoph Wurst Date: Wed, 11 Sep 2019 20:02:50 +0200 Subject: Add Symfony inspired typed event dispatcher method Signed-off-by: Christoph Wurst --- lib/private/EventDispatcher/EventDispatcher.php | 5 +++++ lib/public/EventDispatcher/IEventDispatcher.php | 12 ++++++++++++ 2 files changed, 17 insertions(+) (limited to 'lib') diff --git a/lib/private/EventDispatcher/EventDispatcher.php b/lib/private/EventDispatcher/EventDispatcher.php index 8db2f3101be..7b3a1c378a1 100644 --- a/lib/private/EventDispatcher/EventDispatcher.php +++ b/lib/private/EventDispatcher/EventDispatcher.php @@ -31,6 +31,7 @@ use OCP\IContainer; use OCP\ILogger; use OCP\IServerContainer; use Symfony\Component\EventDispatcher\EventDispatcher as SymfonyDispatcher; +use function get_class; class EventDispatcher implements IEventDispatcher { @@ -75,6 +76,10 @@ class EventDispatcher implements IEventDispatcher { $this->dispatcher->dispatch($eventName, $event); } + public function dispatchTyped(Event $event): void { + $this->dispatch(get_class($event), $event); + } + /** * @return SymfonyDispatcher */ diff --git a/lib/public/EventDispatcher/IEventDispatcher.php b/lib/public/EventDispatcher/IEventDispatcher.php index af4d5316a7b..630b7e3c8a0 100644 --- a/lib/public/EventDispatcher/IEventDispatcher.php +++ b/lib/public/EventDispatcher/IEventDispatcher.php @@ -58,4 +58,16 @@ interface IEventDispatcher { */ public function dispatch(string $eventName, Event $event): void; + /** + * Dispatch a typed event + * + * Only use this with subclasses of ``\OCP\EventDispatcher\Event``. + * The object's class will determine the event name. + * + * @param Event $event + * + * @since 18.0.0 + */ + public function dispatchTyped(Event $event): void; + } -- cgit v1.2.3