Internal System Events
Overview
DomainFlow Core emits a variety of system events throughout its lifecycle. These internally generated events allow you to monitor, log, and react to key operations without modifying core functionality. By tapping into these events, you can extend or customize behaviors for tasks like bootstrapping, caching, middleware execution, and application termination.
Boot Lifecycle Events
- booting.init: Fired when the boot process starts. Use this event to run early initialization logic.
- booting.error: Triggered if an error occurs during the boot callbacks, allowing you to catch and log startup issues.
- booting.complete: Emitted once the boot process has finished successfully, signaling that all services and providers are fully loaded.
Path and Configuration Events
- path.base.set: Fired when the application successfully sets the base path.
- path.base.error: Emitted if an invalid base path is provided.
- path.config.set: Triggered when the configuration path is correctly set.
- path.config.error: Fired if there is an error setting the configuration path.
- path.environment.set: Emitted when the current environment (such as production or development) is updated.
Cache-Related Events
- cache.directory.created: Fired when the cache directory is successfully created.
- cache.directory.creation.error: Emitted if the cache directory creation fails.
- cache.set.cache.path: Triggered when a custom cache path is established.
- cache.cleaned: Indicates that the cache was successfully cleared.
- cache.cleaned.error: Emitted if an error occurs during the cache cleaning process.
- cache.saved: Fired after successfully writing resolved services to a cache file.
- cache.loaded: Emitted when cached services are loaded.
- cache.external.set and cache.external.loaded: Indicate the setup and loading of an external cache system.
- cache.invalidated: Triggered when the in-memory cache is reset.
- cache.updated: Emitted when new entries are merged into the cache.
Middleware Pipeline Events
- middleware.pipeline.start: Fired at the beginning of middleware pipeline execution.
- middleware.pipeline.end: Emitted when the middleware pipeline has completed processing.
- middleware.error: Triggered if an error occurs during middleware execution.
Termination Events
- termination.init: Fired when the termination process begins.
- termination.complete: Emitted after all termination callbacks have been executed.
- termination.error: Triggered if an error occurs during any termination callback.
Service Provider Events
- service_provider.registered: Fired when a service provider is successfully registered.
- service_provider.unregistered: Emitted when a provider is removed from the application.
- service_provider.deferred.loaded: Triggered when a deferred provider is loaded on-demand.
Event Manager Specific Events
- event_manager.dispatcher.set: Fired when a new event dispatcher is set.
- event_manager.dispatch.error: Emitted if an error occurs during the dispatching of an event.
Consuming System Events at Runtime
You can subscribe to these system events to execute custom logic during your application's lifecycle. Below is an example that demonstrates how to attach event listeners for key system events and perform actions such as logging, debugging, or triggering alerts.
Example Usage
// Assume $app is your DomainFlow Core application instance.
// Listen for the boot process start event.
$app->on('booting.init', function ($app) {
echo "Boot process started. Initializing components...\n";
});
// Listen for the middleware pipeline start event.
$app->on('middleware.pipeline.start', function ($payload) {
error_log("Middleware pipeline started with payload: " . json_encode($payload));
});
// Listen for the termination complete event.
$app->on('termination.complete', function ($app) {
echo "Application terminated successfully.\n";
});
// Simulate application lifecycle events for demonstration.
// Start the boot process (this will trigger the 'booting.init' event).
$app->boot();
// Execute the middleware pipeline with an initial payload and a final callback.
$result = $app->pipeline(['data' => 'example'], function ($payload) {
// Final processing logic.
return "Processed payload: " . json_encode($payload);
});
echo $result;
// Terminate the application (this will trigger the 'termination.complete' event).
$app->terminate();
In this example, custom listeners are attached to various system events. As the application boots, processes middleware, and eventually terminates, these listeners output messages and log details, helping you monitor the inner workings of your application in real time.
Extensibility
- Custom Logging: By subscribing to events like booting.error or middleware.error, you can set up custom logging or alerting mechanisms.
- Performance Monitoring: Use events such as middleware.pipeline.start and middleware.pipeline.end to measure processing times and optimize performance.
- Dynamic Behavior: Hook into termination or configuration events to trigger cleanup tasks or reconfigure services on the fly.
This system of internally generated events offers a powerful way to observe and modify the application's behavior without altering its core code. Enjoy leveraging these events to build a more responsive and maintainable application!