Loading core/lib/Drupal/Core/DependencyInjection/AutowireTrait.php +1 −1 Original line number Diff line number Diff line Loading @@ -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; } Loading core/modules/system/tests/modules/system_test/src/Controller/SystemTestController.php +9 −0 Original line number Diff line number Diff line Loading @@ -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; Loading Loading @@ -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')] Loading @@ -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; Loading core/tests/Drupal/KernelTests/Core/Controller/ControllerBaseTest.php +5 −0 Original line number Diff line number Diff line Loading @@ -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'); Loading @@ -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); } /** Loading Loading
core/lib/Drupal/Core/DependencyInjection/AutowireTrait.php +1 −1 Original line number Diff line number Diff line Loading @@ -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; } Loading
core/modules/system/tests/modules/system_test/src/Controller/SystemTestController.php +9 −0 Original line number Diff line number Diff line Loading @@ -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; Loading Loading @@ -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')] Loading @@ -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; Loading
core/tests/Drupal/KernelTests/Core/Controller/ControllerBaseTest.php +5 −0 Original line number Diff line number Diff line Loading @@ -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'); Loading @@ -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); } /** Loading