master

laravel/framework

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

Manager.php

TLDR

The Manager.php file in the Illuminate\Support namespace contains an abstract class called Manager. This class provides a basic structure for managing different "drivers". It includes methods for creating and accessing drivers, as well as registering custom driver creators.

Methods

__constructor

The constructor method accepts a Container instance as a parameter and assigns it to the $container property. It also initializes the $config property by calling the make method of the $container to retrieve the 'config' binding.

getDefaultDriver

This is an abstract method that needs to be implemented by any concrete classes extending Manager. It returns the name of the default driver.

driver

This method accepts an optional driver name as a parameter and returns an instance of the specified driver. If no driver name is provided, it retrieves the default driver name using the getDefaultDriver method. If a driver instance for the specified name does not already exist, it calls the createDriver method to create and cache the instance.

createDriver

This protected method creates a new driver instance based on the specified driver name. It first checks if a custom driver creator exists for the driver name. If it does, it calls the custom creator and returns the result. If no custom creator is found, it constructs a method name based on the driver name and calls that method on the Manager instance. If the method exists, it returns the result. Otherwise, it throws an InvalidArgumentException with a message indicating that the driver is not supported.

callCustomCreator

This protected method calls a custom driver creator for the specified driver name. It retrieves the custom creator closure from the $customCreators array and passes the container instance to it. The result of the closure is returned.

extend

This method registers a custom driver creator closure for the specified driver name. It stores the closure in the $customCreators array with the driver name as the key. The method returns the Manager instance.

getDrivers

This method returns an array containing all of the created driver instances.

getContainer

This method returns the Container instance used by the Manager.

setContainer

This method sets the Container instance used by the Manager to the specified instance and returns the Manager instance.

forgetDrivers

This method clears the $drivers array, which holds the created driver instances, and returns the Manager instance.

__call

This magic method is used to dynamically call methods on the default driver instance. It internally calls the driver method without any parameters to retrieve the default driver instance, and then calls the specified method on that instance with the provided parameters. The result is returned.

Classes

There are no classes in this file.

<?php

namespace Illuminate\Support;

use Closure;
use Illuminate\Contracts\Container\Container;
use InvalidArgumentException;

abstract class Manager
{
    /**
     * The container instance.
     *
     * @var \Illuminate\Contracts\Container\Container
     */
    protected $container;

    /**
     * The configuration repository instance.
     *
     * @var \Illuminate\Contracts\Config\Repository
     */
    protected $config;

    /**
     * The registered custom driver creators.
     *
     * @var array
     */
    protected $customCreators = [];

    /**
     * The array of created "drivers".
     *
     * @var array
     */
    protected $drivers = [];

    /**
     * Create a new manager instance.
     *
     * @param  \Illuminate\Contracts\Container\Container  $container
     * @return void
     */
    public function __construct(Container $container)
    {
        $this->container = $container;
        $this->config = $container->make('config');
    }

    /**
     * Get the default driver name.
     *
     * @return string
     */
    abstract public function getDefaultDriver();

    /**
     * Get a driver instance.
     *
     * @param  string|null  $driver
     * @return mixed
     *
     * @throws \InvalidArgumentException
     */
    public function driver($driver = null)
    {
        $driver = $driver ?: $this->getDefaultDriver();

        if (is_null($driver)) {
            throw new InvalidArgumentException(sprintf(
                'Unable to resolve NULL driver for [%s].', static::class
            ));
        }

        // If the given driver has not been created before, we will create the instances
        // here and cache it so we can return it next time very quickly. If there is
        // already a driver created by this name, we'll just return that instance.
        if (! isset($this->drivers[$driver])) {
            $this->drivers[$driver] = $this->createDriver($driver);
        }

        return $this->drivers[$driver];
    }

    /**
     * Create a new driver instance.
     *
     * @param  string  $driver
     * @return mixed
     *
     * @throws \InvalidArgumentException
     */
    protected function createDriver($driver)
    {
        // First, we will determine if a custom driver creator exists for the given driver and
        // if it does not we will check for a creator method for the driver. Custom creator
        // callbacks allow developers to build their own "drivers" easily using Closures.
        if (isset($this->customCreators[$driver])) {
            return $this->callCustomCreator($driver);
        }

        $method = 'create'.Str::studly($driver).'Driver';

        if (method_exists($this, $method)) {
            return $this->$method();
        }

        throw new InvalidArgumentException("Driver [$driver] not supported.");
    }

    /**
     * Call a custom driver creator.
     *
     * @param  string  $driver
     * @return mixed
     */
    protected function callCustomCreator($driver)
    {
        return $this->customCreators[$driver]($this->container);
    }

    /**
     * Register a custom driver creator Closure.
     *
     * @param  string  $driver
     * @param  \Closure  $callback
     * @return $this
     */
    public function extend($driver, Closure $callback)
    {
        $this->customCreators[$driver] = $callback;

        return $this;
    }

    /**
     * Get all of the created "drivers".
     *
     * @return array
     */
    public function getDrivers()
    {
        return $this->drivers;
    }

    /**
     * Get the container instance used by the manager.
     *
     * @return \Illuminate\Contracts\Container\Container
     */
    public function getContainer()
    {
        return $this->container;
    }

    /**
     * Set the container instance used by the manager.
     *
     * @param  \Illuminate\Contracts\Container\Container  $container
     * @return $this
     */
    public function setContainer(Container $container)
    {
        $this->container = $container;

        return $this;
    }

    /**
     * Forget all of the resolved driver instances.
     *
     * @return $this
     */
    public function forgetDrivers()
    {
        $this->drivers = [];

        return $this;
    }

    /**
     * Dynamically call the default driver instance.
     *
     * @param  string  $method
     * @param  array  $parameters
     * @return mixed
     */
    public function __call($method, $parameters)
    {
        return $this->driver()->$method(...$parameters);
    }
}