master

laravel/framework

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

PasswordBrokerManager.php

TLDR

The PasswordBrokerManager.php file in the Illuminate\Auth\Passwords namespace is responsible for managing and creating instances of the password broker, which is used for resetting user passwords.

Methods

broker

This method is used to retrieve an instance of the password broker. It takes an optional parameter name, which specifies the name of the broker to retrieve. If no name is provided, it retrieves the default broker. The method returns an instance of Illuminate\Contracts\Auth\PasswordBroker.

resolve

This method is responsible for resolving the given broker name and creating an instance of the password broker. It takes the name of the broker and retrieves its configuration. If the configuration is not found, it throws an InvalidArgumentException. The method then creates an instance of PasswordBroker by passing in the token repository and user provider. It returns the created instance of Illuminate\Contracts\Auth\PasswordBroker.

createTokenRepository

This method is used to create an instance of the token repository based on the given configuration. It takes an array $config containing the configuration for the token repository. It retrieves the application key from the config and checks if it needs to be decoded from base64. It then creates an instance of DatabaseTokenRepository by passing in the database connection, the hash service, table name, application key, token expiration time, and throttle limit. It returns the created instance of Illuminate\Auth\Passwords\TokenRepositoryInterface.

getConfig

This method is used to retrieve the configuration for the password broker based on the given name. It retrieves the configuration from the application config using the key auth.passwords.{name}. It returns an array containing the configuration, or null if the configuration is not found.

getDefaultDriver

This method is used to retrieve the default password broker name from the application config. It retrieves the default name using the key auth.defaults.passwords. It returns a string representing the default password broker name.

setDefaultDriver

This method is used to set the default password broker name in the application config. It takes a name parameter and sets the default name using the key auth.defaults.passwords.

__call

This magic method allows for dynamically calling methods on the default driver instance. It takes a method parameter and an array of parameters. It calls the specified method on the default broker instance using the broker() method and passes in the parameters. It returns the result of the method call.

Classes

There are no classes defined in this file.

<?php

namespace Illuminate\Auth\Passwords;

use Illuminate\Contracts\Auth\PasswordBrokerFactory as FactoryContract;
use InvalidArgumentException;

/**
 * @mixin \Illuminate\Contracts\Auth\PasswordBroker
 */
class PasswordBrokerManager implements FactoryContract
{
    /**
     * The application instance.
     *
     * @var \Illuminate\Contracts\Foundation\Application
     */
    protected $app;

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

    /**
     * Create a new PasswordBroker manager instance.
     *
     * @param  \Illuminate\Contracts\Foundation\Application  $app
     * @return void
     */
    public function __construct($app)
    {
        $this->app = $app;
    }

    /**
     * Attempt to get the broker from the local cache.
     *
     * @param  string|null  $name
     * @return \Illuminate\Contracts\Auth\PasswordBroker
     */
    public function broker($name = null)
    {
        $name = $name ?: $this->getDefaultDriver();

        return $this->brokers[$name] ?? ($this->brokers[$name] = $this->resolve($name));
    }

    /**
     * Resolve the given broker.
     *
     * @param  string  $name
     * @return \Illuminate\Contracts\Auth\PasswordBroker
     *
     * @throws \InvalidArgumentException
     */
    protected function resolve($name)
    {
        $config = $this->getConfig($name);

        if (is_null($config)) {
            throw new InvalidArgumentException("Password resetter [{$name}] is not defined.");
        }

        // The password broker uses a token repository to validate tokens and send user
        // password e-mails, as well as validating that password reset process as an
        // aggregate service of sorts providing a convenient interface for resets.
        return new PasswordBroker(
            $this->createTokenRepository($config),
            $this->app['auth']->createUserProvider($config['provider'] ?? null)
        );
    }

    /**
     * Create a token repository instance based on the given configuration.
     *
     * @param  array  $config
     * @return \Illuminate\Auth\Passwords\TokenRepositoryInterface
     */
    protected function createTokenRepository(array $config)
    {
        $key = $this->app['config']['app.key'];

        if (str_starts_with($key, 'base64:')) {
            $key = base64_decode(substr($key, 7));
        }

        $connection = $config['connection'] ?? null;

        return new DatabaseTokenRepository(
            $this->app['db']->connection($connection),
            $this->app['hash'],
            $config['table'],
            $key,
            $config['expire'],
            $config['throttle'] ?? 0
        );
    }

    /**
     * Get the password broker configuration.
     *
     * @param  string  $name
     * @return array|null
     */
    protected function getConfig($name)
    {
        return $this->app['config']["auth.passwords.{$name}"];
    }

    /**
     * Get the default password broker name.
     *
     * @return string
     */
    public function getDefaultDriver()
    {
        return $this->app['config']['auth.defaults.passwords'];
    }

    /**
     * Set the default password broker name.
     *
     * @param  string  $name
     * @return void
     */
    public function setDefaultDriver($name)
    {
        $this->app['config']['auth.defaults.passwords'] = $name;
    }

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