master

laravel/framework

Last updated at: 29/12/2023 09:24

AggregateServiceProvider.php

TLDR

This file defines the AggregateServiceProvider class, which is a service provider that aggregates multiple service providers.

Methods

There are two methods in this file:

register

This method registers the service provider instances. It initializes an empty array of instances and then loops through the $providers property, which contains the class names of the providers to be registered. For each provider, it calls the register method on the application instance ($this->app->register($provider)) and adds the returned instance to the instances array.

provides

This method returns an array of services provided by the aggregated service providers. It initializes an empty array ($provides) and then loops through the $providers property. For each provider, it resolves the provider instance using the application ($this->app->resolveProvider($provider)) and merges the result of the provides method called on the resolved instance with the $provides array. Finally, it returns the $provides array.

<?php

namespace Illuminate\Support;

class AggregateServiceProvider extends ServiceProvider
{
    /**
     * The provider class names.
     *
     * @var array
     */
    protected $providers = [];

    /**
     * An array of the service provider instances.
     *
     * @var array
     */
    protected $instances = [];

    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        $this->instances = [];

        foreach ($this->providers as $provider) {
            $this->instances[] = $this->app->register($provider);
        }
    }

    /**
     * Get the services provided by the provider.
     *
     * @return array
     */
    public function provides()
    {
        $provides = [];

        foreach ($this->providers as $provider) {
            $instance = $this->app->resolveProvider($provider);

            $provides = array_merge($provides, $instance->provides());
        }

        return $provides;
    }
}