DateManager.php
3 weeks ago
EventManager.php
3 weeks ago
LogManager.php
3 weeks ago
OptionManager.php
3 weeks ago
PolicyManager.php
3 weeks ago
EventManager.php
133 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Dispatches domain events to registered listener classes loaded from cache. |
| 5 | * Resolves listeners from the cached map, sorts by priority, and invokes handle on each match. |
| 6 | * Core of the framework event-driven architecture. |
| 7 | * |
| 8 | * @package Framework |
| 9 | * @subpackage Managers |
| 10 | * @since 1.0.0 |
| 11 | */ |
| 12 | namespace Kirki\Framework\Managers; |
| 13 | |
| 14 | \defined('ABSPATH') || exit; |
| 15 | use Closure; |
| 16 | use Kirki\Framework\Listener; |
| 17 | use InvalidArgumentException; |
| 18 | use function Kirki\Framework\config_path; |
| 19 | class EventManager |
| 20 | { |
| 21 | /** |
| 22 | * The listeners array. |
| 23 | * |
| 24 | * @var array |
| 25 | * |
| 26 | * @since 1.0.0 |
| 27 | */ |
| 28 | protected array $listeners = []; |
| 29 | /** |
| 30 | * Create a new event manager instance. |
| 31 | * |
| 32 | * @return void |
| 33 | * |
| 34 | * @since 1.0.0 |
| 35 | */ |
| 36 | public function __construct() |
| 37 | { |
| 38 | $this->load_listeners(); |
| 39 | } |
| 40 | /** |
| 41 | * Load the listeners from the cache. |
| 42 | * |
| 43 | * @return $this |
| 44 | * |
| 45 | * @since 1.0.0 |
| 46 | */ |
| 47 | protected function load_listeners() |
| 48 | { |
| 49 | $listeners_cache_path = config_path('listeners.cache.php'); |
| 50 | if (!\file_exists($listeners_cache_path)) { |
| 51 | return $this; |
| 52 | } |
| 53 | $this->listeners = (require $listeners_cache_path); |
| 54 | return $this; |
| 55 | } |
| 56 | /** |
| 57 | * Dispatch the event. |
| 58 | * |
| 59 | * @param mixed $event The event. |
| 60 | * |
| 61 | * @return void |
| 62 | * |
| 63 | * @throws \InvalidArgumentException |
| 64 | * |
| 65 | * @since 1.0.0 |
| 66 | */ |
| 67 | public function dispatch($event) |
| 68 | { |
| 69 | if (!\is_object($event)) { |
| 70 | throw new InvalidArgumentException(\sprintf('The event must be an object, got [%s]', \gettype($event))); |
| 71 | } |
| 72 | $event_class = \get_class($event); |
| 73 | if (!isset($this->listeners[$event_class])) { |
| 74 | return; |
| 75 | } |
| 76 | $event_listeners = $this->listeners[$event_class] ?? []; |
| 77 | foreach ($event_listeners as $listener) { |
| 78 | $this->resolve($listener, $event); |
| 79 | } |
| 80 | } |
| 81 | /** |
| 82 | * Dispatch the event if the boolean is true. |
| 83 | * |
| 84 | * @param Closure $boolean The boolean. |
| 85 | * @param mixed $event The event. |
| 86 | * |
| 87 | * @return void |
| 88 | * |
| 89 | * @since 1.0.0 |
| 90 | */ |
| 91 | public function dispatch_if(Closure $boolean, $event) |
| 92 | { |
| 93 | if ($boolean($event)) { |
| 94 | $this->dispatch($event); |
| 95 | } |
| 96 | } |
| 97 | /** |
| 98 | * Dispatch the event unless the boolean is true. |
| 99 | * |
| 100 | * @param Closure $boolean The boolean. |
| 101 | * @param mixed $event The event. |
| 102 | * |
| 103 | * @return void |
| 104 | * |
| 105 | * @since 1.0.0 |
| 106 | */ |
| 107 | public function dispatch_unless(Closure $boolean, $event) |
| 108 | { |
| 109 | if (!$boolean($event)) { |
| 110 | $this->dispatch($event); |
| 111 | } |
| 112 | } |
| 113 | /** |
| 114 | * Resolve. |
| 115 | * |
| 116 | * @param mixed $listener The listener. |
| 117 | * @param mixed $event The event. |
| 118 | * |
| 119 | * @return void |
| 120 | * |
| 121 | * @throws \InvalidArgumentException |
| 122 | * |
| 123 | * @since 1.0.0 |
| 124 | */ |
| 125 | protected function resolve($listener, $event) |
| 126 | { |
| 127 | if (!\is_subclass_of($listener, Listener::class)) { |
| 128 | throw new InvalidArgumentException(\sprintf('The listener [%s] must be a subclass of [%s]', Listener::class)); |
| 129 | } |
| 130 | return (new $listener())->handle($event); |
| 131 | } |
| 132 | } |
| 133 |