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: 123
Properties
instance
public EventDispatcher $instance
listeners
private array $listeners
dispatchQueue
private array $dispatchQueue
Methods
__construct
Private constructor to prevent direct instantiation.
private __construct(): void
Use getInstance()
to get the singleton instance.
__clone
Private clone method to prevent cloning of the singleton instance.
private __clone(): void
getInstance
Gets the singleton instance of the EventDispatcher.
public getInstance(): EventDispatcher
Ensures 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): void
Assumes 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): bool
The 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): void
This 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): void
Parameters
Parameter | Type | Description |
---|---|---|
$event | string | the fully qualified class name of the event |
clearListeners
Removes all registered listeners from the dispatcher.
public clearListeners(): void
dispatch
Dispatches an event object to all registered listeners.
public dispatch(object $event): void
Finds 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): void
Iterates 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 Tue, 06 May 2025 01:33:01 +0000