Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.Sign up
Adding support for every kind of callables and removed the need for static getServices method #404
I had a couple of hours to spare and was feeling in the mood for a coding frenzy :)
I've been doing some tests, forked your service-provider branch and finally added support for any kind of callables in PHP-DI.
I've kept some optimizations: public static function are still called directly, while the other methods must go through a
There are certainly a lot of things to be improved, but that's already a fairly good proof of concept.
I've updated the unit tests so you can have a look at what's going on easily.
Let me know what you think about this!
I've improved the implementation a bit by hiding the dependency on the
Now, you can do:
$containerBuilder->addDefinitions($myServiceProviderInstance); // or $containerBuilder->addDefinitions(MyServiceProvider::class);
In case you use the second version, it is assumed that the service provider is not taking any argument for its constructor. The service provider is created only if needed (i.e. only if no cache or if you fetch an entry whose factory is not a public static method).
I guess it's as far as I can go for a proof of concept.
What do you think? If you are interested to go this way, I can change the service locator
Thanks! I've continued working on this to get up to date with container-interop/service-provider#20 I first started off your PR but I don't think it's worth having the "service provider registry" in this case. Most definitions will not be cacheable (closures, methods, etc.), so we'll need the service provider instances most of the time, and the cost of creating such instances will probably not be very high (compared to the cost of having the whole registry). Additionally I want to avoid adding new dependencies when it can be avoided. So I went a simpler way (allow adding