event

repository·master·Indexed 23 days ago

https://github.com/thephpleague/event

Searchable repository documentation for Thephpleague Event from https://github.com/thephpleague/event.

Tokens
1K
Snippets
1
Records
10
Agent score
82%

What's inside thephpleague/event

  1. Subscribe listeners using BufferedEventDispatcher

    master

    If the underlying EventDispatcherInterface passed to the BufferedEventDispatcher constructor also implements ListenerRegistry, you can use the buffered dispatcher to manage subscriptions.

    Supported methods include:

    • subscribeTo(string $event, callable $listener, int $priority = ListenerPriority::NORMAL): void
    • subscribeOnceTo(string $event, callable $listener, int $priority = ListenerPriority::NORMAL): void
    • subscribeListenersFrom(ListenerSubscriber $subscriber): void

    If the wrapped dispatcher does not implement ListenerRegistry, these methods will throw an UnableToSubscribeListener exception.

  2. Initialize the EventDispatcher

    master
    The EventDispatcher is the primary class for managing and dispatching events. You can instantiate it without arguments, in which case it will automatically use a PrioritizedListenerRegistry as its internal listener provider. Alternatively, you can provide your own implementation of Psr\/EventDispatcher\/ListenerProviderInterface to control how listeners are retrieved.
  3. Subscribe to events with priority in PrioritizedListenerRegistry

    master

    Use PrioritizedListenerRegistry to manage event listeners where execution order matters. You can attach listeners to specific event classes or event names using subscribeTo. If a priority is not provided, it defaults to ListenerPriority::NORMAL.

    To ensure a listener only runs once, use subscribeOnceTo.

  4. Use BufferedEventDispatcher to delay event dispatching

    master

    The BufferedEventDispatcher implements Psr\/EventDispatcher\/EventDispatcherInterface but instead of immediately dispatching events, it buffers them using internal event generation behavior.

    When you call dispatch($event), the event is recorded but not immediately sent to listeners. To actually trigger the listeners, you must call dispatchBufferedEvents(). This method iterates through all recorded events and dispatches them using the wrapped EventDispatcherInterface provided during construction.

  5. Retrieve listeners for an event instance

    master

    The getListenersForEvent method allows you to retrieve all registered listeners for a given event object. It supports two types of matching:

    1. Class-based matching: It iterates through registered event types and yields listeners if the provided event is an instance of the registered type.
    2. Name-based matching: If the event implements HasEventName, it will also yield listeners registered specifically for the string returned by $event->eventName().

    This method implements Psr\EventDispatcher\ListenerProviderInterface.

  6. Dispatch an event with dispatch()

    master

    Use the dispatch() method to trigger an event. It accepts any object as an event.

    • If the event implements Psr\/EventDispatcher\/StoppableEventInterface, the dispatcher will respect the event's propagation state. If a listener calls $event->stopPropagation(), subsequent listeners in the chain will not be executed.
    • If the event does not implement StoppableEventInterface, all listeners will be executed in order.
  7. Subscribe listeners to events

    master

    You can register listeners directly on the EventDispatcher using the following methods. Note that these methods require the internal listenerProvider to implement League\/Event\/ListenerRegistry (which PrioritizedListenerRegistry does).

    • subscribeTo(string $event, callable $listener, int $priority = ListenerPriority::NORMAL): Registers a listener for a specific event name or class. Use $priority to control execution order.
    • subscribeOnceTo(string $event, callable $listener, int $priority = ListenerPriority::NORMAL): Registers a listener that will only be triggered once.
    • subscribeListenersFrom(ListenerSubscriber $subscriber): Registers all listeners contained within a ListenerSubscriber object.