VOOZH about

URL: https://deepwiki.com/hypervel/foundation/2.2-service-providers-and-application-lifecycle

⇱ Service Providers and Application Lifecycle | hypervel/foundation | DeepWiki


Loading...
Last indexed: 7 February 2026 (101eff)
Menu

Service Providers and Application Lifecycle

Purpose and Scope

This document covers the service provider system and application lifecycle management in Hypervel Foundation. It explains how service providers are registered and booted, the two-phase initialization process, bootstrapping mechanics, and lifecycle callbacks. This document focuses specifically on the provider pattern and application boot sequence.

For information about the Application container's dependency injection capabilities, see Application Container and Dependency Injection. For configuration system integration and hot reloading, see Environment Management and Hot Reloading. For details on the FoundationServiceProvider's configuration overrides, see Foundation Service Provider.


Service Provider System Overview

Service providers are the primary mechanism for registering and configuring framework services. The Application class manages a registry of providers and orchestrates their initialization through a two-phase process: registration followed by booting.

Core Components

ComponentLocationPurpose
Applicationsrc/Application.php25Manages provider registry and orchestrates lifecycle
ServiceProviderHypervel\Support\ServiceProviderBase class for all providers
FoundationServiceProvidersrc/Providers/FoundationServiceProvider.php33Framework's core service provider
Provider registrysrc/Application.php70-75Tracks registered and loaded providers

Sources: src/Application.php25-90 src/Contracts/Application.php1-207


Service Provider Lifecycle

The service provider lifecycle follows a strict two-phase initialization pattern that ensures all services are registered before any boot logic executes.


Sources: src/Application.php309-430


Registration Phase

Provider Registration Process

The register() method handles service provider registration with the following logic:


Key Methods:

The registration phase stores providers in two internal arrays:

ArrayPurposeLocation
$serviceProvidersMaps class name → provider instancesrc/Application.php70
$loadedProvidersMaps class name → true (tracking only)src/Application.php75

Sources: src/Application.php309-385

Automatic Binding Registration

Service providers can declare a $bindings property that will be automatically registered in the container:


The Application processes these bindings automatically at src/Application.php329-333

Sources: src/Application.php329-333


Booting Phase

Application Boot Sequence

The boot() method orchestrates the booting of all registered providers:


Key Methods:

Sources: src/Application.php398-466

Late Provider Registration

If a provider is registered after the application has already booted, it is immediately booted:


This ensures that late-registered providers follow the same lifecycle as early-registered ones, located at src/Application.php340-342

Sources: src/Application.php340-342


Application Bootstrapping

Bootstrap Process

The bootstrapWith() method executes bootstrapper classes in a specific order:


Bootstrapping Methods:

MethodPurposeLocation
bootstrapWith()Execute bootstrap classessrc/Application.php118-129
hasBeenBootstrapped()Check bootstrap statussrc/Application.php150-153
beforeBootstrapping()Register pre-bootstrap callbacksrc/Application.php134-137
afterBootstrapping()Register post-bootstrap callbacksrc/Application.php142-145

The bootstrap process is idempotent - the $hasBeenBootstrapped flag prevents duplicate execution.

Sources: src/Application.php118-153


FoundationServiceProvider

The FoundationServiceProvider is the framework's core service provider that sets up essential services and configurations.

Registration Phase Operations


Key Registration Operations:

  1. Configuration Overrides - src/Providers/FoundationServiceProvider.php108-128

    • Copies values from config/app.php and config/database.php to Hyperf-expected locations
    • Merges Redis configurations with global options
    • Sorts middleware stacks using MiddlewareManager::sortMiddlewares()
  2. Exception Listening - src/Providers/FoundationServiceProvider.php77-99

    • Listens for FailToHandle events from Hyperf's command system
    • Routes exceptions to ConsoleKernel for proper rendering
  3. VarDumper Registration - src/Providers/FoundationServiceProvider.php181-201

    • Registers custom casters for framework classes
    • Sets up HtmlDumper or CliDumper based on environment

Sources: src/Providers/FoundationServiceProvider.php59-201

Boot Phase Operations


Boot Operations:

  1. Timezone Configuration - src/Providers/FoundationServiceProvider.php171-174

    • Sets PHP's default timezone from config('app.timezone')
  2. Encoding Configuration - src/Providers/FoundationServiceProvider.php176-179

    • Sets PHP's internal encoding to UTF-8
  3. Database Connection - src/Providers/FoundationServiceProvider.php101-106

    • Sets default database connection from configuration

Sources: src/Providers/FoundationServiceProvider.php49-179


Lifecycle Callbacks and Hooks

Application-Level Callbacks

The Application provides hooks for executing code at specific lifecycle points:

Callback TypeRegistration MethodExecution PointStorage Location
Bootingbooting(callable)Before provider boot loopsrc/Application.php56
Bootedbooted(callable)After provider boot loopsrc/Application.php63

Key Characteristics:

Sources: src/Application.php407-415 src/Application.php435-466

Bootstrap Event Hooks

The bootstrap process emits events that can be listened to:


Hook Registration Methods:

These hooks allow external code to inject logic before or after specific bootstrappers execute.

Sources: src/Application.php123-127 src/Application.php134-145


Provider Registry and Queries

Querying Registered Providers

The Application provides methods to inspect the provider registry:

MethodReturn TypePurposeLocation
getProvider()?ServiceProviderGet single provider instancesrc/Application.php350-355
getProviders()arrayGet all instances of a provider classsrc/Application.php360-365
getLoadedProviders()array<string, bool>Get map of loaded provider namessrc/Application.php488-491
providerIsLoaded()boolCheck if provider is loadedsrc/Application.php496-499

Example Usage Patterns:


Sources: src/Application.php350-365 src/Application.php488-499


Enhanced VarDumper Integration

The FoundationServiceProvider registers custom dumpers that resolve the source location of dump calls.

Dumper Registration Flow


Dumper Classes:

Both dumpers use the ResolvesDumpSource trait to automatically detect and display the source file and line number of dump calls.

Sources: src/Providers/FoundationServiceProvider.php181-201 src/Concerns/ResolvesDumpSource.php9-186

Source Resolution Mechanism


The source resolution handles compiled Blade views by extracting the original view path from the /**PATH ... ENDPATH*/ comment embedded in compiled files at src/Concerns/ResolvesDumpSource.php126-135

Sources: src/Concerns/ResolvesDumpSource.php56-167


Complete Initialization Timeline

The following diagram shows the complete application initialization sequence from construction through provider booting:


Initialization Steps:

  1. Construction Phase - src/Application.php82-90

    • Base path set
    • Container initialized
    • Core bindings and aliases registered
  2. Bootstrap Phase - src/Application.php118-129

    • Bootstrappers executed in order
    • Each bootstrapper receives $app instance
  3. Registration Phase - During RegisterProviders bootstrapper

    • Service providers call register() methods
    • Container bindings established
  4. Boot Phase - During BootProviders bootstrapper

    • Service providers call boot() methods
    • Services fully initialized

Sources: src/Application.php82-129 src/Application.php398-416