You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
minor #50775 [EventDispatcher] Throw exception when listener method cannot be resolved (nikophil)
This PR was merged into the 5.4 branch.
Discussion
----------
[EventDispatcher] Throw exception when listener method cannot be resolved
| Q | A
| ------------- | ---
| Branch? | 5.4
| Bug fix? | yes
| New feature? | no
| Deprecations? | no
| License | MIT
| Doc PR | not needed
while working on #50687 I've discovered bugs on how the method for listener is resolved:
Given this code:
```php
#[AsEventListener(event: KernelEvents::REQUEST, priority: 5)]
class MyListenerNotInvokable
{
public function someMethod(RequestEvent $event): void {}
}
```
A listener on `MyListenerNotInvokable::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 from [this code](https://github.com/symfony/symfony/blob/6.4/src/Symfony/Component/EventDispatcher/DependencyInjection/RegisterListenersPass.php#L85-L95): since the `method` 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`
Commits
-------
2b3497f [EventDispatcher] [EventDispatcher] Throw exception when listener method cannot be resolved
thrownewInvalidArgumentException(sprintf('None of the "%s" or "__invoke" methods exist for the service "foo". Please define the "method" attribute on "kernel.event_listener" tags.', $event['method'], $id, $this->listenerTag));
$this->expectExceptionMessage('None of the "onFooBar" or "__invoke" methods exist for the service "foo". Please define the "method" attribute on "kernel.event_listener" tags.');
0 commit comments