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
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
Original file line number Diff line number Diff 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 /**
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 77 'customComponent ' => [
88 'class ' => MyActiveRecord::class,
99 ],
10+ 'customInitializedComponent ' => new MyActiveRecord (),
1011 ],
1112 'container ' => [
1213 'singletons ' => [
You can’t perform that action at this time.
0 commit comments