### Setup Doctrine Event Manager Source: https://github.com/doctrine/event-manager/blob/2.0.x/docs/en/reference/index.rst Demonstrates how to create an instance of the Doctrine Event Manager. This is the starting point for using the event system. ```php use Doctrine\Common\EventManager; $eventManager = new EventManager(); ``` -------------------------------- ### Install Doctrine Event Manager Source: https://github.com/doctrine/event-manager/blob/2.0.x/docs/en/reference/index.rst Installs the Doctrine Event Manager library using Composer. This is the primary method for adding the library to your project. ```sh $ composer require doctrine/event-manager ``` -------------------------------- ### Event Subscriber Example Source: https://github.com/doctrine/event-manager/blob/2.0.x/docs/en/reference/index.rst Illustrates the use of an Event Subscriber. The `TestEventSubscriber` class implements `Doctrine\Common\EventSubscriber` and defines which events it subscribes to via `getSubscribedEvents()`. ```php use Doctrine\Common\EventSubscriber; final class TestEventSubscriber implements EventSubscriber { /** @var bool */ public $preFooInvoked = false; public function preFoo() : void { $this->preFooInvoked = true; } public function getSubscribedEvents() : array { return [TestEvent::preFoo]; } } $eventSubscriber = new TestEventSubscriber(); $eventManager->addEventSubscriber($eventSubscriber); ``` -------------------------------- ### Custom Event Listener Example Source: https://github.com/doctrine/event-manager/blob/2.0.x/docs/en/reference/index.rst Defines a custom event listener class 'TestEvent' that subscribes to 'preFoo' and 'postFoo' events. It includes methods to handle these events and flags to track invocation. ```php use Doctrine\Common\EventArgs; use Doctrine\Common\EventManager; final class TestEvent { public const preFoo = 'preFoo'; public const postFoo = 'postFoo'; /** @var EventManager */ private $eventManager; /** @var bool */ public $preFooInvoked = false; /** @var bool */ public $postFooInvoked = false; public function __construct(EventManager $eventManager) { $eventManager->addEventListener([self::preFoo, self::postFoo], $this); } public function preFoo(EventArgs $eventArgs) : void { $this->preFooInvoked = true; } public function postFoo(EventArgs $eventArgs) : void { $this->postFooInvoked = true; } } // Create a new instance $testEvent = new TestEvent($eventManager); ``` -------------------------------- ### EventManager::getAllListeners() - Access All Listeners Source: https://github.com/doctrine/event-manager/blob/2.0.x/UPGRADE.md The `getAllListeners()` method is introduced to retrieve listeners for all events. This method should be used when you need to access the listeners of multiple events, replacing the previous behavior of calling `EventManager::getListeners()` without an event name. ```php /** * Returns all listeners for all events. * * @return array */ public function getAllListeners(): array; ``` -------------------------------- ### EventManager::getListeners() - Mandatory Event Parameter (v2.0) Source: https://github.com/doctrine/event-manager/blob/2.0.x/UPGRADE.md In version 2.0 of Doctrine Event Manager, the `$event` parameter for the `EventManager::getListeners()` method is now mandatory. This means you must specify the event name when retrieving listeners. For accessing listeners of all events, use the `getAllListeners()` method instead. ```php /** * Returns the listeners for a given event. * * @param string $event The event name * @return array */ public function getListeners(string $event): array; ``` -------------------------------- ### Deprecated EventManager::getListeners() without Event Name (v1.2) Source: https://github.com/doctrine/event-manager/blob/2.0.x/UPGRADE.md Version 1.2 of Doctrine Event Manager deprecated the practice of calling `EventManager::getListeners()` without providing an event name. Previously, this returned all listeners keyed by event name. The `getAllListeners()` method is now the recommended alternative for this functionality. ```php // Deprecated in v1.2 // Use getAllListeners() instead // public function getListeners(); ``` -------------------------------- ### Dispatching Event to Subscriber Source: https://github.com/doctrine/event-manager/blob/2.0.x/docs/en/reference/index.rst Dispatches an event ('preFoo') and shows how to verify if the event subscriber's corresponding method was invoked by checking its internal state. ```php $eventManager->dispatchEvent(TestEvent::preFoo); if ($eventSubscriber->preFooInvoked) { // the preFoo method was invoked } ``` -------------------------------- ### Dispatching Events Source: https://github.com/doctrine/event-manager/blob/2.0.x/docs/en/reference/index.rst Shows how to dispatch events using the `dispatchEvent()` method of the EventManager. This triggers the registered listeners for the specified events. ```php $eventManager->dispatchEvent(TestEvent::preFoo); $eventManager->dispatchEvent(TestEvent::postFoo); ``` -------------------------------- ### Removing Event Listeners Source: https://github.com/doctrine/event-manager/blob/2.0.x/docs/en/reference/index.rst Demonstrates how to remove a specific listener from the EventManager using the `removeEventListener()` method. This stops the listener from receiving further event notifications. ```php $eventManager->removeEventListener([TestEvent::preFoo, TestEvent::postFoo], $testEvent); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.