Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Event Dispatcher Implementations Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

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();
    });
}

Implementation Patterns

Core Workflow

  1. PSR-14 Compliance: Use the package to integrate PSR-14 event dispatchers (e.g., Symfony, League) alongside Laravel’s native events.
  2. Event Listeners: Register listeners via:
    • Config file (published by the package):
      'listeners' => [
          'App\Events\OrderPlaced' => [
              'App\Listeners\NotifyCustomer',
              'App\Listeners\LogOrder',
          ],
      ],
      
    • Programmatically in a service provider:
      $dispatcher->addListener(OrderPlaced::class, new NotifyCustomer());
      
  3. Hybrid Dispatching: Dispatch both Laravel and PSR-14 events:
    // Laravel-style
    event(new OrderPlaced($order));
    
    // PSR-14 style
    $dispatcher->dispatch(new OrderPlaced($order));
    

Integration Tips

  • Middleware: Use Symfony’s EventSubscriberInterface for subscriber-based patterns:
    class OrderSubscriber implements EventSubscriberInterface
    {
        public static function getSubscribedEvents(): array
        {
            return [
                OrderPlaced::class => 'onOrderPlaced',
            ];
        }
    }
    
  • Testing: Mock the dispatcher in tests:
    $mockDispatcher = $this->createMock(EventDispatcherInterface::class);
    $this->app->instance(EventDispatcherInterface::class, $mockDispatcher);
    

Gotchas and Tips

Breaking Changes (1.2.0)

  • PHP 8.2 Requirement: Ensure your project meets the minimum PHP version. Update php: in composer.json:
    "config": {
        "platform": {
            "php": "8.2"
        }
    }
    
    Run composer update afterward.

Debugging

  • Listener Registration: Verify listeners are bound via:
    php artisan config:clear
    
    Or inspect the dispatcher’s listeners:
    $dispatcher->getListenersFor(OrderPlaced::class);
    
  • Circular Dependencies: Avoid circular references between listeners/subcribers (e.g., A calls B, which calls A).

Extension Points

  • Custom Dispatchers: Extend the base EventDispatcherImplementationsServiceProvider to add your own PSR-14 implementations.
  • Priority Handling: Use Symfony’s EventDispatcher for priority-based listeners (not natively supported in Laravel’s dispatcher):
    $dispatcher->addListener(OrderPlaced::class, new NotifyCustomer(), 100); // Higher priority
    
  • Event Normalization: Convert Laravel events to PSR-14-compliant classes if needed (e.g., implement Psr\EventDispatcher\StoppableEventInterface).
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
terminal42/code-quality-tools
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky