class Session

Same name in other branches
  1. 9 core/lib/Drupal/Core/StackMiddleware/Session.php \Drupal\Core\StackMiddleware\Session
  2. 8.9.x core/lib/Drupal/Core/StackMiddleware/Session.php \Drupal\Core\StackMiddleware\Session
  3. 11.x core/lib/Drupal/Core/StackMiddleware/Session.php \Drupal\Core\StackMiddleware\Session

Wrap session logic around a HTTP request.

Note, the session service is wrapped in a closure in order to prevent premature initialization of session storage (database).

Hierarchy

Expanded class hierarchy of Session

44 string references to 'Session'
AccessTest::testFileCacheability in core/modules/file/tests/src/Kernel/AccessTest.php
Tests cacheability metadata.
authorize.php in core/authorize.php
Administrative script for running authorized file operations.
BigPipeTest::assertSessionCookieExists in core/modules/big_pipe/tests/src/Functional/BigPipeTest.php
Asserts whether a session cookie exists or not.
core.services.yml in core/core.services.yml
core/core.services.yml
core.services.yml in core/core.services.yml
core/core.services.yml

... See full list

1 service uses Session
http_middleware.session in core/core.services.yml
Drupal\Core\StackMiddleware\Session

File

core/lib/Drupal/Core/StackMiddleware/Session.php, line 17

Namespace

Drupal\Core\StackMiddleware
View source
class Session implements HttpKernelInterface {
    
    /**
     * The wrapped HTTP kernel.
     *
     * @var \Symfony\Component\HttpKernel\HttpKernelInterface
     */
    protected $httpKernel;
    
    /**
     * Constructs a Session stack middleware object.
     *
     * @param \Symfony\Component\HttpKernel\HttpKernelInterface $http_kernel
     *   The decorated kernel.
     * @param \Closure $sessionClosure
     *   A closure that wraps the session service.
     */
    public function __construct(HttpKernelInterface $http_kernel, \Closure $sessionClosure) {
        $this->httpKernel = $http_kernel;
    }
    
    /**
     * {@inheritdoc}
     */
    public function handle(Request $request, $type = self::MAIN_REQUEST, $catch = TRUE) : Response {
        // Initialize and start a session for web requests. Command line tools and
        // the parent site in functional tests must continue to use the ephemeral
        // session initialized and started in DrupalKernel::preHandle().
        if ($type === self::MAIN_REQUEST && PHP_SAPI !== 'cli') {
            $this->initializePersistentSession($request);
        }
        $result = $this->httpKernel
            ->handle($request, $type, $catch);
        if ($type === self::MAIN_REQUEST && !$result instanceof ResponseKeepSessionOpenInterface && PHP_SAPI !== 'cli') {
            $request->getSession()
                ->save();
        }
        return $result;
    }
    
    /**
     * Initializes a session backed by persistent store and puts it on the request.
     *
     * Sessions for web requests need to be backed by a persistent session store
     * and a real session handler (responsible for session cookie management).
     * In contrast, a simple in-memory store is sufficient for command line tools
     * and tests. Hence, the persistent session should only ever be placed on web
     * requests while command line tools and the parent site in functional tests
     * must continue to use the ephemeral session initialized in
     * DrupalKernel::preHandle().
     *
     * @param \Symfony\Component\HttpFoundation\Request $request
     *   The request.
     *
     * @see \Drupal\Core\DrupalKernel::preHandle()
     */
    protected function initializePersistentSession(Request $request) : void {
        
        /** @var \Symfony\Component\HttpFoundation\Session\SessionInterface $session */
        $session = ($this->sessionClosure)();
        $session->start();
        $request->setSession($session);
    }

}

Members

Title Sort descending Modifiers Object type Summary
Session::$httpKernel protected property The wrapped HTTP kernel.
Session::handle public function
Session::initializePersistentSession protected function Initializes a session backed by persistent store and puts it on the request.
Session::__construct public function Constructs a Session stack middleware object.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.