master

laravel/framework

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

RedisServiceProvider.php

TLDR

The RedisServiceProvider.php file is a part of the Illuminate\Redis namespace and it extends the ServiceProvider class. It registers the Redis service provider and provides the Redis and Redis connection services.

Methods

register

The register method registers the Redis service provider. It creates a singleton instance of the RedisManager class and binds it to the 'redis' key on the application container. It also binds the 'redis.connection' key to a closure that returns the connection from the Redis service provider.

provides

The provides method returns an array of services provided by the Redis service provider, namely 'redis' and 'redis.connection'.

<?php

namespace Illuminate\Redis;

use Illuminate\Contracts\Support\DeferrableProvider;
use Illuminate\Support\Arr;
use Illuminate\Support\ServiceProvider;

class RedisServiceProvider extends ServiceProvider implements DeferrableProvider
{
    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        $this->app->singleton('redis', function ($app) {
            $config = $app->make('config')->get('database.redis', []);

            return new RedisManager($app, Arr::pull($config, 'client', 'phpredis'), $config);
        });

        $this->app->bind('redis.connection', function ($app) {
            return $app['redis']->connection();
        });
    }

    /**
     * Get the services provided by the provider.
     *
     * @return array
     */
    public function provides()
    {
        return ['redis', 'redis.connection'];
    }
}