This commit is contained in:
Martin Slachta
2026-06-11 19:03:29 +02:00
commit 0d829845c4
150 changed files with 38582 additions and 0 deletions
+39
View File
@@ -0,0 +1,39 @@
<?php
interface RsvEventBusInterface {
public function dispatch(object $event): void;
public function listen(string $event_class, callable $listener): void;
}
class RsvWordPressEventBus implements RsvEventBusInterface {
public function dispatch(object $event): void {
do_action(get_class($event), $event);
}
public function listen(string $event_class, callable $listener): void {
add_action($event_class, $listener);
}
}
class RsvEventDispatcher {
private static ?RsvEventBusInterface $bus = null;
public static function init(RsvEventBusInterface $bus): void {
self::$bus = $bus;
}
private static function bus(): RsvEventBusInterface {
// Fall back to the WordPress bus if init() was never called, so a stray
// dispatch/listen during early bootstrap can't fatal on an uninitialised
// typed property.
return self::$bus ??= new RsvWordPressEventBus();
}
public static function dispatch(object $event): void {
self::bus()->dispatch($event);
}
public static function listen(string $event_class, callable $listener): void {
self::bus()->listen($event_class, $listener);
}
}