Skip to content

Introduce two new convenience methods: registerSharedService, extendService#62

Closed
igorw wants to merge 3 commits intosilexphp:masterfrom
igorw:convenience
Closed

Introduce two new convenience methods: registerSharedService, extendService#62
igorw wants to merge 3 commits intosilexphp:masterfrom
igorw:convenience

Conversation

@igorw
Copy link
Copy Markdown
Contributor

@igorw igorw commented Jan 13, 2013

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.

registerSharedService addresses the common use case of:

$c['foo'] = $c->share(function ($c) {
    return new Foo($c['bar']);
});

The shorter version of that is:

$c->registerSharedService('foo', 'Foo', array('bar'));

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.

extendService is a shorthand for:

$c['foo'] = $c->share($c->extend('foo', function ($foo, $c) {
    $foo->addBar(new Bar());
    return $foo;
}));

The new version is:

$c->extendService('foo', function ($foo, $c) {
    $foo->addBar(new Bar());
    return $foo;
});

I'm open to splitting this PR if one of the proposed additions is controversial.

igorw added 3 commits January 13, 2013 20:30
…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;
    });
@GromNaN
Copy link
Copy Markdown
Contributor

GromNaN commented Jan 13, 2013

Interesting.

  • extendService 👍 It avoid forgeting the share when extending a shared service. The extendService method may be named extendShared to match naming of other methods. An alternative could be to add a 3rd parameter to the extend method to make it shared.
  • registerSharedService 👎 I don't really see the gain of registerSharedService. If someone need to configure services with an array of options, he should use Symfony DI.

@igorw
Copy link
Copy Markdown
Contributor Author

igorw commented Jan 13, 2013

I wasn't so happy about the naming inconsistency myself, so maybe the best solution is indeed to just use the longer version of extendSharedService.

As for registerSharedService, I'm not sure I understand your concerns. There is nothing new added here, it's really just a shorter way of writing the exact same thing. And injecting parameters into services is the essence of DI.

@GromNaN
Copy link
Copy Markdown
Contributor

GromNaN commented Jan 13, 2013

It's not really shorter with registerSharedService, but it is really less explicit in context of Pimple.
The only use case I see is to load services from a config file (where you can't define Closures).

@dominikzogg
Copy link
Copy Markdown

extendService 👍
registerSharedService 👎 seems inconsistent to me, i prefere the closure

@simensen
Copy link
Copy Markdown

👍 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. :)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

@davedevelopment
Copy link
Copy Markdown
Contributor

I like extendService, but I think I'd call it sharedExtend, almost concatentating the $app->share($app->extend(.

My only gripe with the registerSharedService is that I'd have to type the FQCN.

@igorw
Copy link
Copy Markdown
Contributor Author

igorw commented Jan 14, 2013

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.

@ghost
Copy link
Copy Markdown

ghost commented Jan 14, 2013

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 registerSharedService could be Pimple\InfectionTrait or Pimple\ContagiousTrait as a couple of odd/funny examples. Cheers!

@simensen
Copy link
Copy Markdown

I'm open to splitting this PR if one of the proposed additions is controversial.

@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 extendShare or shareExtend. Would love to see if it would be merged in more quickly as its own PR.

@igorw
Copy link
Copy Markdown
Contributor Author

igorw commented Feb 12, 2013

@fabpot you haven't commented on this PR yet, can you please share your thoughts?

@igorw
Copy link
Copy Markdown
Contributor Author

igorw commented Mar 5, 2013

Bump.

@igorw
Copy link
Copy Markdown
Contributor Author

igorw commented Sep 26, 2013

Bump.

@dominikzogg
Copy link
Copy Markdown

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')));

fabpot added a commit that referenced this pull request Nov 11, 2013
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
@fabpot
Copy link
Copy Markdown
Member

fabpot commented Nov 11, 2013

Closing as I'm not convinced registerSharedService is a good thing, and extendService is not needed anymore in 2.0.

@fabpot fabpot closed this Nov 11, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

6 participants