Skip to main content

Application Lifecycle

Overview

This section explores how DomainFlow Core manages its startup and shutdown phases. You’ll learn how to:

  • Register callbacks to run before and after the application boots.
  • Determine if the application has finished booting.
  • Gracefully terminate the application, performing any final cleanup.

These features are all encapsulated within the Application class, which coordinates bootstrapping and termination behind the scenes.


Key Concepts

  • Booting Callbacks: Functions that run before the app is fully started.
  • Booted Callbacks: Functions that run after the app finishes starting.
  • Deferred Service Resolution: Optionally resolve any deferred services once you are ready.
  • Termination Callbacks: Functions to clean up resources just before the app shuts down.

Method Descriptions

booting($callback)

Signature

public function booting(callable $callback): void

Purpose
Registers a callback that executes before the application completes its boot process.

ParameterTypeRequired?Description
$callbackcallableYesA function that receives the Application instance as an argument.
  • These callbacks typically handle preliminary setup tasks (e.g., logging configuration).

Usage Example

$app->booting(function ($app) {
// Perform early setup
});

booted($callback)

Signature

public function booted(callable $callback): void

Purpose
Registers a callback that executes after the application has booted successfully.

ParameterTypeRequired?Description
$callbackcallableYesA function that receives the Application instance as an argument.
  • Useful for tasks that should only run once all providers and services are fully initialized.

Usage Example

$app->booted(function ($app) {
// Perform tasks that depend on fully loaded services
});

boot()

Signature

public function boot(): void

Purpose
Starts the application’s boot process. Runs:

  1. All registered “booting” callbacks.
  2. Service provider registration and boot methods.
  3. All registered “booted” callbacks.

If caching is enabled and a cache file is found, boot() may load services from the cache instead of running the usual steps.

ParametersTypeRequired?Description
NoneN/AN/AN/A

Usage Example

$app->boot(); // triggers booting callbacks, providers, then booted callbacks

isBooted()

Signature

public function isBooted(): bool

Purpose
Checks whether the boot process has already completed.

ParametersTypeRequired?Description
NoneN/AN/AN/A
  • Returns true if the application has finished booting, or false otherwise.

Usage Example

if ($app->isBooted()) {
// The app is fully initialized, so we can safely do post-boot logic
}

resolveDeferredServices()

Signature

public function resolveDeferredServices(): void

Purpose
Forces the resolution of any deferred services that have not yet been loaded. This can be useful if you want all services to be ready before performing certain tasks.

ParametersTypeRequired?Description
NoneN/AN/AN/A
  • Typically called after the app is booted if you don’t want to wait for on-demand resolution of deferred services.

Usage Example

$app->boot();
$app->resolveDeferredServices();
// Now we know all deferred providers are fully registered.

registerTerminationCallback($callback)

Signature

public function registerTerminationCallback(callable $callback): void

Purpose
Registers a function that will run during application termination. Ideal for cleanup tasks or final logging actions.

ParameterTypeRequired?Description
$callbackcallableYesA function that receives the Application instance as an argument.

Usage Example

$app->registerTerminationCallback(function ($app) {
// e.g., close database connections
});

terminate()

Signature

public function terminate(): void

Purpose
Executes all registered termination callbacks, then fires an event indicating the application has fully terminated.

ParametersTypeRequired?Description
NoneN/AN/AN/A
  • If any callback throws an exception, it’s caught, and a termination error event is fired.

Usage Example

$app->terminate();
// All termination callbacks have now been executed

Comparative Descriptions

  • booting() vs. booted()
  • booting() handles pre-boot tasks.
  • booted() runs post-boot tasks—only once the application is fully ready.
  • registerTerminationCallback() vs. terminate()
  • Register your cleanup logic first with registerTerminationCallback().
  • terminate() calls those callbacks in sequence, ensuring a clean shutdown flow.
  • resolveDeferredServices()
  • Unique to the boot lifecycle; it lets you preemptively load all deferred services rather than waiting for lazy loading.

Practical Examples

$app = new Application();

// Set up an early callback before boot
$app->booting(function ($app) {
// Possibly load environment variables
});

// Perform logic after everything is ready
$app->booted(function ($app) {
// For instance, initialize logging with all dependencies
});

// Boot the application
$app->boot(); // triggers booting, providers, then booted

if (!$app->isBooted()) {
// This condition won't happen unless there's an error
}

// If you want all deferred services resolved upfront:
$app->resolveDeferredServices();

// Register a shutdown handler
$app->registerTerminationCallback(function ($app) {
// e.g., flush caches, close open streams, etc.
});

// Terminate the app
$app->terminate();

Extensibility

  • Custom Boot Phases: You can nest your own logic in “booting” or “booted” callbacks, or even wrap them in your own service providers.
  • Conditional Termination: If your application runs with a long-lived process, you might decide to only invoke terminate() when a specific event or signal occurs.
  • Deferred Services: Fine-tune the moment you choose to load certain costly or optional services, balancing performance and resource usage.

By weaving together boot, termination, and deferred resolution, you gain precise control over your application’s lifecycle—from the earliest startup tasks to the final cleanup.