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

Defines lightweight, version-stable contracts for Symfony’s EventDispatcher: interfaces and abstractions shared across components. Use it to type-hint and build compatible event dispatching integrations with proven Symfony semantics without pulling full implementations.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Framework-Agnostic Alignment: The package enables Laravel to adopt PSR-14 contracts, reducing vendor lock-in and enabling cross-framework interoperability (e.g., shared libraries with Symfony). This is critical for projects targeting modular architectures or microservices.
  • Decoupling Benefits: Event definitions become reusable value objects, aligning with DDD principles. However, Laravel’s native Event class already extends Symfony’s Event, so the primary value lies in dispatcher abstraction rather than event definitions.
  • Hybrid Architecture Risk: Laravel’s dispatcher (Illuminate\Events) is not PSR-14-compliant, creating a mismatch between event contracts and dispatching logic. This requires a bridge (e.g., spatie/laravel-psr-event-dispatcher) to fully leverage PSR-14.
  • Key Fit Criteria:
    • Must: Cross-framework reuse (e.g., shared domain events) or PSR-14 compliance requirements.
    • Should: Modular design, long-term maintainability, or migration plans to/from Symfony.
    • Avoid: Monolithic Laravel apps with no interoperability needs or tight coupling to Illuminate\Events.

Integration Feasibility

  • Event Definitions: Trivial to adopt—replace use Illuminate\Contracts\Events\Dispatcher with PSR-14 interfaces in event classes. Zero runtime impact if using Laravel’s dispatcher.
  • Dispatcher Integration: High effort due to Laravel’s non-PSR-14 dispatcher. Requires:
    • A bridge (e.g., spatie/laravel-psr-event-dispatcher) to translate between Laravel’s and PSR-14 dispatchers.
    • Migration of listeners to PSR-14’s ListenerProvider or EventDispatcherInterface.
  • Tooling Gaps: Laravel’s make:event does not generate PSR-14-compliant classes by default, necessitating custom templates or post-generation scripts.
  • Dependents Risk: The package has 0 dependents, increasing the risk of unsupported edge cases in Laravel-specific tooling.

Technical Risk

  • Breaking Changes:
    • Listener methods must change from handle() to __invoke() for PSR-14 compliance.
    • EventServiceProvider bindings may need refactoring to use ListenerProvider.
  • Performance Overhead: Bridge layers (e.g., adapters) introduce indirection. Benchmarking is critical before production use.
  • PHP Version: Requires PHP 8.1+, aligning with Laravel 10+ but excluding older projects.
  • Tooling Dependencies: Custom solutions may be needed for IDE support, testing (e.g., Event::fake()), and debugging.

Key Questions

  1. Strategic Justification:
    • Is cross-framework interoperability a hard requirement, or is this a premature abstraction for a Laravel-only project?
    • Does the team have a roadmap for Symfony integration or shared libraries?
  2. Dispatcher Strategy:
    • Will the project use Laravel’s native dispatcher (with PSR-14 events) or migrate to a PSR-14-compliant dispatcher (e.g., Symfony’s)?
    • What is the trade-off between immediate compatibility (hybrid approach) and long-term flexibility (full PSR-14)?
  3. Listener Migration:
    • How will existing listeners (e.g., EventServiceProvider bindings) adapt to PSR-14’s ListenerProvider or EventDispatcherInterface?
    • Are there third-party packages that rely on Laravel’s Event class or Illuminate\Events APIs?
  4. Testing and Observability:
    • Does the project use event-based testing (e.g., Event::fake())? Custom test doubles may be required for PSR-14.
    • How will event serialization/deserialization (e.g., for logging or analytics) be standardized across frameworks?
  5. Team and Tooling:
    • Is the team familiar with PSR-14, or will this introduce a learning curve?
    • Are there existing tools (e.g., IDE plugins, debug bars) that need updates to support PSR-14 events?

Integration Approach

Stack Fit

  • Partial Fit: The package’s event contracts integrate seamlessly with Laravel’s event definitions but not its dispatcher. This creates a semantic upgrade rather than a drop-in replacement.
    • Best For:
      • New projects requiring PSR-14 compliance (e.g., microservices, shared libraries).
      • Existing projects adopting Symfony components (e.g., symfony/event-dispatcher as the primary dispatcher).
    • Not Ideal For:
      • Monolithic Laravel apps with deep ties to Illuminate\Events.
      • Projects where PSR-14 adds unnecessary complexity (e.g., simple CRUD apps).
  • Laravel-Specific Considerations:
    • Laravel’s Event class already extends Symfony’s Event, so event definitions can be PSR-14-compliant without changes.
    • The dispatcher layer (Illuminate\Events) is the primary integration challenge.

