Doctrine Event Manager
repository·2.1.x·Indexed 26 days ago
https://github.com/doctrine/event-managerA 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.
What's inside Doctrine Event Manager
- 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.
Initialize the EventManager
2.1.xThe event system is managed by the
Doctrine\Common\EventManagerclass. Create a new instance to start managing events.use Doctrine\Common\EventManager; $eventManager = new EventManager();Install Doctrine Event Manager via Composer
2.1.xInstall the library using Composer to include it in your project.
$ composer require doctrine/event-managerMigrate `EventManager::getListeners()` usage for version 2.0
2.1.xIn version 2.0, the
$eventparameter forEventManager::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.Get started with Doctrine Event Manager
2.1.xTo begin using the Doctrine Event Manager, start by reading the Introduction section in the documentation to understand the core concepts and setup requirements.Get help with Doctrine Event Manager
2.1.xIf you encounter issues or have questions regarding the Doctrine Event Manager, you can seek assistance through the following channels:
- Slack: Join the
#event-managerchannel at https://www.doctrine-project.org/slack. - Stack Overflow: Use the
doctrine-event-managertag at http://stackoverflow.com/questions/tagged/doctrine-event-manager. - Mailing List: Join the Doctrine Mailing List at http://groups.google.com/group/doctrine-user.
- GitHub Issues: Report bugs or request features on the GitHub issues page.
- Slack: Join the
Dispatch Events
2.1.xTrigger an event using the
dispatchEvent()method. This will notify all registered listeners and subscribers associated with that event name.$eventManager->dispatchEvent(TestEvent::preFoo); $eventManager->dispatchEvent(TestEvent::postFoo);Use Event Subscribers
2.1.xEvent subscribers are classes that implement the
Doctrine\Common\EventSubscriberinterface. Instead of manually adding each event, the subscriber defines which events it wants to handle via thegetSubscribedEvents()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 }Register Event Listeners
2.1.xTo 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 } }Remove Event Listeners
2.1.xUse the
removeEventListener()method to stop an object from listening to specific events. You must pass the array of event names and the listener instance.$eventManager->removeEventListener([TestEvent::preFoo, TestEvent::postFoo], $testEvent);Register an event listener with addEventListener()
2.1.xUseaddEventListener()to register a listener object for one or more events. The listener object must have a method named after the event being dispatched. TheEventManagerprevents duplicate registration of the same listener instance for the same event using the object's unique ID.Dispatch an event with dispatchEvent()
2.1.xTrigger an event by callingdispatchEvent(). This method iterates through all registered listeners for the specified$eventNameand calls the corresponding method on each listener, passing the$eventArgsobject. If no$eventArgsare provided, an emptyEventArgsinstance is automatically created.