Doctrine Event Manager

repository·2.1.x·Indexed 26 days ago

https://github.com/doctrine/event-manager

A lightweight PHP library providing a simple event system to implement the Observer pattern. It allows components to communicate via events using the Doctrine\Common\EventManager class, supporting event listeners, event subscribers via the EventSubscriber interface, and the EventArgs class for stateless events.

Tokens
2K
Snippets
6
Records
16
Agent score
91%

What's inside Doctrine Event Manager

  1. Overview of Doctrine Event Manager

    2.1.x
    The Doctrine Event Manager is a PHP library that provides a simple, lightweight event system. It allows you to implement the Observer pattern, enabling components to communicate via events without being tightly coupled.
  2. Migrate `EventManager::getListeners()` usage for version 2.0

    2.1.x

    In version 2.0, the $event parameter for EventManager::getListeners() is now mandatory. You must specify the specific event name you wish to fetch listeners for.

    If your goal is to retrieve all listeners across all events, use the getAllListeners() method instead.

  3. Get help with Doctrine Event Manager

    2.1.x

    If you encounter issues or have questions regarding the Doctrine Event Manager, you can seek assistance through the following channels:

  4. Use Event Subscribers

    2.1.x

    Event subscribers are classes that implement the Doctrine\Common\EventSubscriber interface. Instead of manually adding each event, the subscriber defines which events it wants to handle via the getSubscribedEvents() method. The method name in the subscriber must match the event name exactly.

    use Doctrine\Common\EventSubscriber;
    
    final class TestEventSubscriber implements EventSubscriber
    {
        public $preFooInvoked = false;
    
        public function preFoo() : void
        {
            $this->preFooInvoked = true;
        }
    
        /**
         * @return array<string> List of event names to subscribe to
         */
        public function getSubscribedEvents() : array
        {
            return [TestEvent::preFoo];
        }
    }
    
    $eventSubscriber = new TestEventSubscriber();
    $eventManager->addEventSubscriber($eventSubscriber);
    
    // Dispatching the event will trigger the subscriber's preFoo method
    $eventManager->dispatchEvent(TestEvent::preFoo);
    
    if ($eventSubscriber->preFooInvoked) {
        // the preFoo method was invoked
    }
  5. Register Event Listeners

    2.1.x

    To listen for specific events, use the addEventListener() method. You provide an array of event names and the listener object. The listener object must contain methods named exactly after the event names to be triggered.

    use Doctrine\Common\EventArgs;
    use Doctrine\Common\EventManager;
    
    final class TestEvent
    {
        public const preFoo = 'preFoo';
        public const postFoo = 'postFoo';
    
        /** @var EventManager */
        private $eventManager;
    
        public function __construct(EventManager $eventManager)
        {
            // Register this object as a listener for preFoo and postFoo
            $eventManager->addEventListener([self::preFoo, self::postFoo], $this);
        }
    
        public function preFoo(EventArgs $eventArgs) : void
        {
            // Logic for preFoo event
        }
    
        public function postFoo(EventArgs $eventArgs) : void
        {
            // Logic for postFoo event
        }
    }
  6. Register an event listener with addEventListener()

    2.1.x
    Use addEventListener() to register a listener object for one or more events. The listener object must have a method named after the event being dispatched. The EventManager prevents duplicate registration of the same listener instance for the same event using the object's unique ID.
  7. Dispatch an event with dispatchEvent()

    2.1.x
    Trigger an event by calling dispatchEvent(). This method iterates through all registered listeners for the specified $eventName and calls the corresponding method on each listener, passing the $eventArgs object. If no $eventArgs are provided, an empty EventArgs instance is automatically created.