diff --git a/core/core.services.yml b/core/core.services.yml index 9fe8324..bd14567 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -477,9 +477,10 @@ services: - { name: http_middleware, priority: 100 } http_middleware.session: class: Drupal\Core\StackMiddleware\Session - arguments: ['@session'] tags: - { name: http_middleware, priority: 50 } + calls: + - [setContainer, ['@service_container']] language_manager: class: Drupal\Core\Language\LanguageManager arguments: ['@language.default'] diff --git a/core/lib/Drupal/Core/DrupalKernel.php b/core/lib/Drupal/Core/DrupalKernel.php index bfa2c98..d8b77c6 100644 --- a/core/lib/Drupal/Core/DrupalKernel.php +++ b/core/lib/Drupal/Core/DrupalKernel.php @@ -573,7 +573,9 @@ public function prepareLegacyRequest(Request $request) { $this->preHandle($request); // Setup services which are normally initialized from within stack // middleware or during the request kernel event. - $request->setSession($this->container->get('session')); + if (PHP_SAPI !== 'cli') { + $request->setSession($this->container->get('session')); + } $request->attributes->set(RouteObjectInterface::ROUTE_OBJECT, new Route('')); $request->attributes->set(RouteObjectInterface::ROUTE_NAME, ''); $this->container->get('request_stack')->push($request); diff --git a/core/lib/Drupal/Core/StackMiddleware/Session.php b/core/lib/Drupal/Core/StackMiddleware/Session.php index b45a16e..0fb786a 100644 --- a/core/lib/Drupal/Core/StackMiddleware/Session.php +++ b/core/lib/Drupal/Core/StackMiddleware/Session.php @@ -7,15 +7,22 @@ namespace Drupal\Core\StackMiddleware; +use Symfony\Component\DependencyInjection\ContainerAwareTrait; use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\HttpFoundation\Session\SessionInterface; use Symfony\Component\HttpKernel\HttpKernelInterface; /** * Wrap session logic around a HTTP request. + * + * Note, the session service is not injected into this class in order to prevent + * premature initialization of session storage (database). Instead the session + * is retrieved from the container only immediately before passing an incomming + * request on to the wrapped kernel. */ class Session implements HttpKernelInterface { + use ContainerAwareTrait; + /** * The wrapped HTTP kernel. * @@ -24,31 +31,31 @@ class Session implements HttpKernelInterface { protected $httpKernel; /** - * The session. + * The session service name. * - * @var \Symfony\Component\HttpFoundation\Session\SessionInterface + * @var string */ - protected $session; + protected $sessionServiceName; /** * Constructs a Session stack middleware object. * * @param \Symfony\Component\HttpKernel\HttpKernelInterface $http_kernel * The decorated kernel. - * @param \Symfony\Component\HttpFoundation\Session\SessionInterface $session - * The session. + * @param string $service_name + * The name of the session service, defaults to "session". */ - public function __construct(HttpKernelInterface $http_kernel, SessionInterface $session) { + public function __construct(HttpKernelInterface $http_kernel, $service_name = 'session') { $this->httpKernel = $http_kernel; - $this->session = $session; + $this->sessionServiceName = $service_name; } /** * {@inheritdoc} */ public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = TRUE) { - if ($type === self::MASTER_REQUEST) { - $request->setSession($this->session); + if ($type === self::MASTER_REQUEST && PHP_SAPI !== 'cli') { + $request->setSession($this->container->get($this->sessionServiceName)); } $result = $this->httpKernel->handle($request, $type, $catch);