Skip to content

Commit

Permalink
Add support for non-singleton services (definitions) (#30)
Browse files Browse the repository at this point in the history
* add support for non-singleton services

* remove unused code

* fix coding style

Co-authored-by: Thomas Gnandt <[email protected]>
  • Loading branch information
Khartir and Khartir authored Jul 13, 2020
1 parent 7a48c4d commit f56d9a4
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 16 deletions.
36 changes: 20 additions & 16 deletions src/ServiceMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,10 @@ public function __construct(string $configPath)

$config = require $configPath;
foreach ($config['container']['singletons'] ?? [] as $id => $service) {
if ($service instanceof \Closure || \is_string($service)) {
$returnType = (new \ReflectionFunction($service))->getReturnType();
if (!$returnType instanceof \ReflectionNamedType) {
throw new \RuntimeException(sprintf('Please provide return type for %s service closure', $id));
}

$this->services[$id] = $returnType->getName();
} else {
$this->services[$id] = $service['class'] ?? $service[0]['class'];
}
$this->addServiceDefinition($id, $service);
}
foreach ($config['container']['definitions'] ?? [] as $id => $service) {
$this->addServiceDefinition($id, $service);
}

foreach ($config['components'] ?? [] as $id => $component) {
Expand All @@ -52,10 +46,6 @@ public function __construct(string $configPath)
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;
}

if (null !== $class = $component['class'] ?? null) {
$this->components[$id]['class'] = $class;
}
Expand All @@ -81,8 +71,22 @@ public function getComponentClassById(string $id): ?string
return $this->components[$id]['class'] ?? null;
}

public function getComponentIdentityClassById(string $id): ?string
/**
* @param string|\Closure|array<mixed> $service
*
* @throws \ReflectionException
*/
private function addServiceDefinition(string $id, $service): void
{
return $this->components[$id]['identityClass'] ?? null;
if ($service instanceof \Closure || \is_string($service)) {
$returnType = (new \ReflectionFunction($service))->getReturnType();
if (!$returnType instanceof \ReflectionNamedType) {
throw new \RuntimeException(sprintf('Please provide return type for %s service closure', $id));
}

$this->services[$id] = $returnType->getName();
} else {
$this->services[$id] = $service['class'] ?? $service[0]['class'];
}
}
}
4 changes: 4 additions & 0 deletions tests/ServiceMapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ public function testItLoadsServicesAndComponents(): void
{
$serviceMap = new ServiceMap(__DIR__.DIRECTORY_SEPARATOR.'assets'.DIRECTORY_SEPARATOR.'yii-config-valid.php');

$this->assertSame(\SplStack::class, $serviceMap->getServiceClassFromNode(new String_('singleton-closure')));
$this->assertSame(\SplObjectStorage::class, $serviceMap->getServiceClassFromNode(new String_('singleton-service')));
$this->assertSame(\SplFileInfo::class, $serviceMap->getServiceClassFromNode(new String_('singleton-nested-service-class')));

$this->assertSame(\SplStack::class, $serviceMap->getServiceClassFromNode(new String_('closure')));
$this->assertSame(\SplObjectStorage::class, $serviceMap->getServiceClassFromNode(new String_('service')));
$this->assertSame(\SplFileInfo::class, $serviceMap->getServiceClassFromNode(new String_('nested-service-class')));
Expand Down
9 changes: 9 additions & 0 deletions tests/assets/yii-config-valid.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@
],
'container' => [
'singletons' => [
'singleton-closure' => function(): \SplStack {
return new \SplStack();
},
'singleton-service' => ['class' => \SplObjectStorage::class],
'singleton-nested-service-class' => [
['class' => \SplFileInfo::class]
]
],
'definitions' => [
'closure' => function(): \SplStack {
return new \SplStack();
},
Expand Down

0 comments on commit f56d9a4

Please sign in to comment.