Event Dispatcher will allow you to attach listeners and notify those listeners so they can handle the event that you dispatch. The Event Dispatcher is PSR-14 Compatible with extra features.
Features
Event Names
Event Subscribers
Listener Priorities
Installation
composerrequiresonsofphp/event-dispatcher
Usage
<?phpuseSonsOfPHP\Component\EventDispatcher\EventDispatcher;$dispatcher =newEventDispatcher();// If you have a custom ListenerProviderInterface you can inject it into the// EventDispatcher//$dispatcher = new EventDispatcher($provider);$dispatcher->addListener($event::class,function ($event, $eventName, $dispatcher) {});$dispatcher->addListener('event.name',function ($event, $eventName, $dispatcher) {});$dispatcher->addSubscriber($subscriber);$dispatcher->dispatch($event); // PSR-14$dispatcher->dispatch($event,'event.name'); // Custom Event Name
The priority will default to 0. Lower numbers are higher priority. Higher numbers will be handled later. For example, a listener with a priority of -1 will be handled before a listener of priority 1.
Stoppable Events
If your code extends the AbstractStoppableEvent and within your listener or subscriber code, you execute $this->stopPropagation(); and it will return the event and no more listeners or subscribers will handle that event.
The dispatcher will always invoke the Listener with those three arguments in that order. If you do not need to know the event name or if you do not need the event dispatcher, you can ignore those two arguments.
Creating an Event Subscriber
Subscribers allow you to "subscribe" to multiple events.
<?phpuseOrderCreated;useOrderUpdated;useOrderDeleted;useSonsOfPHP\Component\EventDispatcher\EventSubscriberInterface;classOrderSubscriberimplementsEventSubscriberInterface{// ...publicstaticfunctiongetSubscribedEvents() {// Can return like this:yieldOrderCreated::class=>'onCreated';yieldOrderUpdated::class=> ['onUpdated',100];yieldOrderDeleted::class=> [['onDeleted',100], ['doFirstOnDeleted',-100]];// OR like thisreturn [OrderCreated::class=>'onCreated',OrderUpdated::class=> ['onUpdated',100],OrderDeleted::class=> [['onDeleted',100], ['doFirstOnDeleted',-100]], ]; }}