Skip to content

Commit f56d9a4

Browse files
authored
Add support for non-singleton services (definitions) (#30)
* add support for non-singleton services * remove unused code * fix coding style Co-authored-by: Thomas Gnandt <thomas.gnandt@mayflower.de>
1 parent 7a48c4d commit f56d9a4

File tree

3 files changed

+33
-16
lines changed

3 files changed

+33
-16
lines changed

src/ServiceMap.php

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,10 @@ public function __construct(string $configPath)
3030

3131
$config = require $configPath;
3232
foreach ($config['container']['singletons'] ?? [] as $id => $service) {
33-
if ($service instanceof \Closure || \is_string($service)) {
34-
$returnType = (new \ReflectionFunction($service))->getReturnType();
35-
if (!$returnType instanceof \ReflectionNamedType) {
36-
throw new \RuntimeException(sprintf('Please provide return type for %s service closure', $id));
37-
}
38-
39-
$this->services[$id] = $returnType->getName();
40-
} else {
41-
$this->services[$id] = $service['class'] ?? $service[0]['class'];
42-
}
33+
$this->addServiceDefinition($id, $service);
34+
}
35+
foreach ($config['container']['definitions'] ?? [] as $id => $service) {
36+
$this->addServiceDefinition($id, $service);
4337
}
4438

4539
foreach ($config['components'] ?? [] as $id => $component) {
@@ -52,10 +46,6 @@ public function __construct(string $configPath)
5246
throw new \RuntimeException(sprintf('Invalid value for component with id %s. Expected object or array.', $id));
5347
}
5448

55-
if (null !== $identityClass = $component['identityClass'] ?? null) {
56-
$this->components[$id]['identityClass'] = $identityClass;
57-
}
58-
5949
if (null !== $class = $component['class'] ?? null) {
6050
$this->components[$id]['class'] = $class;
6151
}
@@ -81,8 +71,22 @@ public function getComponentClassById(string $id): ?string
8171
return $this->components[$id]['class'] ?? null;
8272
}
8373

84-
public function getComponentIdentityClassById(string $id): ?string
74+
/**
75+
* @param string|\Closure|array<mixed> $service
76+
*
77+
* @throws \ReflectionException
78+
*/
79+
private function addServiceDefinition(string $id, $service): void
8580
{
86-
return $this->components[$id]['identityClass'] ?? null;
81+
if ($service instanceof \Closure || \is_string($service)) {
82+
$returnType = (new \ReflectionFunction($service))->getReturnType();
83+
if (!$returnType instanceof \ReflectionNamedType) {
84+
throw new \RuntimeException(sprintf('Please provide return type for %s service closure', $id));
85+
}
86+
87+
$this->services[$id] = $returnType->getName();
88+
} else {
89+
$this->services[$id] = $service['class'] ?? $service[0]['class'];
90+
}
8791
}
8892
}

tests/ServiceMapTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ public function testItLoadsServicesAndComponents(): void
3939
{
4040
$serviceMap = new ServiceMap(__DIR__.DIRECTORY_SEPARATOR.'assets'.DIRECTORY_SEPARATOR.'yii-config-valid.php');
4141

42+
$this->assertSame(\SplStack::class, $serviceMap->getServiceClassFromNode(new String_('singleton-closure')));
43+
$this->assertSame(\SplObjectStorage::class, $serviceMap->getServiceClassFromNode(new String_('singleton-service')));
44+
$this->assertSame(\SplFileInfo::class, $serviceMap->getServiceClassFromNode(new String_('singleton-nested-service-class')));
45+
4246
$this->assertSame(\SplStack::class, $serviceMap->getServiceClassFromNode(new String_('closure')));
4347
$this->assertSame(\SplObjectStorage::class, $serviceMap->getServiceClassFromNode(new String_('service')));
4448
$this->assertSame(\SplFileInfo::class, $serviceMap->getServiceClassFromNode(new String_('nested-service-class')));

tests/assets/yii-config-valid.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@
1111
],
1212
'container' => [
1313
'singletons' => [
14+
'singleton-closure' => function(): \SplStack {
15+
return new \SplStack();
16+
},
17+
'singleton-service' => ['class' => \SplObjectStorage::class],
18+
'singleton-nested-service-class' => [
19+
['class' => \SplFileInfo::class]
20+
]
21+
],
22+
'definitions' => [
1423
'closure' => function(): \SplStack {
1524
return new \SplStack();
1625
},

0 commit comments

Comments
 (0)