Migration Path

  1. Phase 1: Event Definitions (Low Risk, High Reward)

    • Action: Update event classes to extend Symfony\Contracts\EventDispatcher\Event or implement EventInterface.
    • Example:
      // Before (Laravel-specific)
      class UserRegistered implements ShouldBroadcast {
          use Dispatchable, SerializesModels;
          public User $user;
      }
      // After (PSR-14 compliant)
      class UserRegistered extends \Symfony\Contracts\EventDispatcher\Event {
          public function __construct(public User $user) {}
      }
      
    • Tooling: Use a custom make:event template or post-class script to enforce PSR-14 compliance.
    • Impact: Zero runtime changes if using Laravel’s dispatcher. Enables future compatibility.
  2. Phase 2: Dispatcher Integration (High Risk, Strategic Decision)

    • Option A: Hybrid Approach (Recommended for Gradual Migration)
      • Action: Keep Laravel’s Event facade but use PSR-14-compliant event classes.
      • Example:
        event(new UserRegistered($user)); // Works with Laravel’s dispatcher
        
      • Pros:
        • Zero breaking changes to dispatching logic.
        • Immediate benefit of PSR-14 event definitions.
      • Cons:
        • Does not fully leverage PSR-14’s cross-framework capabilities.
        • Still tied to Laravel’s dispatcher.
    • Option B: Full PSR-14 Dispatcher (Breaking Change)
      • Action: Replace Laravel’s dispatcher with a PSR-14-compliant one (e.g., symfony/event-dispatcher via spatie/laravel-psr-event-dispatcher).
      • Steps:
        1. Install the bridge: composer require spatie/laravel-psr-event-dispatcher.
        2. Update EventServiceProvider to register listeners using ListenerProvider or EventDispatcherInterface.
        3. Replace event() calls with PSR-14’s dispatch() where needed.
      • Pros:
        • Full PSR-14 compliance, enabling shared dispatchers across frameworks.
        • Aligns with Symfony’s ecosystem.
      • Cons:
        • Significant refactoring required.
        • Potential breaking changes for third-party packages.
        • May require custom solutions for Laravel-specific features (e.g., broadcasting).
  3. Phase 3: Listener Migration (Moderate Risk)

    • Action: Update listeners to use PSR-14’s __invoke() method instead of handle().
    • Example:
      // Before (Laravel)
      public function handle(UserRegistered $event) { ... }
      // After (PSR-14)
      public function __invoke(UserRegistered $event) { ... }
      
    • Tooling: Use static analysis (e.g., PHPStan) to identify non-compliant listeners.
    • Impact: May require updates to EventServiceProvider bindings or third-party listener registrations.
  4. Phase 4: Testing and Validation

    • Action: Verify:
      • Event listeners work with PSR-14 events (e.g., __invoke() signatures).
      • Cross-framework compatibility if using shared libraries.
      • Performance benchmarks with/without the bridge layer.
    • Tools:
      • Custom test doubles for Event::fake() if using Laravel’s testing helpers.
      • Integration tests with Symfony’s dispatcher to validate interoperability.

Compatibility

  • Laravel Versions:
    • Laravel 10+: Full compatibility (PHP 8.1+ required).
    • Laravel <10: Requires PHP upgrade or polyfills for modern features (e.g., readonly properties).
  • Third-Party Packages:
    • Risk of incompatibility if packages assume Laravel’s Event class or Illuminate\Events APIs.
    • Example: Packages using ShouldBroadcast or Dispatchable traits may need updates.
  • Symfony Components:
    • Seamless integration with symfony/event-dispatcher or other PSR-14-compliant dispatchers
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.
codraw/entity-migrator
codraw/doctrine-extra
codraw/aws-tool-kit
codraw/validator
codraw/workflow
codraw/open-api
codraw/cron-job
codraw/process
codraw/log
nexmo/api-specification
capell-app/block-library
axium/identity
cetria/laravel-dummy-models
cetria/reflection-helper
agropredict/sso-auth-bundle
evolvestudio/spam-protection
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony