Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle already-initialized components #15

Merged
merged 1 commit into from
Sep 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
akondas marked this conversation as resolved.
Show resolved Hide resolved
}

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