Skip to content

Commit e624a4b

Browse files
authored
Register dynamic services from DrupalKernel::attachSynthetic (#762)
1 parent a8b25cc commit e624a4b

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

src/Drupal/DrupalAutoloader.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace mglaman\PHPStanDrupal\Drupal;
44

5+
use Composer\Autoload\ClassLoader;
56
use Drupal\Core\DependencyInjection\ContainerNotInitializedException;
7+
use Drupal\Core\DrupalKernelInterface;
68
use Drupal\TestTools\PhpUnitCompatibility\PhpUnit8\ClassWriter;
79
use DrupalFinder\DrupalFinder;
810
use Drush\Drush;
@@ -102,6 +104,10 @@ public function register(Container $container): void
102104
$this->serviceYamls['core'] = $drupalRoot . '/core/core.services.yml';
103105
$this->serviceClassProviders['core'] = '\Drupal\Core\CoreServiceProvider';
104106
$this->serviceMap['service_provider.core.service_provider'] = ['class' => $this->serviceClassProviders['core']];
107+
// Attach synthetic services
108+
// @see \Drupal\Core\DrupalKernel::attachSynthetic
109+
$this->serviceMap['kernel'] = ['class' => DrupalKernelInterface::class];
110+
$this->serviceMap['class_loader'] = ['class' => ClassLoader::class];
105111

106112
$extensionDiscovery = new ExtensionDiscovery($this->drupalRoot);
107113
$extensionDiscovery->setProfileDirectories([]);

tests/src/Type/DrupalContainerDynamicReturnTypeTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public static function dataFileAsserts(): iterable
1818
yield from self::gatherAssertTypes(__DIR__ . '/data/drupal-class-resolver.php');
1919
yield from self::gatherAssertTypes(__DIR__ . '/data/bug-563.php');
2020
yield from self::gatherAssertTypes(__DIR__ . '/data/container-optional.php');
21+
yield from self::gatherAssertTypes(__DIR__ . '/data/synthetic.php');
2122
}
2223

2324
/**

tests/src/Type/data/synthetic.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace DrupalContainerSyntheticServices;
4+
5+
use Composer\Autoload\ClassLoader;
6+
use Drupal\Core\DrupalKernelInterface;
7+
use function PHPStan\Testing\assertType;
8+
9+
function foo(): void {
10+
$container = \Drupal::getContainer();
11+
assertType(DrupalKernelInterface::class, $container->get('kernel'));
12+
assertType(ClassLoader::class, $container->get('class_loader'));
13+
}

0 commit comments

Comments
 (0)