From b1d0f64f9838409fcba5fb22f47190476b311f06 Mon Sep 17 00:00:00 2001
From: catch <catch@35733.no-reply.drupal.org>
Date: Thu, 29 Feb 2024 12:55:35 +0000
Subject: [PATCH] Issue #3420215 by longwave, catch, Spokje: Remove
 ContainerAwareTrait from session middleware

---
 core/core.services.yml                        |  3 +-
 .../Drupal/Core/StackMiddleware/Session.php   | 30 ++++++++-----------
 core/phpstan-baseline.neon                    |  8 -----
 3 files changed, 13 insertions(+), 28 deletions(-)

diff --git a/core/core.services.yml b/core/core.services.yml
index ef0caadceadf..4c35a65395d3 100644
--- a/core/core.services.yml
+++ b/core/core.services.yml
@@ -941,10 +941,9 @@ services:
       - { name: http_middleware, priority: 100 }
   http_middleware.session:
     class: Drupal\Core\StackMiddleware\Session
+    autowire: true
     tags:
       - { name: http_middleware, priority: 50 }
-    calls:
-      - [setContainer, ['@service_container']]
   http_middleware.cors:
     class: Asm89\Stack\Cors
     arguments: ['%cors.config%']
diff --git a/core/lib/Drupal/Core/StackMiddleware/Session.php b/core/lib/Drupal/Core/StackMiddleware/Session.php
index 6e8082b81e44..c7778cb2dcd6 100644
--- a/core/lib/Drupal/Core/StackMiddleware/Session.php
+++ b/core/lib/Drupal/Core/StackMiddleware/Session.php
@@ -3,7 +3,7 @@
 namespace Drupal\Core\StackMiddleware;
 
 use Drupal\Core\Session\ResponseKeepSessionOpenInterface;
-use Symfony\Component\DependencyInjection\ContainerAwareTrait;
+use Symfony\Component\DependencyInjection\Attribute\AutowireServiceClosure;
 use Symfony\Component\HttpFoundation\Request;
 use Symfony\Component\HttpFoundation\Response;
 use Symfony\Component\HttpKernel\HttpKernelInterface;
@@ -11,14 +11,11 @@
 /**
  * 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
- * service is retrieved from the container only when handling the request.
+ * Note, the session service is wrapped in a closure in order to prevent
+ * premature initialization of session storage (database).
  */
 class Session implements HttpKernelInterface {
 
-  use ContainerAwareTrait;
-
   /**
    * The wrapped HTTP kernel.
    *
@@ -26,24 +23,20 @@ class Session implements HttpKernelInterface {
    */
   protected $httpKernel;
 
-  /**
-   * The session service name.
-   *
-   * @var string
-   */
-  protected $sessionServiceName;
-
   /**
    * Constructs a Session stack middleware object.
    *
    * @param \Symfony\Component\HttpKernel\HttpKernelInterface $http_kernel
    *   The decorated kernel.
-   * @param string $service_name
-   *   The name of the session service, defaults to "session".
+   * @param \Closure $sessionClosure
+   *   A closure that wraps the session service.
    */
-  public function __construct(HttpKernelInterface $http_kernel, $service_name = 'session') {
+  public function __construct(
+    HttpKernelInterface $http_kernel,
+    #[AutowireServiceClosure('session')]
+    protected \Closure $sessionClosure,
+  ) {
     $this->httpKernel = $http_kernel;
-    $this->sessionServiceName = $service_name;
   }
 
   /**
@@ -83,7 +76,8 @@ public function handle(Request $request, $type = self::MAIN_REQUEST, $catch = TR
    * @see \Drupal\Core\DrupalKernel::preHandle()
    */
   protected function initializePersistentSession(Request $request): void {
-    $session = $this->container->get($this->sessionServiceName);
+    /** @var \Symfony\Component\HttpFoundation\Session\SessionInterface $session */
+    $session = ($this->sessionClosure)();
     $session->start();
     $request->setSession($session);
   }
diff --git a/core/phpstan-baseline.neon b/core/phpstan-baseline.neon
index ae8a75f01291..a6d44a12408c 100644
--- a/core/phpstan-baseline.neon
+++ b/core/phpstan-baseline.neon
@@ -791,14 +791,6 @@ parameters:
 			count: 3
 			path: lib/Drupal/Core/Site/SettingsEditor.php
 
-		-
-			message: """
-				#^Usage of deprecated trait Symfony\\\\Component\\\\DependencyInjection\\\\ContainerAwareTrait in class Drupal\\\\Core\\\\StackMiddleware\\\\Session\\:
-				since Symfony 6\\.4, use dependency injection instead$#
-			"""
-			count: 1
-			path: lib/Drupal/Core/StackMiddleware/Session.php
-
 		-
 			message: "#^Method Drupal\\\\Core\\\\Template\\\\AttributeValueBase\\:\\:render\\(\\) should return string but return statement is missing\\.$#"
 			count: 1
-- 
GitLab