diff --git a/core/lib/Drupal/Core/DependencyInjection/AutowireTrait.php b/core/lib/Drupal/Core/DependencyInjection/AutowireTrait.php index 855bf20d82cd2acc517629746f4a1c734f312663..617699bda15f0c1f331dc7c1c29a6ecbbfcca677 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 f5cf04c5bdce18d5a649c07f66a73fc077206caa..6b6ef7686f5efcbb34910ad123dddd88981057f3 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 257982e2ea6e0adf7a35621923079f83e65aa7df..c21f989de9a8f593c715decf81925817371b81b1 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 8a74fd0e357d5d835112d8e3eb77f27151466b3f..828e1c5aa5b6870cd7acb2d2b3395f1f0f83f169 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); } /**