Feature: Event Listeners
Problem Statement
The current Hazaar framework lacks a formalised event listener system that allows for decoupled application logic. This limits flexibility for developers wanting to implement modular responses to application events such as user logins, database operations, or lifecycle events.
Who will benefit?
- Developers creating modular and maintainable applications.
- Teams implementing cross-cutting concerns like logging, auditing, or caching.
- Plugin or module developers who need to hook into framework behaviour without modifying core logic.
Benefits and risks
Benefits:
- Encourages a decoupled architecture by separating event emitters and listeners.
- Allows easier testing and debugging of isolated logic.
- Enables reusable components or modules to respond to events.
- Supports clean and maintainable extension mechanisms.
Risks:
- Slight increase in framework complexity.
- Poorly managed events could lead to performance degradation or race conditions.
- Requires developer discipline to avoid overusing the event system in place of direct logic.
Proposed solution
Introduce an event system with the following features:
- A central
EventDispatcher
service that can be registered in the application container. -
Event::dispatch('event.name', $payload)
to trigger an event. - Listeners defined via config (
config/events.php
) or programmatically usingEvent::listen('event.name', $callback)
. - Support for:
- Synchronous and asynchronous listeners.
- Listener priorities.
- Anonymous closures or class-based handlers.
- Integration with application lifecycle events and framework components (e.g. controller pre/post dispatch, model operations).
Examples
Event dispatching:
Event::dispatch('user.registered', $user);
Registering a listener
Event::listen('user.registered', function($user) {
// Send welcome email
});
Class-based listener:
class SendWelcomeEmail {
public function handle($user) {
// logic here
}
}
Event::listen('user.registered', [SendWelcomeEmail::class, 'handle']);
Inspired by other frameworks that implement robust event-driven architectures (e.g., Laravel’s event and listener system).
Priority/Severity
-
High (This will bring a huge increase in performance/productivity/usability/legislative cover) -
Medium (This will bring a good increase in performance/productivity/usability) -
Low (anything else e.g., trivial, minor improvements)