Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[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

Closed
ManInTheBox opened this issue Jul 1, 2014 · 10 comments
Closed

[EventDispatcher] Access listener's priority. #11263

ManInTheBox opened this issue Jul 1, 2014 · 10 comments
Labels
DX DX = Developer eXperience (anything that improves the experience of using Symfony) EventDispatcher

Comments

@ManInTheBox
Copy link

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 (or 0 if omitted) <- easier way
b) 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 for priority attribute (or 0 if omitted) <- harder way

I'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

@stof
Copy link
Member

stof commented Jul 1, 2014

an event does not have a priority. Listeners have priority

@ManInTheBox
Copy link
Author

True! I wasn't sure though. Worth renaming to "listener's priority" then?

@christickner
Copy link

+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?

@kingcrunch
Copy link
Contributor

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 container:debug:listeners, that returns every known listener including their priorities. Maybe that is already enough for your purpose. If not, have a look at its code. It extracts the relevant information directly out of the container, what makes sense, because that's where they come from.
Just reuse, what is already there: https://github.com/egulias/ListenersDebug/blob/master/Listener/ListenerFetcher.php#L22

@christickner
Copy link

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.

@aferrandini
Copy link
Contributor

Ping @egulias

@egulias
Copy link
Contributor

egulias commented Aug 26, 2014

Thanks @aferrandini for the ping.
HI @christickner , the use case during development may not be so small if you have a heavy listeners infrastructure like e.g eZPublish who has the command required by default.
That being said, as @kingcrunch stated you can use the library inside your listeners. Beware that inspecting the whole container can be an expensive task to do during a request, I would try to avoid it. Or just doing it once, with a CompilerPass.

There is no need of a BC to implement this, we don't need to modify the interface.

@kix
Copy link
Contributor

kix commented Oct 3, 2014

@ManInTheBox, if you're trying to implement a Priority column in the profiler, then you could cover #11915 too.

@dupuchba
Copy link
Contributor

any news on this one ?

@jakzal jakzal changed the title [EventDispatcher] Access event's priority. [EventDispatcher] Access listener's priority. Apr 16, 2015
jelte pushed a commit to jelte/symfony that referenced this issue Jun 12, 2015
@javiereguiluz
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
DX DX = Developer eXperience (anything that improves the experience of using Symfony) EventDispatcher
Projects
None yet
Development

No branches or pull requests

10 participants