VOOZH about

URL: https://deepwiki.com/hypervel/support/3.3-service-provider-system

⇱ Service Provider System | hypervel/support | DeepWiki


Loading...
Menu

Service Provider System

The Service Provider System provides an extension mechanism for registering services, loading resources, and publishing assets in the Hypervel framework. Service providers are the central place for bootstrapping package functionality and integrating with the application container.

This document covers the ServiceProvider abstract class and the DefaultProviders collection. For information about the container and dependency injection, see Architecture Overview. For manager-based services, see Manager Pattern.

ServiceProvider Abstract Class

The ServiceProvider class at src/ServiceProvider.php17 is the base class for all service providers in the framework. It provides methods for container registration, resource loading, asset publishing, and lifecycle management.

Class Structure


Sources: src/ServiceProvider.php17-339

Constructor and Application Dependency

The service provider constructor receives an ApplicationContract instance, which is the application container. This allows providers to access and register services during their lifecycle.


The $app property is used throughout the provider's methods to interact with the container, resolve services, and register bindings.

Sources: src/ServiceProvider.php46-49

Service Provider Lifecycle

Service providers follow a three-phase lifecycle that allows for proper initialization order and dependency management.


Sources: src/ServiceProvider.php54-92

Registration Phase

The register() method is called first and should only bind services into the container. At this point, other services may not yet be available.


This method is intentionally empty in the base class and must be implemented by concrete providers.

Sources: src/ServiceProvider.php54-56

Booting Phase

Providers can register callbacks to run before the main boot logic using the booting() method. These callbacks receive the application instance.


The framework calls callBootingCallbacks() to execute all registered pre-boot callbacks.

Sources: src/ServiceProvider.php61-82

Booted Phase

After the boot phase completes, providers can register callbacks to run using the booted() method. This is useful for logic that depends on all providers being fully booted.


The framework calls callBootedCallbacks() to execute all registered post-boot callbacks.

Sources: src/ServiceProvider.php69-92

Configuration Management

Service providers can merge configuration files into the application's existing configuration using mergeConfigFrom().


The method loads a configuration file and merges it with existing configuration, giving precedence to existing values:


This ensures that user configuration overrides package defaults.

Sources: src/ServiceProvider.php97-104

Resource Loading

Service providers provide methods for loading various types of resources. All loading methods use deferred resolution to handle services that may not be available during registration.

Loading Routes

The loadRoutesFrom() method registers route files with the RouteFileCollector:


Sources: src/ServiceProvider.php109-113

Loading Views

The loadViewsFrom() method registers a view namespace with the view factory. It supports both package views and published application views:


Published views in view_path/vendor/{namespace} take precedence over package views.

Sources: src/ServiceProvider.php118-130

Loading View Components

Blade components can be registered with a custom prefix using loadViewComponentsAs():


This allows components to be used in templates with the specified prefix, e.g., <x-prefix::component-name />.

Sources: src/ServiceProvider.php135-142

Loading Translations

Translation namespaces are registered using loadTranslationsFrom():


JSON translation files can be loaded separately with loadJsonTranslationsFrom():


Sources: src/ServiceProvider.php147-162

Loading Migrations

Database migrations are registered using loadMigrationsFrom():


This method accepts either a single path string or an array of paths.

Sources: src/ServiceProvider.php167-174

Deferred Resolution with callAfterResolving

The callAfterResolving() helper registers a callback to run after a service is resolved, or immediately if it has already been resolved:


This ensures resource loading works correctly regardless of when the provider is registered relative to when services are resolved.

Sources: src/ServiceProvider.php179-186

Publishing System

The publishing system allows packages to publish their assets (configuration, views, migrations) to the application's file system where they can be customized by users.


Sources: src/ServiceProvider.php206-290

Registering Publishable Paths

The publishes() method registers paths that can be published by the vendor:publish command:


The $paths array maps source paths to destination paths:


Sources: src/ServiceProvider.php206-215

Publishing Migrations

Migrations have a dedicated method publishesMigrations() that registers them both as publishable paths and tracks them separately:


This allows migrations to be identified separately from other publishable assets.

Sources: src/ServiceProvider.php191-201

Publishing Groups

Assets can be organized into groups (tags) for selective publishing. Groups are stored in the static $publishGroups array:

Group ExamplesTypical Contents
configConfiguration files
migrationsDatabase migrations
viewsView templates
assetsCSS, JavaScript, images
translationsLanguage files

When publishing, users can specify a provider, group, or both to filter which assets are published.

Sources: src/ServiceProvider.php230-240

Retrieving Publishable Paths

The static method pathsToPublish() retrieves publishable paths with optional filtering:


The method supports four scenarios:

  1. No filter - Returns all publishable paths from all providers
  2. Provider filter - Returns paths registered by a specific provider class
  3. Group filter - Returns paths tagged with a specific group
  4. Provider and group - Returns the intersection of provider and group paths

Sources: src/ServiceProvider.php245-290

Publishing Query Methods

Three static methods allow querying the publishing registry:


These methods are used by the vendor:publish command to display available options.

Sources: src/ServiceProvider.php295-314

Command Registration

Service providers can register Artisan commands using the commands() method:


This method typically appears in the boot() method of a provider:


Sources: src/ServiceProvider.php319-322

DefaultProviders Collection

The DefaultProviders class at src/DefaultProviders.php7 manages a collection of service provider class names. It provides a fluent interface for manipulating the list of providers that will be registered with the application.


Sources: src/DefaultProviders.php7-69

Default Provider List

The constructor initializes with a default set of providers if none are provided:


Sources: src/DefaultProviders.php17-23

Merging Providers

The merge() method adds additional providers to the collection:


Example usage:


Sources: src/DefaultProviders.php28-33

Replacing Providers

The replace() method swaps one provider for another in the collection:


Example usage:


Sources: src/DefaultProviders.php38-49

Excluding Providers

The except() method removes providers from the collection:


Example usage:


Sources: src/DefaultProviders.php54-60

Converting to Array

The toArray() method returns the final provider list:


This is typically used by the application bootstrap process to get the list of providers to register.

Sources: src/DefaultProviders.php65-68

Creating Custom Service Providers

To create a custom service provider, extend the ServiceProvider abstract class and implement the register() and optionally the boot() method:

Basic Provider Structure


Provider Registration Pattern


Common Patterns

Pattern 1: Configuration with Publishing


Pattern 2: Resource Loading with Publishing


Pattern 3: Conditional Registration


Pattern 4: Deferred Callbacks


Sources: src/ServiceProvider.php17-339

Static Provider Configuration

Service providers can optionally implement getProviderConfig() to provide configuration for the Hyperf framework:


This method returns an array of configuration specific to how the provider integrates with Hyperf's service container and lifecycle management. It is typically left empty unless the provider needs to customize container behavior.

Sources: src/ServiceProvider.php335-338