Skip to content

Commit

Permalink
Container::getServiceType() ignores dynamically added services (BC br…
Browse files Browse the repository at this point in the history
…eak)
  • Loading branch information
dg committed Apr 28, 2024
1 parent ed53906 commit 067cdf5
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 12 deletions.
11 changes: 5 additions & 6 deletions src/DI/Container.php
Expand Up @@ -100,10 +100,10 @@ public function addService(string $name, object $service): static
$type = $service::class;
}

if (!isset($this->methods[self::getMethodName($name)])) {
$this->types[$name] = $type;

} elseif (($expectedType = $this->getServiceType($name)) && !is_a($type, $expectedType, allow_string: true)) {
if (isset($this->methods[self::getMethodName($name)])
&& ($expectedType = $this->getServiceType($name))
&& !is_a($type, $expectedType, allow_string: true)
) {
throw new Nette\InvalidArgumentException(sprintf(
"Service '%s' must be instance of %s, %s.",
$name,
Expand All @@ -114,7 +114,6 @@ public function addService(string $name, object $service): static

if ($service instanceof \Closure) {
$this->factories[$name] = $service;
$this->types[$name] = $type;
} else {
$this->instances[$name] = $service;
}
Expand Down Expand Up @@ -182,7 +181,7 @@ public function getServiceType(string $name): string
return (string) (new \ReflectionFunction($cb))->getReturnType();

} else {
throw new MissingServiceException(sprintf("Service '%s' not found.", $name));
throw new MissingServiceException(sprintf("Type of service '%s' not known.", $name));
}
}

Expand Down
10 changes: 7 additions & 3 deletions tests/DI/Container.dynamic.phpt
Expand Up @@ -32,12 +32,16 @@ test('basic', function () {

Assert::same($one, $container->getService('one'));
Assert::same($two, $container->getService('two'));

Assert::same(Service::class, $container->getServiceType('one'));
Assert::same(Service::class, $container->getServiceType('two'));
});


testException('type not known', function () {
$container = new Container;
$container->addService('one', new Service);
$container->getServiceType('one');
}, Nette\DI\MissingServiceException::class, "Type of service 'one' not known.");


test('closure', function () {
$container = new Container;
$container->addService('four', fn() => new Service);
Expand Down
2 changes: 1 addition & 1 deletion tests/DI/Container.getServiceType.phpt
Expand Up @@ -40,5 +40,5 @@ Assert::same('One', $container->getServiceType('three'));
Assert::exception(
fn() => $container->getServiceType('four'),
Nette\DI\MissingServiceException::class,
"Service 'four' not found.",
"Type of service 'four' not known.",
);
4 changes: 2 additions & 2 deletions tests/DI/Container.static-dynamic.phpt
Expand Up @@ -60,7 +60,7 @@ test('closure & typehint', function () {

$container->addService('one', fn(): stdClass => new stdClass);

Assert::same(stdClass::class, $container->getServiceType('one'));
Assert::same('', $container->getServiceType('one'));
Assert::true($container->hasService('one'));
Assert::type(stdClass::class, $container->getService('one'));
});
Expand All @@ -75,7 +75,7 @@ test('closure & matching typehint', function () {

$container->addService('typehint', fn(): MyClass => new MyClass);

Assert::same(MyClass::class, $container->getServiceType('typehint'));
Assert::same(stdClass::class, $container->getServiceType('typehint'));
Assert::true($container->hasService('typehint'));
Assert::type(MyClass::class, $container->getService('typehint'));
});
Expand Down

0 comments on commit 067cdf5

Please sign in to comment.