Skip to content

Commit

Permalink
Handle already-initialized components (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
EtienneBruines authored and akondas committed Sep 10, 2019
1 parent ab51e6b commit 6b006ff
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/ServiceMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ final class ServiceMap
private $services = [];

/**
* @var string[][]
* @var (string[]|object)[]
*/
private $components = [];

Expand Down Expand Up @@ -43,6 +43,15 @@ public function __construct(string $configPath)
}

foreach ($config['components'] ?? [] as $id => $component) {
if (is_object($component)) {
$this->components[$id] = $component;
continue;
}

if (!is_array($component)) {
throw new \RuntimeException(sprintf('Invalid value for component with id %s. Expected object or array.', $id));
}

if (null !== $identityClass = $component['identityClass'] ?? null) {
$this->components[$id]['identityClass'] = $identityClass;
}
Expand All @@ -64,6 +73,11 @@ public function getServiceClassFromNode(Node $node): ?string

public function getComponentClassById(string $id): ?string
{
// Special case in which the component is already initialized
if (is_object($this->components[$id])) {
return get_class($this->components[$id]);
}

return $this->components[$id]['class'] ?? null;
}

Expand Down
9 changes: 9 additions & 0 deletions tests/ServiceMapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ public function testThrowExceptionWhenClosureServiceHasMissingReturnType(): void
new ServiceMap(__DIR__.DIRECTORY_SEPARATOR.'assets'.DIRECTORY_SEPARATOR.'yii-config-invalid.php');
}

public function testThrowExceptionWhenComponentHasInvalidValue(): void
{
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('Invalid value for component with id customComponent. Expected object or array.');

new ServiceMap(__DIR__.DIRECTORY_SEPARATOR.'assets'.DIRECTORY_SEPARATOR.'yii-config-invalid-component.php');
}

public function testItLoadsServicesAndComponents(): void
{
$serviceMap = new ServiceMap(__DIR__.DIRECTORY_SEPARATOR.'assets'.DIRECTORY_SEPARATOR.'yii-config-valid.php');
Expand All @@ -36,6 +44,7 @@ public function testItLoadsServicesAndComponents(): void
$this->assertSame(\SplFileInfo::class, $serviceMap->getServiceClassFromNode(new String_('nested-service-class')));

$this->assertSame(MyActiveRecord::class, $serviceMap->getComponentClassById('customComponent'));
$this->assertSame(MyActiveRecord::class, $serviceMap->getComponentClassById('customInitializedComponent'));
}

/**
Expand Down
7 changes: 7 additions & 0 deletions tests/assets/yii-config-invalid-component.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

return [
'components' => [
'customComponent' => 5,
],
];
1 change: 1 addition & 0 deletions tests/assets/yii-config-valid.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
'customComponent' => [
'class' => MyActiveRecord::class,
],
'customInitializedComponent' => new MyActiveRecord(),
],
'container' => [
'singletons' => [
Expand Down

0 comments on commit 6b006ff

Please sign in to comment.