Add Symfony inspired typed event dispatcher method

Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
This commit is contained in:
Christoph Wurst 2019-09-11 20:02:50 +02:00
parent 15d39c48e6
commit b9e14d5972
No known key found for this signature in database
GPG Key ID: CC42AC2A7F0E56D8
2 changed files with 17 additions and 0 deletions

View File

@ -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
*/

View File

@ -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;
}