Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork9.7k
[DependencyInjection] AddSubscribedService attribute, deprecate currentServiceSubscriberTrait usage#42238
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
weaverryan left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
I like this approach - we have decided not to useServiceSubscriberTrait in our site because, when trying to introduce it, one dev spent a long time trying to figure out a sudden autowiring error (it was because it was trying to wire a model class due to an unrelated method on the service that we did not want to be considered for the locator).
Uh oh!
There was an error while loading.Please reload this page.
| if (!$returnTypeinstanceof \ReflectionNamedType) { | ||
| // todo, what exception to throw? | ||
| thrownew \RuntimeException(sprintf('Cannot use "%s" on methods with a union return type in "%s::%s()".', SubscribedService::class,$method->name,self::class)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Why ? the previous loop does not have this restriction.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
I treated this as a bug. If the return type isService1|Service2, what should be used for the service locator?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Yes :). Is there an example of using union/intersection types withServiceSubscriberInterface?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
The example from the PR was an intersection of two serializer interfaces that would yield theserializer service, e.g.NormalizerInterface&DenormalizerInterface, so let's explore that:
class MyServiceimplements ServiceSubscriberInterface{use ServiceSubscriberTrait;privatefunctiongetSerializer():NormalizerInterface&DenormalizerInterface {return$this->container->get(__METHOD__); }}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Yep, I'll try to make that work. I'm looking for what the array returned fromServiceSubscriberInterface::getSubscribedServices() looks like when using union/intersection.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
I see. We need to come up with a new convention for that, I guess.
stof commentedJul 23, 2021
To make the trait usable in 5.4 without WTF, we would need a way to opt-in for the new behavior (here, there is nothing that will stop registering other private methods, with no way to avoid the deprecation warning if they are not meant to be subscribed) |
nicolas-grekas commentedJul 23, 2021
What about deprecating the trait instead? |
kbond commentedJul 23, 2021
What about, if using the attribute on any method, you've opted into the new way and methods without the attribute are ignored? |
kbond commentedJul 23, 2021 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
And creating a new trait entirely? Suggestions on a new name? |
stof commentedJul 23, 2021
Well, providing an easy implemention of the |
nicolas-grekas commentedJul 23, 2021
I was considering deprecating the trait and not replacing it, but if you think we should keep it, no need to create a new one if we can make a nice deprecation layer. |
kbond commentedJul 23, 2021 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
I personally like this feature so I think we should keep it but I understand it's a bit... controversial (and could be easily abused). If you're ok keeping it, I'll add the nice deprecation layer and finish this PR. Alternatively, if you want to deprecate/remove, I can easily maintain this feature in a 3rd party package. |
nicolas-grekas commentedJul 23, 2021
I'm fine keeping it now that we have it. |
07877ad tofee84f7Comparekbond commentedJul 23, 2021
I've updated/added tests and improved the deprecation layer:
|
kbond commentedJul 23, 2021
I could use some direction on fixing the remaining CI errors. |
SubscribedService attribute, deprecate currentServiceSubscriberTrait usageUh oh!
There was an error while loading.Please reload this page.
54535f1 to1cbc600Comparekbond commentedOct 20, 2021
Assuming this is still wanted for 5.4, once merged, I'll work on a PR to drop the deprecation layer in 6.0. Still need a bit of direction for fixing the tests. I believe it's a dependency issue, |
nicolas-grekas left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
ideally tests on DI should work on all versions of contracts, or be skipped on non-supported ones
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
7afa27c to716b275CompareUh oh!
There was an error while loading.Please reload this page.
716b275 toa5bb886Comparekbond commentedOct 20, 2021
I believe all related tests are now passing. I fixed some pslam issues but don't understand the remaining ones. |
nicolas-grekas left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
one last non-trivial note :)
Uh oh!
There was an error while loading.Please reload this page.
| thrownew \LogicException(sprintf('Cannot use "%s" on methods without a return type in "%s::%s()".', SubscribedService::class,$method->name,self::class)); | ||
| } | ||
| $services[$attribute->newInstance()->key ??self::class.'::'.$method->name] ='?'.$returnType->getName(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
this should likely do$returnType instanceof \ReflectionNamedType ? ->getName() : (string) for the last part.
also, when the attribute is used, I'm not sure about the? prefix. Shouldn't we add it only when the type is nullable? (and remove|null from the string representation of the union)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
this should likely do $returnType instanceof \ReflectionNamedType ? ->getName() : (string) for the last part.
Like this?
$services[$attribute->newInstance()->key ??self::class.'::'.$method->name] ='?'.($returnTypeinstanceof \ReflectionNamedType ?$returnType->getName() :$returnType);
I just realizedReflectionType::__toString isdeprecated.. Should we be relying on this?
also, when the attribute is used, I'm not sure about the ? prefix. Shouldn't we add it only when the type is nullable? (and remove |null from the string representation of the union)
Good call, I'll make this change. Should I leave the BC layer as is?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
I've made these changes (let me know if you'd like me to backport this to the BC layer).
I'm not sure how to handle union/intersection types. See#42238 (comment).
Uh oh!
There was an error while loading.Please reload this page.
a5bb886 to2fa213fCompare
nicolas-grekas left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
(with minor comments)
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
f045e22 to3cef7b7Compare…rrent `ServiceSubscriberTrait` usage
3cef7b7 to4a7aa8eComparenicolas-grekas commentedOct 27, 2021
Thank you@kbond. |
kbond commentedOct 27, 2021
Great, once this makes its way into the 6.0 branch, I'll remove the deprecation layer. |
nicolas-grekas commentedOct 27, 2021
now merged up to 6.0 |
…precation layer (kbond)This PR was merged into the 6.0 branch.Discussion----------[DependencyInjection] remove `ServiceSubscriberTrait` deprecation layer| Q | A| ------------- | ---| Branch? | 6.0| Bug fix? | no| New feature? | no| Deprecations? | no| Tickets | Followup to#42238| License | MIT| Doc PR | TODOThis removes the deprecation layer we added in#42238.Commits-------4a6226d [DependencyInjection] remove ServiceSubscriberTrait deprecation layer
Uh oh!
There was an error while loading.Please reload this page.
This is a fix for#42217 using what I proposed there (adding
SubscribedServiceattribute). The current usage is deprecated in 5.4 only when using PHP 8+. I suggest removing the current usage entirely in Symfony 6.0 - this willfully fix the bug.Deprecation Layer:
SubscribedServiceattribute, works as it does currently but a deprecation warning is triggered.SubscribedServiceattribute, you've opted-into the new functionality (methods w/o the attribute are ignored).