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 Contracts Laravel Package

symfony/event-dispatcher-contracts

Interfaces and base abstractions for Symfony’s event dispatching system. Use these contracts to standardize how events and listeners interact, and to build libraries compatible with Symfony components and their proven implementations.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Pros: Aligns with PSR-14 standards, enabling framework-agnostic event handling and reducing vendor lock-in. Ideal for shared libraries, microservices, or projects requiring interoperability with Symfony/Laravel/other ecosystems. Laravel’s native Event class already extends Symfony\Contracts\EventDispatcher\Event, so minimal changes are needed for basic adoption.
  • Cons: Misaligned with Laravel’s native event system (non-PSR-14), introducing unnecessary complexity for pure Laravel projects. Overkill for simple event-driven workflows where Laravel’s built-in system suffices.

Integration Feasibility

  • Low for vanilla Laravel: Requires explicit adoption of PSR-14 patterns (e.g., defining events as Symfony\Contracts\EventDispatcher\Event subclasses) or a bridge (e.g., laravel-psr-event-dispatcher). No direct integration with Laravel’s EventServiceProvider or event() helper without custom logic.
  • High for cross-framework projects: Seamless if the project already uses Symfony’s dispatcher or plans to support multiple frameworks. Existing Laravel events can be retrofitted with minimal effort (e.g., extending Symfony\Contracts\EventDispatcher\Event).
  • Risk: Technical debt if the project later migrates away from PSR-14 or if Laravel’s native system evolves to support PSR-14 natively (reducing the need for this package).

Technical Risk

  • Low for interfaces: Pure contracts pose no runtime risk; they only enforce consistency.
  • Moderate for implementation: Requires discipline to adhere to PSR-14 patterns (e.g., avoiding Laravel-specific event traits/methods like broadcastOn). Potential for fragmentation if multiple PSR-14 implementations (e.g., Symfony’s dispatcher vs. Laravel’s) behave differently.
  • Dependencies: PHP 8.1+ requirement aligns with Laravel 10+, but PSR-14 adoption remains unofficial in Laravel’s core. Risk of maintaining custom bridges or adapters if Laravel’s event system diverges.

Key Questions

  1. Strategic Fit:
    • Is framework-agnostic event handling a core requirement (e.g., shared libraries, microservices), or is Laravel’s native system sufficient?
    • Will the project use this for domain events (high cohesion) or trivial hooks (low value)?
  2. Adoption Path:
    • Are existing events already compatible with Symfony\Contracts\EventDispatcher\Event? If not, what’s the migration effort?
    • Does the team have experience with PSR-14 or Symfony’s event system? If not, what’s the ramp-up cost?
  3. Tooling:
    • Are there mature Laravel-specific PSR-14 bridges (e.g., laravel-psr-event-dispatcher)? If not, is the team willing to maintain a custom adapter?
  4. Future-Proofing:
    • How might Laravel’s event system evolve to support PSR-14 natively (e.g., via a future Laravel release or community package)?
    • Could this package introduce unnecessary complexity if Laravel’s native system becomes PSR-14-compliant?

Integration Approach

Stack Fit

  • Partial Fit for Laravel: The package is a supplemental layer, not a replacement for Laravel’s event system. Best suited for:
    • New Projects: Designing events from the ground up with PSR-14 in mind (e.g., domain-driven design).
    • Shared Libraries: Creating reusable event classes consumed by Laravel and other frameworks.
    • Hybrid Architectures: Projects using Symfony’s dispatcher alongside Laravel (e.g., via symfony/event-dispatcher + a PSR-14 bridge).
  • Non-Fit for:
    • Monolithic Laravel apps relying on Laravel’s native event system (e.g., EventServiceProvider, event() helper).
    • Projects without a clear need for cross-framework interoperability.

Migration Path

  1. Assessment Phase:
    • Audit existing events to identify candidates for PSR-14 adoption (e.g., domain events with high reuse potential).
    • Evaluate whether to use the package for all events or only new ones.
  2. Incremental Adoption:
    • Step 1: Update event classes to extend Symfony\Contracts\EventDispatcher\Event (or implement EventInterface).
      // Before (Laravel-native)
      class UserRegistered implements ShouldBroadcast {}
      
      // After (PSR-14 compliant)
      class UserRegistered extends Symfony\Contracts\EventDispatcher\Event
      {
          public function __construct(public User $user) {}
      }
      
    • Step 2: Replace direct EventServiceProvider bindings with PSR-14-compatible listeners (if using a bridge like laravel-psr-event-dispatcher).
    • Step 3: Update tests to use PSR-14 interfaces (e.g., mock EventDispatcherInterface).
  3. Bridge Integration (Optional):
    • If using Laravel’s native dispatcher, leverage its compatibility with Symfony\Contracts\EventDispatcher\Event.
    • For full PSR-14 support, integrate a bridge (e.g., laravel-psr-event-dispatcher) to connect Laravel’s dispatcher to PSR-14 listeners.

Compatibility

  • Laravel 10+: Full compatibility due to PHP 8.1+ support and Laravel’s Event class extending Symfony\Contracts\EventDispatcher\Event.
  • Older Laravel: Requires PHP 8.1+ and may need polyfills for newer features (e.g., readonly properties).
  • Symfony Integration: Seamless if using symfony/event-dispatcher alongside Laravel (via a PSR-14 bridge).
  • Third-Party Libraries: Events defined with this package can be consumed by any PSR-14-compliant dispatcher (e.g., league/event, symfony/event-dispatcher).

Sequencing

  1. Design Phase:
    • Define event contracts (e.g., UserRegistered) as PSR-14-compliant classes.
    • Decide on event naming conventions (e.g., constants for event names).
  2. Implementation Phase:
    • Refactor existing events incrementally (prioritize high-value domain events).
    • Update listeners to work with PSR-14 interfaces (e.g., EventDispatcherInterface).
  3. Testing Phase:
    • Verify events fire correctly in both Laravel’s native dispatcher and PSR-14 dispatchers (if applicable).
    • Test cross-framework compatibility (e.g., dispatching events from a Symfony CLI tool to a Laravel app).
  4. Deployment Phase:
    • Roll out changes in feature flags or per-module to minimize risk.
    • Monitor for issues with event propagation or listener registration.

Operational Impact

Maintenance

  • Pros:
    • Decoupling: Events become framework-agnostic, reducing friction if the project later adopts Symfony or other frameworks.
    • Standardization: PSR-14 enforces consistent event design (e.g., immutable events via Event base class).
  • Cons:
    • Additional Abstraction: Requires maintaining PSR-14-compliant event classes alongside Laravel’s native system, increasing cognitive load.
    • Bridge Dependencies: If using a PSR-14 bridge (e.g., laravel-psr-event-dispatcher), it must be kept updated with Laravel’s changes.
    • Debugging Complexity: Events may behave differently across dispatchers (e.g., Laravel’s dispatch() vs. Symfony’s dispatch()), requiring clear documentation.

Support

  • Pros:
    • Community: Leverages Symfony’s battle-tested contracts and PSR-14 ecosystem (e.g., documentation, tools like symfony/var-dumper).
    • Tooling: IDE support for PSR-14 interfaces (e.g., autocompletion for EventInterface).
  • Cons:
    • Limited Laravel-Specific Resources: Fewer tutorials/examples for Laravel + PSR-14 integration compared to Symfony.
    • Fragmented Debugging: Issues may span Laravel’s event system and PSR-14 implementations, requiring cross-stack debugging skills.

Scaling

  • Pros:
    • Horizontal Scalability: PSR-14 events can be dispatched across services (e.g., via message queues) without framework-specific serialization.
    • Modularity: Events can be consumed by non-Laravel components (e.g., Symfony CLI tools, ReactPHP apps).
  • Cons:
    • Performance Overhead: Indirect dispatch (e.g., via a PSR-14 bridge) may add latency compared to Laravel’s native system.
    • State Management: Events must be designed as immutable value objects (per PSR-14), which may require refactoring stateful event classes.

Failure Modes

Failure Scenario Impact Mitigation
Event not dispatched Listeners miss critical updates. Use symfony/var-dumper to inspect event objects; validate dispatcher config.
Incompatible PSR-14 implementations Events behave differently across
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.
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope
anil/file-picker
broqit/fields-ai