EventDispatcher
EventDispatcher
EventDispatcher Class.
Manages the registration and dispatching of events throughout the application. This class follows the Singleton pattern to ensure a single instance manages all events. Listeners are registered based on the type hint of the first parameter of their handle method.
Example Usage:
// Define an event
class UserLoggedInEvent {
use Hazaar\Events\Dispatchable;
public $userId;
public function __construct(int $userId) { $this->userId = $userId; }
}
// Define a listener
class UserLoginListener {
public function handle(UserLoggedInEvent $event) {
echo "User logged in: " . $event->userId;
}
}
// Get the singleton instance
$dispatcher = EventDispatcher::getInstance();
// Add the listener
$dispatcher->addListener(new UserLoginListener());
// Dispatch the event using the Dispatchable trait
UserLoggedInEvent::dispatch(123); // Output: User logged in: 123Properties
instance
public EventDispatcher $instancelisteners
private array $listenersdispatchQueue
private array $dispatchQueueMethods
__construct
Private constructor to prevent direct instantiation.
private __construct(): voidUse getInstance() to get the singleton instance.
__clone
Private clone method to prevent cloning of the singleton instance.
private __clone(): voidgetInstance
Gets the singleton instance of the EventDispatcher.
public getInstance(): EventDispatcherEnsures that only one instance of the EventDispatcher exists. If an instance does not exist, it creates one.
withEvents
Scans a directory for listener classes, instantiates them, and adds them.
public withEvents(string $listenerDir): voidAssumes listener classes are in the 'App\Listener' namespace and filenames match class names.
Parameters
| Parameter | Type | Description |
|---|---|---|
$listenerDir | string | the directory path containing listener class files |
addListener
Adds an event listener object to the dispatcher.
public addListener(object $listener): boolThe listener is registered based on the type hint of the first parameter of its handle method. The listener object must have a public handle method.
Parameters
| Parameter | Type | Description |
|---|---|---|
$listener | object | the listener object to add |
listen
Registers a listener class for a specific event class name.
public listen(string $event, string $listener): voidThis method is less type-safe than addListener as it relies on string class names. It's generally recommended to use addListener with instantiated objects.
Parameters
| Parameter | Type | Description |
|---|---|---|
$event | string | the fully qualified class name of the event |
$listener | string | the fully qualified class name of the listener |
getListeners
Retrieves all registered listener objects for a given event class name.
public getListeners(string $event): voidParameters
| Parameter | Type | Description |
|---|---|---|
$event | string | the fully qualified class name of the event |
clearListeners
Removes all registered listeners from the dispatcher.
public clearListeners(): voiddispatch
Dispatches an event object to all registered listeners.
public dispatch(object $event): voidFinds listeners registered for the specific class of the event object. If a listener implements the Queuable interface, the event is added to a queue for later processing via dispatchQueue(). Otherwise, the listener's handle method is called immediately with the event object.
Parameters
| Parameter | Type | Description |
|---|---|---|
$event | object | the event object to dispatch |
dispatchQueue
Dispatches all events currently held in the queue.
public dispatchQueue(?string $eventName): voidIterates through the queued events and calls the handle method of the corresponding listeners. The queue is cleared after processing.
Parameters
| Parameter | Type | Description |
|---|---|---|
$eventName | string | the fully qualified class name of the event to dispatch from the queue, or null to dispatch all queued events |
Generated by Hazaar API Doc Generator on Wed, 07 Jan 2026 11:29:59 +0000