Skip to content

Internals Event EventDispatcher

Stefan Crkvenjakov edited this page Oct 7, 2026 · 1 revision

event\EventDispatcher

Framework Internals · Subsystem overview · Developer guide

Source: awt_src/classes/event/EventDispatcher.php · Snapshot 8411f4287706.

The request-wide event dispatcher calls listeners synchronously in registration order and collects their arrays. Listener exceptions propagate.

Imports and dependencies

use event\interfaces\IEvent;
use event\interfaces\IEventListener;

EventDispatcher

Declaration and architectural role

class EventDispatcher

Source description (behavioral corrections are recorded in Known discrepancies):

Class EventDispatcher

The EventDispatcher class manages the registration and dispatching of events within the application. It allows event listeners to be added for specific events and triggers those listeners when the associated event is dispatched.

Constants, enum cases, traits and properties

Declarations below preserve visibility, type, default and modifiers exactly. A typed property without an initializer remains uninitialized until its constructor or another method assigns it. Mutable fields belong to this instance unless marked static; readonly fields can be initialized once. Constructor-promoted properties are also listed below.

Member Declaration Purpose or source contract
$listeners public array $listeners = []; @var array $listeners An associative array where keys are event names and values are arrays of registered event listeners for those events.

Method index

Method Signature
addListener public function addListener(string $eventName, IEventListener $listener): void
dispatch public function dispatch(IEvent $event): array
removeListeners public function removeListeners(string $eventName): void

addListener

Source line 37

public function addListener(string $eventName, IEventListener $listener): void

Parameters

Parameter Type, modifiers and default Purpose
$eventName string $eventName The name of the event to listen for.
$listener IEventListener $listener An instance of a class that implements

Adds an event listener for a specified event name.

This method allows a listener to be registered for a specific event, enabling the listener to respond when that event is dispatched.

@param string $eventName The name of the event to listen for. @param IEventListener $listener An instance of a class that implements IEventListener to handle the event. @return void

State writes: $this->listeners[$eventName][].

Implementation, validation guards and interaction order
/**
     * Adds an event listener for a specified event name.
     *
     * This method allows a listener to be registered for a specific
     * event, enabling the listener to respond when that event is
     * dispatched.
     *
     * @param string $eventName The name of the event to listen for.
     * @param IEventListener $listener An instance of a class that implements
     * IEventListener to handle the event.
     * @return void
     */
    public function addListener(string $eventName, IEventListener $listener): void
    {
        $this->listeners[$eventName][] = $listener;
    }

dispatch

Source line 54

public function dispatch(IEvent $event): array

Parameters

Parameter Type, modifiers and default Purpose
$event IEvent $event An instance of a class that implements

Dispatches an event to its registered listeners.

This method triggers the handling of an event by invoking all listeners registered for that event name. The results of each listener's handling are collected and returned.

@param IEvent $event An instance of a class that implements IEvent, representing the event to be dispatched. @return array An array of results returned by each listener that handled the event.

Return expressions (including local callbacks): $result.

Implementation, validation guards and interaction order
/**
     * Dispatches an event to its registered listeners.
     *
     * This method triggers the handling of an event by invoking all
     * listeners registered for that event name. The results of each
     * listener's handling are collected and returned.
     *
     * @param IEvent $event An instance of a class that implements
     * IEvent, representing the event to be dispatched.
     * @return array An array of results returned by each listener that
     * handled the event.
     */
    public function dispatch(IEvent $event): array
    {
        $eventName = $event->getName();

        $result = [];

        if (!empty($this->listeners[$eventName])) {
            foreach ($this->listeners[$eventName] as $listener) {
                $result[] = $listener->handle($event);
            }
        }


        return $result;
    }

removeListeners

Source line 75

public function removeListeners(string $eventName): void

Parameters

Parameter Type, modifiers and default Purpose
$eventName string $eventName * @return void

Removes all listeners for a specific event. @param string $eventName @return void

Computes or delegates the operation shown below; no explicit receiver property assignment or throw occurs in this method.

Implementation, validation guards and interaction order
/**
     * Removes all listeners for a specific event.
     * @param string $eventName
     * @return void
     */
    public function removeListeners(string $eventName): void
    {
        if(isset($this->listeners[$eventName]))
            unset(
                $this->listeners[$eventName]
            );
    }

Related types

Clone this wiki locally