Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Session.php

class Session

Same name in this branch
  1. 11.1.x vendor/lullabot/php-webdriver/lib/WebDriver/Session.php \WebDriver\Session
  2. 11.1.x vendor/lullabot/php-webdriver/lib/WebDriver/Storage/Session.php \WebDriver\Storage\Session
  3. 11.1.x vendor/behat/mink/src/Session.php \Behat\Mink\Session
  4. 11.1.x vendor/symfony/http-foundation/Session/Session.php \Symfony\Component\HttpFoundation\Session\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

  • class \Drupal\Core\StackMiddleware\Session implements \Symfony\Component\HttpKernel\HttpKernelInterface

Expanded class hierarchy of Session

24 string references to 'Session'
authorize.php in core/authorize.php
Administrative script for running authorized file operations.
default.services.yml in sites/default/default.services.yml
sites/default/default.services.yml
default.services.yml in core/assets/scaffold/files/default.services.yml
core/assets/scaffold/files/default.services.yml
DrupalKernel::initializeContainer in core/lib/Drupal/Core/DrupalKernel.php
Initializes the service container.
DrupalKernel::resetContainer in core/lib/Drupal/Core/DrupalKernel.php
Force a container reset.

... See full list

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 Overriden Title
HttpKernelInterface::MAIN_REQUEST public constant
HttpKernelInterface::SUB_REQUEST public constant
Session::$httpKernel protected property The wrapped HTTP kernel.
Session::handle public function Handles a Request to convert it to a Response. Overrides HttpKernelInterface::handle
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.

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal