master

laravel/framework

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

SessionServiceProvider.php

TLDR

This file defines the SessionServiceProvider class, which is responsible for registering the session manager and session driver in a Laravel application.

Methods

register

This method registers the session manager and the session driver with the application. It also creates a singleton instance of the StartSession middleware.

registerSessionManager

This protected method registers the session manager instance with the application.

registerSessionDriver

This protected method registers the session driver instance with the application. It creates the session manager and resolves the driver on a lazy load basis.

Classes

SessionServiceProvider

This class extends the ServiceProvider class and is responsible for registering the session manager and session driver in a Laravel application.

<?php

namespace Illuminate\Session;

use Illuminate\Contracts\Cache\Factory as CacheFactory;
use Illuminate\Session\Middleware\StartSession;
use Illuminate\Support\ServiceProvider;

class SessionServiceProvider extends ServiceProvider
{
    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        $this->registerSessionManager();

        $this->registerSessionDriver();

        $this->app->singleton(StartSession::class, function ($app) {
            return new StartSession($app->make(SessionManager::class), function () use ($app) {
                return $app->make(CacheFactory::class);
            });
        });
    }

    /**
     * Register the session manager instance.
     *
     * @return void
     */
    protected function registerSessionManager()
    {
        $this->app->singleton('session', function ($app) {
            return new SessionManager($app);
        });
    }

    /**
     * Register the session driver instance.
     *
     * @return void
     */
    protected function registerSessionDriver()
    {
        $this->app->singleton('session.store', function ($app) {
            // First, we will create the session manager which is responsible for the
            // creation of the various session drivers when they are needed by the
            // application instance, and will resolve them on a lazy load basis.
            return $app->make('session')->driver();
        });
    }
}