psr-discovery/event-dispatcher-implementations
Discovers installed PSR-14 event dispatcher implementations (Symfony, League, Yiisoft, etc.) at runtime and returns the first available instance. Ideal for libraries/SDKs to support PSR-14 without hard dependencies or user configuration.
Install via Composer:
composer require psr-discovery/event-dispatcher-implementations
Register the package in config/app.php under providers:
PsrDiscovery\EventDispatcherImplementations\EventDispatcherImplementationsServiceProvider::class,
Publish the config (if needed) with:
php artisan vendor:publish --provider="PsrDiscovery\EventDispatcherImplementations\EventDispatcherImplementationsServiceProvider"
First use case: Replace Laravel's default event dispatcher with a PSR-14 compliant implementation (e.g., Symfony's EventDispatcher). Bind it in AppServiceProvider:
public function register(): void
{
$this->app->bind(\Symfony\Component\EventDispatcher\EventDispatcherInterface::class, function ($app) {
return new \Symfony\Component\EventDispatcher\EventDispatcher();
});
}
'listeners' => [
'App\Events\OrderPlaced' => [
'App\Listeners\NotifyCustomer',
'App\Listeners\LogOrder',
],
],
$dispatcher->addListener(OrderPlaced::class, new NotifyCustomer());
// Laravel-style
event(new OrderPlaced($order));
// PSR-14 style
$dispatcher->dispatch(new OrderPlaced($order));
EventSubscriberInterface for subscriber-based patterns:
class OrderSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
OrderPlaced::class => 'onOrderPlaced',
];
}
}
$mockDispatcher = $this->createMock(EventDispatcherInterface::class);
$this->app->instance(EventDispatcherInterface::class, $mockDispatcher);
php: in composer.json:
"config": {
"platform": {
"php": "8.2"
}
}
Run composer update afterward.php artisan config:clear
Or inspect the dispatcher’s listeners:
$dispatcher->getListenersFor(OrderPlaced::class);
A calls B, which calls A).EventDispatcherImplementationsServiceProvider to add your own PSR-14 implementations.EventDispatcher for priority-based listeners (not natively supported in Laravel’s dispatcher):
$dispatcher->addListener(OrderPlaced::class, new NotifyCustomer(), 100); // Higher priority
Psr\EventDispatcher\StoppableEventInterface).How can I help you explore Laravel packages today?