Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[EventDispatcher] Throw exception when listener method cannot be resolved#50775

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

Merged

Conversation

nikophil
Copy link
Contributor

@nikophilnikophil commentedJun 26, 2023
edited
Loading

QA
Branch?5.4
Bug fix?yes
New feature?no
Deprecations?no
LicenseMIT
Doc PRnot needed

while working on#50687 I've discovered bugs on how the method for listener is resolved:

Given this code:

#[AsEventListener(event: KernelEvents::REQUEST, priority:5)]class MyListenerNotInvokable{publicfunctionsomeMethod(RequestEvent$event):void {}}

A listener onMyListenerNotInvokable::onKernelRequest() is registered, and then an error is dispatched at runtime when the event is dispatched:Error: Call to undefined method App\EventListener\MyListenerNotInvokable::onKernelRequest()

The problem comes fromthis code: since themethod is omitted in tag definition, we "camelize" the event's name. We then try to fallback on__invoke but because it does not exist, the wrong method name is kept.

this PR fixes this behavior by throwing an exception at compile time if neither the camlized method exist, not the__invoke method.

ping@GromNaN

@carsonbotcarsonbot added this to the6.3 milestoneJun 26, 2023
@nikophilnikophil marked this pull request as draftJune 26, 2023 06:23
@nikophilnikophilforce-pushed thefix/resolve-event-listener branch from0319d14 to0434c2eCompareJune 26, 2023 06:30
@nikophilnikophil changed the title[EnventDispatcher] Fix method resolving for listeners[EnventDispatcher] Fix method resolving when it is omitted in tagJun 26, 2023
@nikophilnikophilforce-pushed thefix/resolve-event-listener branch 4 times, most recently frome3797d3 to838a2fcCompareJune 26, 2023 07:05
@nicolas-grekas
Copy link
Member

This should target 5.4 to me since it's a bugfix.

nikophil reacted with thumbs up emoji

@nikophilnikophilforce-pushed thefix/resolve-event-listener branch from838a2fc to1339e8dCompareJune 26, 2023 07:21
@nikophilnikophil changed the base branch from6.3 to5.4June 26, 2023 07:21
@nikophilnikophilforce-pushed thefix/resolve-event-listener branch from1339e8d to20ae84eCompareJune 26, 2023 07:22
@nikophilnikophil marked this pull request as ready for reviewJune 26, 2023 07:23
@carsonbotcarsonbot modified the milestones:6.3,5.4Jun 26, 2023
@symfonysymfony deleted a comment fromcarsonbotJun 26, 2023
@nikophil
Copy link
ContributorAuthor

@nicolas-grekas I've rebased the PR

@GromNaN
Copy link
Member

GromNaN commentedJun 26, 2023
edited
Loading

Thank you for your findings on this bug.

If it does not, we evaluate if one unique public method exists which would take the event as unique parameter (ie: the listener definition above would resolve someMethod() as the listener's method).

This looks like a new feature. Even if there is currently a runtime exception for this situation, I don't think we should support it as a bugfix.
The bugfix should be restricted to throwing an exception on build time when the method (__invoke oronEventName) doesn't exist.

I see situations where the "only public method" will fail and the developers will have to rework their configuration while it would be working previously:

  • a public getter or setter is added for DI or testing
  • a second event listener is added

For brevity, developers should set theAsEventListener attribute on the method instead of the class.

@nikophil
Copy link
ContributorAuthor

This looks like a new feature. Even if there is currently a runtime exception for this situation, I don't think we should support it as a bugfix.
The bugfix should be restricted to throwing an exception on build time when the method (__invoke or onEventName) doesn't exist.

ok let's do this: I'll split the PR and in the bugfix I'll only throw an exception if the resolved method does not exist

I see situations where the "only public method" will fail and the developers will have to rework their configuration while it would be working previously:

a public getter or setter is added for DI or testing
a second event listener is added

For brevity, developers should set the AsEventListener attribut on the method instead of the class.

indeed, there will be a failure in these cases, but for both cases, we don't have any way to resolve the method name, then their listener declaration becomes invalid. And since the error will occur in compile time, I think this is acceptable

@GromNaN
Copy link
Member

ok let's do this: I'll split the PR and in the bugfix I'll only throw an exception if the resolved method does not exist

👍🏻 Sounds like a plan.

nikophil reacted with thumbs up emoji

@nikophil
Copy link
ContributorAuthor

nikophil commentedJun 26, 2023
edited
Loading

For brevity, developers should set the AsEventListener attribut on the method instead of the class.

This is just an idea, but since listeners are callables and not actual services, maybe in 7.0 we should allow#[AsEventListener] to be set on the class only if the class in invokable? This would simplify a lot all this method-resolving stuff, and remove some un-needed magic

@GromNaN
Copy link
Member

I wouldn't bother developers with such a restriction. We're not going to deprecatedocumented way of using this attribute.

nikophil reacted with thumbs up emoji

@nicolas-grekas
Copy link
Member

nicolas-grekas commentedJun 26, 2023
edited
Loading

I'll split the PR and in the bugfix I'll only throw an exception if the resolved method does not exist

Totally. I missed this was a new feature.

This is just an idea, but since listeners are callables and not actual services, maybe in 7.0 we should allow #[AsEventListener] to be set on the class only if the class in invokable? This would simplify a lot all this method-resolving stuff, and remove some un-needed magic

I would advise against this. It'd be needlessly rigid.

nikophil reacted with thumbs up emoji

@kbondkbond changed the title[EnventDispatcher] Fix method resolving when it is omitted in tag[EventDispatcher] Fix method resolving when it is omitted in tagJun 26, 2023
@nikophilnikophilforce-pushed thefix/resolve-event-listener branch from20ae84e to36ef0b1CompareJune 26, 2023 15:25
Copy link
Member

@GromNaNGromNaN left a comment

Choose a reason for hiding this comment

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

LGTM, after few changes.

@nikophil
Copy link
ContributorAuthor

CI failures seems unrelated

Copy link
Member

@nicolas-grekasnicolas-grekas left a comment

Choose a reason for hiding this comment

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

One minor thing. Can you please also update the commit+PR messages to make them tell what this does?

@nikophilnikophil changed the title[EventDispatcher] Fix method resolving when it is omitted in tag[EventDispatcher] Throw exception when listener method cannot be guessedJun 27, 2023
@nikophilnikophilforce-pushed thefix/resolve-event-listener branch fromeff4526 to2b3497fCompareJune 27, 2023 09:59
@nikophilnikophil changed the title[EventDispatcher] Throw exception when listener method cannot be guessed[EventDispatcher] Throw exception when listener method cannot be resolvedJun 27, 2023
@nikophil
Copy link
ContributorAuthor

@nicolas-grekas done!

@nicolas-grekas
Copy link
Member

Thank you@nikophil.

@nicolas-grekasnicolas-grekas merged commita6e5b75 intosymfony:5.4Jun 28, 2023
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@nicolas-grekasnicolas-grekasnicolas-grekas approved these changes

@GromNaNGromNaNGromNaN approved these changes

Assignees
No one assigned
Projects
None yet
Milestone
5.4
Development

Successfully merging this pull request may close these issues.

5 participants
@nikophil@nicolas-grekas@GromNaN@kbond@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp