initial
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user