-
-
Notifications
You must be signed in to change notification settings - Fork 59
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
Improve Delegator Documentation Specifically for Aliased Services #90
Comments
Somewhere I pointed that out as well. AFAIR that was in some of the plugin manager issues where one changed old behavior ( Didn't read all of the comments in that PR but I personally cannot remember on why this did not work for aliases. Maybe, we can add that missing feature tho. |
It doesn't work on aliases because delegators are applied after the alias has already been resolved, and the alias isn't known in the scope in which the delegators are applied. If an alias is in play, it is resolved, and then https://github.com/laminas/laminas-servicemanager/blob/3.7.x/src/ServiceManager.php#L224 is invoked - with the resolved name. It's in the |
It would be handy (for me at least) to have delegators applied to aliases - My use case is that I use aliases to hide an implementation like a PSR18 Http Client for example. If I want to, for example, wrap whatever client is configured in another implementation that logs requests for example, I also have to know up-front which implementation/service name I need to target in order to execute the delegator. For now though, I just wanted to mention how it works in the docs! |
@weierophinney we know in |
@boesing correct - but that would require passing the alias to |
Once you have the resolved name and you're ready to |
I guess that could work, yes. |
Oof, I'm not sure I like that idea. There's basically two ways to think of aliases.
Right now, we are supporting (1) in laminas-servicemanager. I think what you opened in this thread is the idea of moving to (2), which would be a BC break if we make it work as I suggest. |
I can see that there's a lot of potential here for bc breaks and excessive complexity so I'm not advocating for any change, but the discussion is interesting. I wouldn't like to attempt a change like this and I think it would be reasonable to assume a fair performance penalty if implemented. I will stick to the original plan though and send in a pull to make a note in the documentation when I get a chance. 😁 |
Well, I'd love to have that feature on aliases aswell. I have a concrete example here: interface ClientInterface extends \Psr\Client\ClientInterface
{}
^ This is actually not possible, so I add that delegator to the TBH, I dont see any BC break as either We could provide a configuration toggle which enables this new behavior while make that default behavior in v4 and remove that config toggle again. |
@boesing Let me make sure I understand: you want the delegators for I ask, because the instead of part is fine, and it's something I'd support. However, it's not the current behavior; the current behavior is to run delegators and intializers only for the resolved service name, and we don't currently store separate instances for aliases versus the resolved service name. I think we could likely do something like this as a feature toggle, though, during instantiation of the SM instance. (This is easy to accomplish in Mezzio, as you would do it in the The in addition to scenario would be a nightmare. The reason is because a user would need to know the entire inheritance tree in order to determine which delegators/initilizers trigger, and in which order. It would also require us to store instances per name, because otherwise you potentially have delegators on a child service operating on the same instance as a service pulled from a parent, and you get unexpected behavior. I think this approach (a) adds a ton of complexity to the implementation, (b) makes understanding the lifecycle of creating an instance too difficult, and (c) is likely to lead to a lot of WTF moments when debugging. |
…or of delegators when applied to aliases is made clear
…or of delegators when applied to aliases is made clear Signed-off-by: George Steel <[email protected]>
@weierophinney when alias is requested, the alias delegators + those from the service the alias is pointing to should be applied. If the service is requested explicitly, only those from the Service should be applied. Same for the initializers. |
See my notes above; I think this will cause confusion for users. In particular, what if the expectation is that the alias delegators are the only ones applied? What if they do not want other delegators applied? It's exactly these sorts of questions that lead to @Ocramius suggesting that aliases should be abolished entirely, in favor of just assigning a factory directly to the "alias". The benefit of that particular approach is that everything then becomes explicit: if you want both a set of delegators for the alias as well as for the resolved service name, you specify the full list to the "alias" service, and the partial list to the resolved service name, and you get two instances with two different behaviors, all done with factories: [
'factories' => [
ResolvedService::class => ResolvedServiceFactory::class,
AliasedService::class => ResolvedServiceFactory::class,
],
'delegators' => [
ResolvedService::class => [
Delegator1ForResolvedService::class,
Delegator2ForResolvedService::class,
],
AliasedService::class => [
Delegator1ForResolvedService::class,
Delegator2ForResolvedService::class,
DelegatorForAliasedService::class,
],
]; What's even better about the above approach is that you can explicitly state the order in which they should apply. I can choose to apply the delegators for the alias before or after those I'd normally use with the resolved service name, or intersperse between, or override completely. (This would work the same for initializers. But, honestly, initializers really aren't necessary when you have delegators available.) Generally speaking, I think we should favor explicit definitions over implied workflows. |
…or of delegators when applied to aliases is made clear Signed-off-by: George Steel <[email protected]>
…or of delegators when applied to aliases is made clear Signed-off-by: George Steel <[email protected]>
RFC
Goal
Update docs to state that delegators cannot target service aliases.
Background
For some reason, I personally can never remember that you can't wrap a service alias with a delegator - you must target the service name that corresponds to concrete factory.
Because this frequently trips me up, I'd like to submit a pull request to update the docs at delegators.md - so I hope it's OK to leave this issue here as a reminder for myself to do this when I have time…
FWIW, I get that the point of delegators is to modify instantiation logic, but what is the primary reason that this does not apply to aliases (If anyone has the time to comment)??
The text was updated successfully, but these errors were encountered: