master

laravel/framework

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

CreatesUserProviders.php

TLDR

This file contains the CreatesUserProviders trait, which provides methods for creating user providers in the Illuminate\Auth namespace. It includes methods for creating the user provider implementation based on the driver, retrieving user provider configuration, and creating database and Eloquent user providers.

Methods

createUserProvider($provider = null)

This method is responsible for creating the user provider implementation based on the driver. It takes an optional $provider parameter and returns an instance of the Illuminate\Contracts\Auth\UserProvider class or null. If the user provider configuration is defined and a custom provider creator is registered for the driver, it calls the custom provider creator and passes the Laravel application and configuration array. If no custom provider creator is registered, it creates the appropriate user provider (database or Eloquent) based on the driver.

getProviderConfiguration($provider)

This method retrieves the configuration array for the specified user provider. It takes an optional $provider parameter and returns an array or null. It uses Laravel's application container ($this->app) to retrieve the configuration from the auth.providers array.

createDatabaseProvider($config)

This method creates an instance of the Illuminate\Auth\DatabaseUserProvider class. It takes the $config array as a parameter and returns an instance of the DatabaseUserProvider class. It uses the Laravel application container ($this->app) to get the database connection ($this->app['db']) and the hash service ($this->app['hash']) to create the database user provider.

createEloquentProvider($config)

This method creates an instance of the Illuminate\Auth\EloquentUserProvider class. It takes the $config array as a parameter and returns an instance of the EloquentUserProvider class. It uses the Laravel application container ($this->app) to get the hash service ($this->app['hash']) to create the Eloquent user provider.

getDefaultUserProvider()

This method retrieves the default user provider name from the Laravel application configuration. It returns a string representing the default user provider name.

<?php

namespace Illuminate\Auth;

use InvalidArgumentException;

trait CreatesUserProviders
{
    /**
     * The registered custom provider creators.
     *
     * @var array
     */
    protected $customProviderCreators = [];

    /**
     * Create the user provider implementation for the driver.
     *
     * @param  string|null  $provider
     * @return \Illuminate\Contracts\Auth\UserProvider|null
     *
     * @throws \InvalidArgumentException
     */
    public function createUserProvider($provider = null)
    {
        if (is_null($config = $this->getProviderConfiguration($provider))) {
            return;
        }

        if (isset($this->customProviderCreators[$driver = ($config['driver'] ?? null)])) {
            return call_user_func(
                $this->customProviderCreators[$driver], $this->app, $config
            );
        }

        return match ($driver) {
            'database' => $this->createDatabaseProvider($config),
            'eloquent' => $this->createEloquentProvider($config),
            default => throw new InvalidArgumentException(
                "Authentication user provider [{$driver}] is not defined."
            ),
        };
    }

    /**
     * Get the user provider configuration.
     *
     * @param  string|null  $provider
     * @return array|null
     */
    protected function getProviderConfiguration($provider)
    {
        if ($provider = $provider ?: $this->getDefaultUserProvider()) {
            return $this->app['config']['auth.providers.'.$provider];
        }
    }

    /**
     * Create an instance of the database user provider.
     *
     * @param  array  $config
     * @return \Illuminate\Auth\DatabaseUserProvider
     */
    protected function createDatabaseProvider($config)
    {
        $connection = $this->app['db']->connection($config['connection'] ?? null);

        return new DatabaseUserProvider($connection, $this->app['hash'], $config['table']);
    }

    /**
     * Create an instance of the Eloquent user provider.
     *
     * @param  array  $config
     * @return \Illuminate\Auth\EloquentUserProvider
     */
    protected function createEloquentProvider($config)
    {
        return new EloquentUserProvider($this->app['hash'], $config['model']);
    }

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