File tree Expand file tree Collapse file tree 4 files changed +32
-1
lines changed Expand file tree Collapse file tree 4 files changed +32
-1
lines changed Original file line number Diff line number Diff line change @@ -14,7 +14,7 @@ final class ServiceMap
14
14
private $ services = [];
15
15
16
16
/**
17
- * @var string[][]
17
+ * @var ( string[]|object) []
18
18
*/
19
19
private $ components = [];
20
20
@@ -43,6 +43,15 @@ public function __construct(string $configPath)
43
43
}
44
44
45
45
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
+
46
55
if (null !== $ identityClass = $ component ['identityClass ' ] ?? null ) {
47
56
$ this ->components [$ id ]['identityClass ' ] = $ identityClass ;
48
57
}
@@ -64,6 +73,11 @@ public function getServiceClassFromNode(Node $node): ?string
64
73
65
74
public function getComponentClassById (string $ id ): ?string
66
75
{
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
+
67
81
return $ this ->components [$ id ]['class ' ] ?? null ;
68
82
}
69
83
Original file line number Diff line number Diff line change @@ -27,6 +27,14 @@ public function testThrowExceptionWhenClosureServiceHasMissingReturnType(): void
27
27
new ServiceMap (__DIR__ .DIRECTORY_SEPARATOR .'assets ' .DIRECTORY_SEPARATOR .'yii-config-invalid.php ' );
28
28
}
29
29
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
+
30
38
public function testItLoadsServicesAndComponents (): void
31
39
{
32
40
$ serviceMap = new ServiceMap (__DIR__ .DIRECTORY_SEPARATOR .'assets ' .DIRECTORY_SEPARATOR .'yii-config-valid.php ' );
@@ -36,6 +44,7 @@ public function testItLoadsServicesAndComponents(): void
36
44
$ this ->assertSame (\SplFileInfo::class, $ serviceMap ->getServiceClassFromNode (new String_ ('nested-service-class ' )));
37
45
38
46
$ this ->assertSame (MyActiveRecord::class, $ serviceMap ->getComponentClassById ('customComponent ' ));
47
+ $ this ->assertSame (MyActiveRecord::class, $ serviceMap ->getComponentClassById ('customInitializedComponent ' ));
39
48
}
40
49
41
50
/**
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ return [
4
+ 'components ' => [
5
+ 'customComponent ' => 5 ,
6
+ ],
7
+ ];
Original file line number Diff line number Diff line change 7
7
'customComponent ' => [
8
8
'class ' => MyActiveRecord::class,
9
9
],
10
+ 'customInitializedComponent ' => new MyActiveRecord (),
10
11
],
11
12
'container ' => [
12
13
'singletons ' => [
You can’t perform that action at this time.
0 commit comments