Skip to content

Commit 6b006ff

Browse files
EtienneBruinesakondas
authored andcommitted
Handle already-initialized components (#15)
1 parent ab51e6b commit 6b006ff

File tree

4 files changed

+32
-1
lines changed

4 files changed

+32
-1
lines changed

src/ServiceMap.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ final class ServiceMap
1414
private $services = [];
1515

1616
/**
17-
* @var string[][]
17+
* @var (string[]|object)[]
1818
*/
1919
private $components = [];
2020

@@ -43,6 +43,15 @@ public function __construct(string $configPath)
4343
}
4444

4545
foreach ($config['components'] ?? [] as $id => $component) {
46+
if (is_object($component)) {
47+
$this->components[$id] = $component;
48+
continue;
49+
}
50+
51+
if (!is_array($component)) {
52+
throw new \RuntimeException(sprintf('Invalid value for component with id %s. Expected object or array.', $id));
53+
}
54+
4655
if (null !== $identityClass = $component['identityClass'] ?? null) {
4756
$this->components[$id]['identityClass'] = $identityClass;
4857
}
@@ -64,6 +73,11 @@ public function getServiceClassFromNode(Node $node): ?string
6473

6574
public function getComponentClassById(string $id): ?string
6675
{
76+
// Special case in which the component is already initialized
77+
if (is_object($this->components[$id])) {
78+
return get_class($this->components[$id]);
79+
}
80+
6781
return $this->components[$id]['class'] ?? null;
6882
}
6983

tests/ServiceMapTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ public function testThrowExceptionWhenClosureServiceHasMissingReturnType(): void
2727
new ServiceMap(__DIR__.DIRECTORY_SEPARATOR.'assets'.DIRECTORY_SEPARATOR.'yii-config-invalid.php');
2828
}
2929

30+
public function testThrowExceptionWhenComponentHasInvalidValue(): void
31+
{
32+
$this->expectException(\RuntimeException::class);
33+
$this->expectExceptionMessage('Invalid value for component with id customComponent. Expected object or array.');
34+
35+
new ServiceMap(__DIR__.DIRECTORY_SEPARATOR.'assets'.DIRECTORY_SEPARATOR.'yii-config-invalid-component.php');
36+
}
37+
3038
public function testItLoadsServicesAndComponents(): void
3139
{
3240
$serviceMap = new ServiceMap(__DIR__.DIRECTORY_SEPARATOR.'assets'.DIRECTORY_SEPARATOR.'yii-config-valid.php');
@@ -36,6 +44,7 @@ public function testItLoadsServicesAndComponents(): void
3644
$this->assertSame(\SplFileInfo::class, $serviceMap->getServiceClassFromNode(new String_('nested-service-class')));
3745

3846
$this->assertSame(MyActiveRecord::class, $serviceMap->getComponentClassById('customComponent'));
47+
$this->assertSame(MyActiveRecord::class, $serviceMap->getComponentClassById('customInitializedComponent'));
3948
}
4049

4150
/**
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
return [
4+
'components' => [
5+
'customComponent' => 5,
6+
],
7+
];

tests/assets/yii-config-valid.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
'customComponent' => [
88
'class' => MyActiveRecord::class,
99
],
10+
'customInitializedComponent' => new MyActiveRecord(),
1011
],
1112
'container' => [
1213
'singletons' => [

0 commit comments

Comments
 (0)