Skip to content

Commit b9e14d5

Browse files
Add Symfony inspired typed event dispatcher method
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
1 parent 15d39c4 commit b9e14d5

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

lib/private/EventDispatcher/EventDispatcher.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
use OCP\ILogger;
3232
use OCP\IServerContainer;
3333
use Symfony\Component\EventDispatcher\EventDispatcher as SymfonyDispatcher;
34+
use function get_class;
3435

3536
class EventDispatcher implements IEventDispatcher {
3637

@@ -75,6 +76,10 @@ public function dispatch(string $eventName,
7576
$this->dispatcher->dispatch($eventName, $event);
7677
}
7778

79+
public function dispatchTyped(Event $event): void {
80+
$this->dispatch(get_class($event), $event);
81+
}
82+
7883
/**
7984
* @return SymfonyDispatcher
8085
*/

lib/public/EventDispatcher/IEventDispatcher.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,16 @@ public function addServiceListener(string $eventName, string $className, int $pr
5858
*/
5959
public function dispatch(string $eventName, Event $event): void;
6060

61+
/**
62+
* Dispatch a typed event
63+
*
64+
* Only use this with subclasses of ``\OCP\EventDispatcher\Event``.
65+
* The object's class will determine the event name.
66+
*
67+
* @param Event $event
68+
*
69+
* @since 18.0.0
70+
*/
71+
public function dispatchTyped(Event $event): void;
72+
6173
}

0 commit comments

Comments
 (0)