Introduce two new convenience methods: registerSharedService, extendService#62
Introduce two new convenience methods: registerSharedService, extendService#62igorw wants to merge 3 commits intosilexphp:masterfrom
Conversation
…ervice
registerSharedService addresses the common use case of:
$c['foo'] = $c->share(function ($c) {
return new Foo($c['bar']);
});
extendService is a shorthand for:
$c['foo'] = $c->share($c->extend('foo', function ($foo, $c) {
$foo->addBar(new Bar());
return $foo;
});
|
Interesting.
|
|
I wasn't so happy about the naming inconsistency myself, so maybe the best solution is indeed to just use the longer version of As for |
|
It's not really shorter with |
|
|
|
👍 for both though registerSharedService is maybe not something I would do often? It is a pretty common so I might end up finding it pretty useful. Thanks for including extendService. :) |
There was a problem hiding this comment.
Do we really need to pull this out of registerSharedService? Putting it back in there, would save polluting the public API with a method that is fairly useless?
|
I like My only gripe with the registerSharedService is that I'd have to type the FQCN. |
|
re FQCN: I agree that that sucks. We'll have to wait for PHP core to give us a definitive fix. I still think it's an improvement over what we have now. |
|
Just my two cents: I think one of the "coolness" factors of Pimple is that it's a solid DIC in ~200 lines of code. Short and sweet. I'm wondering if these could be implemented as Pimple traits? The |
@igorw Maybe that would be a good idea. It seems like this has stalled out and some people have mixed feelings on one or the other methods. I'm personally excited for |
|
@fabpot you haven't commented on this PR yet, can you please share your thoughts? |
|
Bump. |
|
Bump. |
|
I would to have this, with this method name (consistent) $c['foo'] = $c->shareExtend('foo', function ($foo, $c) {
$foo->addBar(new Bar());
return $foo;
});For the other case, i would prefere somethin like this $c['foo'] = $c->shareArray(array('bar'), array('setAnotherBar' => array('anotherbar'))); |
This PR was merged into the master branch. Discussion ---------- [2.0] changed services to be shared by default Now that we have several years of experience using Pimple, I think everyone agrees that having to call `share` everytime you need to define a service is cumbersome. And it is even error-prone when calling `extend` as one needs to call `share` again. #62 proposes a shortcut for the extend use case, but I want to go one step further in Pimple 2.0: remove the need to call `share` altogether. The reasoning is quite simple: services are 99.9% shared (I don't even have an example for a prototype service). So, this PR makes closure shared by default. If you need to define a prototype service, call `prototype`. This change is almost backward compatible as the `share` method is kept as a no-op method. The only BC break is if you have a prototype service in which case you need to wrap it with a `prototype` call. Of course, Pimple 2.0 will be used in Silex 2.0, which will make things much easier to read and shorter. TODO: - [x] update docs and website for 2.0 Commits ------- e5309ac changed services to be shared by default
|
Closing as I'm not convinced |
Registering a shared service for a certain class that has certain deps happens very often. I'd consider this the primary use case for pimple. As such, it makes sense to provide some shortcuts to make this easier to use.
registerSharedServiceaddresses the common use case of:The shorter version of that is:
The array of service ids is optional and can be omitted if the service constructor has no args.
While not such a common use case, when extending a service, you usually want to re-share and assign it. And the code we have to write for that is clunky and verbose. It would make sense to have a dedicated method that makes things easier.
extendServiceis a shorthand for:The new version is:
I'm open to splitting this PR if one of the proposed additions is controversial.