boson-php/event-listener-contracts
Lightweight PHP contracts for event listener components in the Boson ecosystem. Defines interfaces and shared types to standardize registering, dispatching, and handling events, helping packages stay decoupled while remaining interoperable across implementations.
Illuminate\Events). It enforces a clean separation between event dispatching and handling, promoting loose coupling and testability.EventListenerContract, EventListenerInterface) encourage modular design, making it easier to swap implementations or integrate third-party listeners without modifying core logic.Model::saved(), Job::dispatching()), this package could standardize listener behavior across custom and framework-provided events, reducing inconsistency.boson-php/boson, suggesting it’s designed for incremental adoption. Existing Laravel event listeners can be retrofitted to conform to the contracts with minimal changes (e.g., adding handle() method, implementing EventListenerContract).Listener class in future versions), the contracts might need updates to stay compatible.Why Standardize Now?
handle() signatures, ad-hoc middleware)?Adoption Scope
Testing Strategy
Performance
Long-Term Maintenance
Listener classes with classes implementing EventListenerContract.listen() method in EventServiceProvider or listen() macros.// Before
Event::listen(MyEvent::class, function ($event) { ... });
// After (using contract)
Event::listen(MyEvent::class, new MyListener());
class ClosureListener implements EventListenerContract {
public function __construct(private Closure $handler) {}
public function handle($event) { ($this->handler)($event); }
}
UserRegistered).implements EventListenerContract and handle() method stubs.EventListenerContract instead of closures.EventServiceProvider to validate listeners at runtime (optional).Illuminate\Contracts\Events\Dispatcher for changes that might affect the contracts.handle() method simplifies logging and error handling (e.g., wrap handle() in a try-catch).SlackNotifier for a DiscordNotifier).handle() threw an exception for event X").EventListenerContract, it may fail silently or throw errors at runtime.Listener interface), the contracts may need updates.ListenerA dispatches EventB handled by ListenerB, which dispatches EventA) could cause infinite loops.php artisan make:listener MyEvent --contract
How can I help you explore Laravel packages today?