Merge pull request #722 from flightphp/eventdispatcher-rework

Simplify EventDispatcher methods and remove redundant descriptions
master
n0nag0n 1 day ago committed by GitHub
commit 5372672332
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -6,134 +6,83 @@ namespace flight\core;
class EventDispatcher class EventDispatcher
{ {
/** @var self|null Singleton instance of the EventDispatcher */
private static ?self $instance = null; private static ?self $instance = null;
/** @var array<string, array<int, callable>> */ /** @var array<string, callable[]> */
protected array $listeners = []; protected array $listeners = [];
/**
* Singleton instance of the EventDispatcher.
*
* @return self
*/
public static function getInstance(): self public static function getInstance(): self
{ {
if (self::$instance === null) { if (self::$instance === null) {
self::$instance = new self(); self::$instance = new self();
} }
return self::$instance; return self::$instance;
} }
/**
* Register a callback for an event.
*
* @param string $event Event name
* @param callable $callback Callback function
*/
public function on(string $event, callable $callback): void public function on(string $event, callable $callback): void
{ {
if (isset($this->listeners[$event]) === false) { $this->listeners[$event] ??= [];
$this->listeners[$event] = [];
}
$this->listeners[$event][] = $callback; $this->listeners[$event][] = $callback;
} }
/** /**
* Trigger an event with optional arguments. * @param mixed ...$args Arguments to pass to the listeners.
*
* @param string $event Event name
* @param mixed ...$args Arguments to pass to the callbacks
*
* @return mixed * @return mixed
*/ */
public function trigger(string $event, ...$args) public function trigger(string $event, ...$args)
{ {
$result = null; $listenerReturnValue = null;
if (isset($this->listeners[$event]) === true) {
foreach ($this->listeners[$event] as $callback) { foreach ($this->getListeners($event) as $listener) {
$result = call_user_func_array($callback, $args); $listenerReturnValue = $listener(...$args);
// If you return false, it will break the loop and stop the other event listeners. if ($listenerReturnValue === false) {
if ($result === false) { break;
break; // Stop executing further listeners
}
} }
} }
return $result;
return $listenerReturnValue;
} }
/**
* Check if an event has any registered listeners.
*
* @param string $event Event name
*
* @return bool True if the event has listeners, false otherwise
*/
public function hasListeners(string $event): bool public function hasListeners(string $event): bool
{ {
return isset($this->listeners[$event]) === true && count($this->listeners[$event]) > 0; return (
isset($this->listeners[$event])
&& is_array($this->listeners[$event])
&& count($this->listeners[$event])
);
} }
/** /** @return callable[] */
* Get all listeners registered for a specific event.
*
* @param string $event Event name
*
* @return array<int, callable> Array of callbacks registered for the event
*/
public function getListeners(string $event): array public function getListeners(string $event): array
{ {
return $this->listeners[$event] ?? []; return $this->listeners[$event] ?? [];
} }
/** /** @return string[] */
* Get a list of all events that have registered listeners.
*
* @return array<int, string> Array of event names
*/
public function getAllRegisteredEvents(): array public function getAllRegisteredEvents(): array
{ {
return array_keys($this->listeners); return array_keys($this->listeners);
} }
/**
* Remove a specific listener for an event.
*
* @param string $event the event name
* @param callable $callback the exact callback to remove
*
* @return void
*/
public function removeListener(string $event, callable $callback): void public function removeListener(string $event, callable $callback): void
{ {
if (isset($this->listeners[$event]) === true && count($this->listeners[$event]) > 0) { if (!$this->hasListeners($event)) {
$this->listeners[$event] = array_filter($this->listeners[$event], function ($listener) use ($callback) { return;
return $listener !== $callback;
});
$this->listeners[$event] = array_values($this->listeners[$event]); // Re-index the array
} }
$this->listeners[$event] = array_values(array_filter(
$this->getListeners($event),
static fn(callable $listener): bool => $listener !== $callback,
));
} }
/**
* Remove all listeners for a specific event.
*
* @param string $event the event name
*
* @return void
*/
public function removeAllListeners(string $event): void public function removeAllListeners(string $event): void
{ {
if (isset($this->listeners[$event]) === true) { unset($this->listeners[$event]);
unset($this->listeners[$event]);
}
} }
/**
* Remove the current singleton instance of the EventDispatcher.
*
* @return void
*/
public static function resetInstance(): void public static function resetInstance(): void
{ {
self::$instance = null; self::$instance = null;

Loading…
Cancel
Save