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 eager providers have booted but immediately before the internal boot flag is set.
  • Deferred Service Resolution: Optionally pre-warm any deferred services once you are ready — see loadDeferredProviders() on the Service Providers page.
  • 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 eager providers have booted and immediately before the application marks itself as booted. isBooted() is still false inside the callback.

ParameterTypeRequired?Description
$callbackcallableYesA function that receives the Application instance as an argument.
  • Useful for tasks that should run after eager providers complete their boot phase. Deferred providers may still be unloaded.

Usage Example

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

boot()

Signature

public function boot(): void

Purpose
Starts the application’s boot process. Runs, in order:

  1. All registered “booting” callbacks.
  2. Attribute-based registrations (#[Service], #[EventListener]).
  3. Default internal service providers (currently EventDispatcherServiceProvider, which binds EventDispatcherInterface in the container).
  4. Registration and boot of every registered service provider, in the order resolved by declarative provider ordering (see Declarative Provider Ordering). Deferred providers are not registered or booted at this stage — see Service Providers.
  5. All registered “booted” callbacks.
Calling boot() more than once

boot() only runs the sequence above the first time it is called on a given Application instance. A repeat call on an already-booted application performs no boot work — it fires booting.repeat_call_ignored instead of booting.init, so a system-event audit trail never implies a real boot cycle ran twice. See System Events.

ParametersTypeRequired?Description
NoneN/AN/AN/A

Usage Example

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

$app->boot(); // no-op: already booted, fires 'booting.repeat_call_ignored'

isBooted()

Signature

public function isBooted(): bool

Purpose
Checks whether the boot process has already completed.

ParametersTypeRequired?Description
NoneN/AN/AN/A
  • Returns true after all booted callbacks have completed and the application has set its internal boot flag. Deferred providers may still be unloaded.

Usage Example

if ($app->isBooted()) {
// Eager providers and booted callbacks have completed.
}

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 termination callbacks sequentially. The first callback failure fires termination.error and throws; later callbacks are skipped and termination.complete is not emitted. The complete event therefore means every callback succeeded.

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

Usage Example

$app->terminate();
// Reached only when every termination callback completed successfully.

Comparative Descriptions

  • booting() vs. booted()
  • booting() handles pre-boot tasks.
  • booted() runs after eager providers have booted but before isBooted() becomes true; deferred providers may still be unloaded.
  • registerTerminationCallback() vs. terminate()
  • Register your cleanup logic first with registerTerminationCallback().
  • terminate() calls those callbacks in sequence and stops on the first failure; termination.complete is emitted only after all callbacks succeed.
  • loadDeferredProviders()
  • Documented on the Service Providers page; it lets you preemptively load all still-deferred services rather than waiting for on-demand resolution.

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 still-deferred providers resolved upfront:
$app->loadDeferredProviders();

// 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.