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

ReflectionException: Method Nette\DI\Container::createServiceFromStaticFactory() does not exist #314

Closed
zeleznypa opened this issue Apr 1, 2024 · 0 comments

Comments

@zeleznypa
Copy link

Version: 3.2.1

Bug Description

The Nette\DI\Container allows the addition of a service as a static factory. All operations can handle this except the Container::getByType method, which fails when creating a reflection over a non-existent container method.

Steps To Reproduce

class Service {
}

$container = new Nette\DI\Container\Container();
$container->addService('fromStaticFactory', static fn(): Service => new Service());
$container->getByType(Service::class)

Expected Behavior

There should be no exception when attempting to access a non-existent method.

Possible Solution

Use reflection of the method given in $foo

di/src/DI/Container.php

Lines 261 to 262 in 9c4af52

foreach ($this->methods as $method => $foo) {
$methodType = (new \ReflectionMethod(static::class, $method))->getReturnType()->getName();

Something like this:

foreach ($this->metohds as $method) {
    $methodType = \Nette\Utils\Callback::toReflection($method)->getReturnType()->getName();
    if (is_a($methodType, $type, allow_string: true)) {
        throw new MissingServiceException(sprintf(
            "Service of type %s is not autowired or is missing in di\u{a0}\u{a0}export\u{a0}\u{a0}types.",
            $type,
        ));
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants