Event System
The WebFramework provides a robust event system that allows for decoupled communication between different parts of your application. This system follows the observer pattern and supports both synchronous and asynchronous event handling.
Core Components
EventService
The EventService is the central manager for all events in the application. It provides methods to:
- Register events and their listeners
- Dispatch events
- Manage event listeners
- Handle queued event listeners
Event Interface
Events are simple data containers that implement the Event interface. They should contain only the data needed for the event, not the logic:
EventListener Interface
Event listeners implement the EventListener interface and contain the actual logic for handling events:
QueuedEventListener
The QueuedEventListener abstract class provides a base for event listeners that should be processed asynchronously:
| PHP | |
|---|---|
Usage
Creating an Event
An Event should only contain data and no logic. All logic related to an Event should be in a separate EventListener.
| PHP | |
|---|---|
Creating a Synchronous Event Listener
| PHP | |
|---|---|
Creating a Queued Event Listener
Registering Events and Listeners
Dispatching Events
| PHP | |
|---|---|
Application Integration
Here are real examples from the WebFramework showing how events are used in practice:
User Login Events
When a user successfully logs in, the framework dispatches a UserLoggedIn event:
| PHP | |
|---|---|
Password Change Events
When a user changes their password, a UserPasswordChanged event is dispatched:
| PHP | |
|---|---|
Email Change Events
When a user's email address is changed, a UserEmailChanged event is dispatched:
| PHP | |
|---|---|
User Verification Events
When a user completes email verification, a UserVerified event is dispatched:
| PHP | |
|---|---|
Queue Setup for Asynchronous Events
To use queued event listeners, you must:
- Register the EventJobHandler in your application configuration:
- Run queue workers to process the jobs:
| Bash | |
|---|---|
- Configure queue names in your listeners:
| PHP | |
|---|---|
Queued events will only be handled if you have a QueueWorker active and registered the EventJobHandler to handle EventJobs.
Framework Events
WebFramework itself will send out the following events:
- UserLoggedIn
- UserPasswordChanged
- UserVerified
- UserEmailChanged
Error Handling & Debugging
Unregistered Events
If you dispatch an event that hasn't been registered, the EventService will log a debug message and silently continue:
| PHP | |
|---|---|
Listener Errors
If a listener throws an exception, it will bubble up and stop processing of subsequent listeners. Wrap listener logic in try-catch blocks for graceful error handling:
| PHP | |
|---|---|
Debugging
Enable debug logging to see event dispatch activity: - Synchronous events: "Dispatching non-queued event" - Asynchronous events: "Dispatching queued event"
Performance Considerations
Synchronous vs Asynchronous Processing
- Use synchronous listeners for lightweight operations that must complete immediately
- Use
QueuedEventListenerfor heavy operations like email sending, file processing, or external API calls - Synchronous listeners block the request until completion
- Queued listeners are processed asynchronously by queue workers
Queue Configuration
For queued events, ensure you have: 1. Queue workers running to process jobs 2. EventJobHandler registered with QueueService 3. Appropriate queue names for different types of processing
Testing Event-Driven Code
Testing Event Dispatch
| PHP | |
|---|---|
Testing Event Listeners
| PHP | |
|---|---|
Best Practices
- Keep events focused on a single state change
- Use descriptive event names that reflect past tense (e.g.,
UserRegisteredEvent) - Include only necessary data in events
- Use queued listeners for time-consuming operations
- Implement proper error handling in listeners
- Consider using different queues for different types of listeners
- Monitor event processing and queue sizes
- Implement proper logging in event listeners