Skip to content

Commit

Permalink
Register dynamic services from DrupalKernel::attachSynthetic (#762)
Browse files Browse the repository at this point in the history
  • Loading branch information
mglaman authored May 10, 2024
1 parent a8b25cc commit e624a4b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Drupal/DrupalAutoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace mglaman\PHPStanDrupal\Drupal;

use Composer\Autoload\ClassLoader;
use Drupal\Core\DependencyInjection\ContainerNotInitializedException;
use Drupal\Core\DrupalKernelInterface;
use Drupal\TestTools\PhpUnitCompatibility\PhpUnit8\ClassWriter;
use DrupalFinder\DrupalFinder;
use Drush\Drush;
Expand Down Expand Up @@ -102,6 +104,10 @@ public function register(Container $container): void
$this->serviceYamls['core'] = $drupalRoot . '/core/core.services.yml';
$this->serviceClassProviders['core'] = '\Drupal\Core\CoreServiceProvider';
$this->serviceMap['service_provider.core.service_provider'] = ['class' => $this->serviceClassProviders['core']];
// Attach synthetic services
// @see \Drupal\Core\DrupalKernel::attachSynthetic
$this->serviceMap['kernel'] = ['class' => DrupalKernelInterface::class];
$this->serviceMap['class_loader'] = ['class' => ClassLoader::class];

$extensionDiscovery = new ExtensionDiscovery($this->drupalRoot);
$extensionDiscovery->setProfileDirectories([]);
Expand Down
1 change: 1 addition & 0 deletions tests/src/Type/DrupalContainerDynamicReturnTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public static function dataFileAsserts(): iterable
yield from self::gatherAssertTypes(__DIR__ . '/data/drupal-class-resolver.php');
yield from self::gatherAssertTypes(__DIR__ . '/data/bug-563.php');
yield from self::gatherAssertTypes(__DIR__ . '/data/container-optional.php');
yield from self::gatherAssertTypes(__DIR__ . '/data/synthetic.php');
}

/**
Expand Down
13 changes: 13 additions & 0 deletions tests/src/Type/data/synthetic.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace DrupalContainerSyntheticServices;

use Composer\Autoload\ClassLoader;
use Drupal\Core\DrupalKernelInterface;
use function PHPStan\Testing\assertType;

function foo(): void {
$container = \Drupal::getContainer();
assertType(DrupalKernelInterface::class, $container->get('kernel'));
assertType(ClassLoader::class, $container->get('class_loader'));
}

0 comments on commit e624a4b

Please sign in to comment.