From a4b69560ea1a555d3f0c183722c22f59624ccde6 Mon Sep 17 00:00:00 2001
From: Dave Long <dave@longwaveconsulting.com>
Date: Sun, 28 Jan 2024 22:55:21 +0000
Subject: [PATCH] Issue #3410222 by alexpott: Autowiring does not support
 nullable types

---
 .../Drupal/Core/DependencyInjection/AutowireTrait.php    | 2 +-
 .../system_test/src/Controller/SystemTestController.php  | 9 +++++++++
 .../modules/toolbar/src/Controller/ToolbarController.php | 2 --
 .../KernelTests/Core/Controller/ControllerBaseTest.php   | 5 +++++
 4 files changed, 15 insertions(+), 3 deletions(-)

diff --git a/core/lib/Drupal/Core/DependencyInjection/AutowireTrait.php b/core/lib/Drupal/Core/DependencyInjection/AutowireTrait.php
index 855bf20d82cd..617699bda15f 100644
--- a/core/lib/Drupal/Core/DependencyInjection/AutowireTrait.php
+++ b/core/lib/Drupal/Core/DependencyInjection/AutowireTrait.php
@@ -28,7 +28,7 @@ public static function create(ContainerInterface $container) {
     if (method_exists(static::class, '__construct')) {
       $constructor = new \ReflectionMethod(static::class, '__construct');
       foreach ($constructor->getParameters() as $parameter) {
-        $service = (string) $parameter->getType();
+        $service = ltrim((string) $parameter->getType(), '?');
         foreach ($parameter->getAttributes(Autowire::class) as $attribute) {
           $service = (string) $attribute->newInstance()->value;
         }
diff --git a/core/modules/system/tests/modules/system_test/src/Controller/SystemTestController.php b/core/modules/system/tests/modules/system_test/src/Controller/SystemTestController.php
index f5cf04c5bdce..6b6ef7686f5e 100644
--- a/core/modules/system/tests/modules/system_test/src/Controller/SystemTestController.php
+++ b/core/modules/system/tests/modules/system_test/src/Controller/SystemTestController.php
@@ -6,6 +6,7 @@
 use Drupal\Core\Cache\CacheableResponse;
 use Drupal\Core\Controller\ControllerBase;
 use Drupal\Core\Messenger\MessengerInterface;
+use Drupal\Core\PageCache\ResponsePolicy\KillSwitch;
 use Drupal\Core\Security\TrustedCallbackInterface;
 use Drupal\Core\Render\RendererInterface;
 use Drupal\Core\Render\Markup;
@@ -70,6 +71,12 @@ class SystemTestController extends ControllerBase implements TrustedCallbackInte
    *   The renderer.
    * @param \Drupal\Core\Messenger\MessengerInterface $messenger
    *   The messenger service.
+   * @param \Drupal\Core\PageCache\ResponsePolicy\KillSwitch|null $killSwitch
+   *   The page cache kill switch. This is here to test nullable types with
+   *   \Drupal\Core\DependencyInjection\AutowireTrait::create().
+   * @param \Drupal\Core\PageCache\ResponsePolicy\KillSwitch|null $killSwitch2
+   *   The page cache kill switch. This is here to test nullable types with
+   *   \Drupal\Core\DependencyInjection\AutowireTrait::create().
    */
   public function __construct(
     #[Autowire(service: 'lock')]
@@ -79,6 +86,8 @@ public function __construct(
     AccountInterface $current_user,
     RendererInterface $renderer,
     MessengerInterface $messenger,
+    public ?KillSwitch $killSwitch = NULL,
+    public KillSwitch|null $killSwitch2 = NULL,
   ) {
     $this->lock = $lock;
     $this->persistentLock = $persistent_lock;
diff --git a/core/modules/toolbar/src/Controller/ToolbarController.php b/core/modules/toolbar/src/Controller/ToolbarController.php
index 257982e2ea6e..c21f989de9a8 100644
--- a/core/modules/toolbar/src/Controller/ToolbarController.php
+++ b/core/modules/toolbar/src/Controller/ToolbarController.php
@@ -11,7 +11,6 @@
 use Drupal\Core\Render\RenderContext;
 use Drupal\Core\Security\TrustedCallbackInterface;
 use Drupal\toolbar\Ajax\SetSubtreesCommand;
-use Symfony\Component\DependencyInjection\Attribute\Autowire;
 
 /**
  * Defines a controller for the toolbar module.
@@ -25,7 +24,6 @@ class ToolbarController extends ControllerBase implements TrustedCallbackInterfa
    *   The time service.
    */
   public function __construct(
-    #[Autowire(service: 'datetime.time')]
     protected ?TimeInterface $time = NULL
   ) {
     if ($this->time === NULL) {
diff --git a/core/tests/Drupal/KernelTests/Core/Controller/ControllerBaseTest.php b/core/tests/Drupal/KernelTests/Core/Controller/ControllerBaseTest.php
index 8a74fd0e357d..828e1c5aa5b6 100644
--- a/core/tests/Drupal/KernelTests/Core/Controller/ControllerBaseTest.php
+++ b/core/tests/Drupal/KernelTests/Core/Controller/ControllerBaseTest.php
@@ -26,6 +26,7 @@ class ControllerBaseTest extends KernelTestBase {
    * @covers ::create
    */
   public function testCreate() {
+    /** @var \Drupal\system_test\Controller\SystemTestController $controller */
     $controller = $this->container->get('class_resolver')->getInstanceFromDefinition(SystemTestController::class);
 
     $property = new \ReflectionProperty(SystemTestController::class, 'lock');
@@ -36,6 +37,10 @@ public function testCreate() {
 
     $property = new \ReflectionProperty(SystemTestController::class, 'currentUser');
     $this->assertSame($this->container->get('current_user'), $property->getValue($controller));
+
+    // Test nullables types.
+    $this->assertSame($this->container->get('page_cache_kill_switch'), $controller->killSwitch);
+    $this->assertSame($this->container->get('page_cache_kill_switch'), $controller->killSwitch2);
   }
 
   /**
-- 
GitLab