-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[EventDispatcher] Access listener's priority. #11263
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
Comments
an event does not have a priority. Listeners have priority |
True! I wasn't sure though. Worth renaming to "listener's priority" then? |
+1 on this. As it stands right now there is no way to inspect any given implementation of EventDispatcherInterface and see a listener's priority. You can request getListeners(), but it is simply an array of the listeners with no guarantee of priority being the array key (in fact, it is impossible to simply indicate the priority as an array key anyway, since multiple listeners can have the same priority). I think the good solutions to this require a BC break (on the interface and the default implementation) to change the getListeners() return contract. Essentially, we would have to force retention of priority on the EventDispatcherInterface. One way to do this is to change the EventDispatcherInterface to be explicit regarding the array structure that is returned by getListeners() that MUST expose priorities of listeners... <?php
array(
"event.name" => array(
10 => array(
"listener1",
"listener2",
"etc"
)
)
) Internally, the current implementation of EventDispatcher actually stores the listeners like the code above, but it then does a bunch of logic to sort it (and flatten it) before it returns it to you in ->getListeners(). It would be much better from an introspection point of view if we could get that array without first removing the information of the set priorities number. Any other ideas? |
I must admit, that I don't really understand the use case. What should this be good for, except debugging? I mean a listener shouldn't need to know any other listener, least of all their priority. Only the dispatcher needs to know. This said I don't think a BC break is reasonable... However, have you seen egulias/listeners-debug-command-bundle already? It provides a command |
It's true that this is pretty much only for debugging. There could be some uses in a situation where you might want to dynamically add an event to the dispatcher above or below an already registered event (which you can't do without knowing its priority). That being said, I think you are right in that it's not worth a BC-break, especially since the use cases are so small / rare. It's purely a "DX" ticket. Often I need to create an event but I am unsure what priority to set it as. If I could just see a list of the listeners and their priority number, I could get it to the right priority without a bunch of trial and error. Thanks for that bundle link, I definitely did not know about it. |
Ping @egulias |
Thanks @aferrandini for the ping. There is no need of a BC to implement this, we don't need to modify the interface. |
@ManInTheBox, if you're trying to implement a |
any news on this one ? |
I'm closing this issue as "fixed". As some people said, you only need the listener priority for debug purposes. The redesigned profiler already includes this information in the events panel. |
I believe currently there is no way (or at least easy way) to access event's priority. Priority is set only when registering all listeners and developer is not able to retrieve that value by simply calling for example
$event->getPriority()
.I noticed this while trying to expose event's priority to profilers' "events" panel. I wanted to add one more column in order to easily see event's priority passed to listed listeners. Moreover I wanted to add sorting ability to that rightmost column so I can easily sort called listeners in reverse order.
Currently if you want to see event's priority you need to:
a) If listener is "a subscriber" you just need to look at your implementation of
EventSubscriber:: getSubscribedEvents()
and priority will be there (or0
if omitted) <- easier wayb) If listener is just simple service tagged by
kernel.event_listener
then you need to search for its service definition and then you need to look forpriority
attribute (or0
if omitted) <- harder wayI'm still not sure if this was intentional by design or could be improved. Also I think this issue can be tagged with DX label since it can improve how developer see registered listeners on profiler's "events" panel. Sometimes it is crucial to have control over event's priorities. Especially if your listener overlaps with some other listener. /cc @javiereguiluz
The text was updated successfully, but these errors were encountered: