Currently #[AsEventListener] only marks methods as implicitly used when the attribute is placed on the class and has an explicit method argument. But in my experience that's not the most common usage of the attribute and both the documentation and unit tests (1, 2) show multiple possibilities, which are:
#[AsEventListener] on the method itself (I believe the most common)
#[AsEventListener] on the class with an explicit method specified
#[AsEventListener] on a class which has an __invoke() method
#[AsEventListener] on a class with an event specified but no method, in which case the method name is automatically determined based on the event name (on<event name>)
Currently
#[AsEventListener]
only marks methods as implicitly used when the attribute is placed on the class and has an explicitmethod
argument. But in my experience that's not the most common usage of the attribute and both the documentation and unit tests (1, 2) show multiple possibilities, which are:#[AsEventListener]
on the method itself (I believe the most common)#[AsEventListener]
on the class with an explicitmethod
specified#[AsEventListener]
on a class which has an__invoke()
method#[AsEventListener]
on a class with an event specified but no method, in which case the method name is automatically determined based on the event name (on<event name>
)Currently only 2 is supported. This adds support for 1, 3 and 4 as well. The logic for this (including the regular expressions) is reused from Symfonys code here: https://github.com/symfony/symfony/blob/82acd7a7d6fe7a12200e949b6268ce5ba13e5e74/src/Symfony/Component/EventDispatcher/DependencyInjection/RegisterListenersPass.php#L71-L96