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
Factories ignore default parameters #526
Comments
Ran into this as well. Frustrating. |
Going over the original report I'm not sure I completely understand the problem. @ccazette could you explain it in your own words? |
Sure. In short, you have to specify all factory parameters using |
Also, non-optional parameters are treated as required in factories. Here's a short concrete example of both, since they are maybe the same issue. I have this class that returns named database connections. The idea is this class is used in different applications, some of which have multiple database adapters, some of which have only one. It returns either a specific named connection or the application's default connection. class ConnectionManager {
public function getConnection($name = null) {
// logic to fetch named connection, using default name if $name is null
}
public function __invoke(ContainerInterface $container, RequestedEntry $entry, $name = null) {
return $this->getConnection();
}
} I've tried the following two definitions for injecting a connection into [Service::class => DI\autowire()->constructorParameter('connection', DI\factory([ConnectionManager::class, 'getConnection']))] This fails since This sort-of makes sense since the assumption is that a factory callable needs something passed to it in order to resolve its definitions, but I think it would less magical if they were not be passed at all if they aren't type hinted.
This fails with: This does not make sense because the parameter is clearly optional, and therefore should not be treated as required by Of course this can be trivially worked around by with Here is the code in question: PHP-DI/src/DI/Definition/Resolver/FactoryResolver.php Lines 76 to 82 in ff03746
|
Fix #526 Support optional parameters in factories
I just wanted to mention the ultimate fix for this is very elegant. That commit makes it clear that the behavior described in this issue was just a small oversight and not a design deficiency. PHP-DI is an extraordinarily well-designed piece of software IMHO. |
😄 thanks! To be honest not all bugs are like that but yep I really enjoyed that fix ;) |
When using a factorie for a parameter the default values are ignored. Instead the container or the definition is injected.
Example:
This fails with the following exception:
DI\Definition\Exception\InvalidDefinition: Entry "test" cannot be resolved: Unable to invoke the callable because no value was given for parameter 3 ($encoding)
Parameter 2 is not mentioned because the container and the definition are injected as per this test:
This produces:
DI\Container DI\Definition\FactoryDefinition
The text was updated successfully, but these errors were encountered: