Skip to content

Commit 49277a3

Browse files
committed
Add a workaround with globals to ensure service mapping
1 parent d0510f1 commit 49277a3

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

src/Drupal/DrupalAutoloader.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,14 @@ class: Drupal\jsonapi\Routing\JsonApiParamEnhancer
191191

192192
$service_map = $container->getByType(ServiceMap::class);
193193
assert($service_map instanceof ServiceMap);
194-
// @todo this is not updating the reference in the container.
194+
// @todo this is a hack that needs investigation.
195+
// We cannot manipulate the service container and add parameters, so we take the existing
196+
// service and modify it's properties so that its reference is updated within the container.
197+
//
198+
// During debug this works, but other times it fails.
195199
$service_map->setDrupalServices($this->serviceMap);
200+
// So, to work around whatever is happening we force it into globals.
201+
$GLOBALS['drupalServiceMap'] = $service_map->getServices();
196202
}
197203

198204
protected function loadLegacyIncludes(): void

src/Drupal/ServiceMap.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,25 @@ class ServiceMap
1111

1212
public function getService(string $id): ?DrupalServiceDefinition
1313
{
14+
// @see notes in DrupalAutoloader.
15+
// This is all a work around due to inability to set container parameters.
1416
if (count($this->services) === 0) {
15-
throw new ShouldNotHappenException('No Drupal service map was registered.');
17+
$this->services = $GLOBALS['drupalServiceMap'];
18+
if (count($this->services) === 0) {
19+
throw new ShouldNotHappenException('No Drupal service map was registered.');
20+
}
1621
}
1722
return $this->services[$id] ?? null;
1823
}
1924

25+
/**
26+
* @return \PHPStan\Drupal\DrupalServiceDefinition[]
27+
*/
28+
public function getServices(): array
29+
{
30+
return $this->services;
31+
}
32+
2033
public function setDrupalServices(array $drupalServices): void
2134
{
2235
$this->services = [];

0 commit comments

Comments
 (0)