Verified Commit 0b80bc53 authored by Dave Long's avatar Dave Long
Browse files

Issue #3410222 by alexpott: Autowiring does not support nullable types

parent b604d68a
Loading
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -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;
        }
+9 −0
Original line number Diff line number Diff line
@@ -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;
+5 −0
Original line number Diff line number Diff line
@@ -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);
  }

  /**