| 1 |
<?php |
| 2 |
/** |
| 3 |
* Event Dispatcher Interface |
| 4 |
* |
| 5 |
* @package Forge12\DoubleOptIn\EventSystem |
| 6 |
* @since 4.0.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace Forge12\DoubleOptIn\EventSystem; |
| 10 |
|
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit; |
| 13 |
} |
| 14 |
|
| 15 |
/** |
| 16 |
* Interface EventDispatcherInterface |
| 17 |
* |
| 18 |
* @api |
| 19 |
* |
| 20 |
* PSR-14 inspired event dispatcher with WordPress hook bridging support. |
| 21 |
* |
| 22 |
* Covered by the Addon API semver policy as of Core API 4.3.0. Use |
| 23 |
* `$container->get(EventDispatcherInterface::class)` inside an addon's |
| 24 |
* boot() to subscribe to lifecycle events. See docs/addon-api.md §6 for |
| 25 |
* the event catalogue. |
| 26 |
*/ |
| 27 |
interface EventDispatcherInterface { |
| 28 |
|
| 29 |
/** |
| 30 |
* Dispatch an event to all registered listeners. |
| 31 |
* |
| 32 |
* @param object $event The event to dispatch. |
| 33 |
* |
| 34 |
* @return object The same event object, potentially modified by listeners. |
| 35 |
*/ |
| 36 |
public function dispatch( object $event ): object; |
| 37 |
|
| 38 |
/** |
| 39 |
* Add a listener for a specific event. |
| 40 |
* |
| 41 |
* @param string $eventName The fully qualified class name of the event. |
| 42 |
* @param callable $listener The listener callable. |
| 43 |
* @param int $priority Higher priority = earlier execution (like WordPress hooks). |
| 44 |
* |
| 45 |
* @return void |
| 46 |
*/ |
| 47 |
public function addListener( string $eventName, callable $listener, int $priority = 10 ): void; |
| 48 |
|
| 49 |
/** |
| 50 |
* Remove a listener. |
| 51 |
* |
| 52 |
* @param string $eventName The event class name. |
| 53 |
* @param callable $listener The listener to remove. |
| 54 |
* |
| 55 |
* @return void |
| 56 |
*/ |
| 57 |
public function removeListener( string $eventName, callable $listener ): void; |
| 58 |
|
| 59 |
/** |
| 60 |
* Check if an event has any listeners. |
| 61 |
* |
| 62 |
* @param string $eventName The event class name. |
| 63 |
* |
| 64 |
* @return bool |
| 65 |
*/ |
| 66 |
public function hasListeners( string $eventName ): bool; |
| 67 |
|
| 68 |
/** |
| 69 |
* Get all listeners for an event, sorted by priority. |
| 70 |
* |
| 71 |
* @param string $eventName The event class name. |
| 72 |
* |
| 73 |
* @return callable[] |
| 74 |
*/ |
| 75 |
public function getListeners( string $eventName ): array; |
| 76 |
} |
| 77 |
|