-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[EventDispatcher] Added EventTrait #21827
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
Conversation
This allows to easily make any custom class fit for EventDispatcher
I'm 👎 for such a change. |
Do you mind explaining why? Personally I would be satisfied with just removing the typehint against the Event class, but that's probably not going to happen. I've never seen anybody use the stopPropagation feature of this component. Current implementation forces us to break DRY. Every time we want to dispatch something, we need to wrap the object we really care about to custom Event class (just so it fullfills EventDispatcher contract) , in event listener we then have to manually call the method to retrieve such object from the wrapper and manually check for correct type retrieved from Event class. |
See the example here: #18873. |
Bear in mind that BC break was added by a bot only because I forgot to change the default yes/no value |
*/ | ||
public function dispatch($eventName, Event $event = null); | ||
public function dispatch($eventName, EventInterface $event = null); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gadelat In fact, changing the type hint here is a BC break.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How so? Users pass the event class here, which implements this interface.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Every class implementing this interface must be updated too (see the fatal error you will get otherwise: https://3v4l.org/CQgFB).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's true :( Are the downvotes then only because of this BC, or is there something else? Would you be more keen to merge this at least in 4.0?
I use it frequently (see e.g. lexik/LexikJWTAuthenticationBundle:JWTDecodedEvent#L47), like anyone setting the response of a |
@gadelat
It seems like Symfony's contract doesn't satisfy your needs. namespace Gadelat;
interface EventDispatcher
{
public function dispatch(Event $event);
}
interface Event
{
} and use Symfony EventDispatcher as an infrastructure. I agree that |
If someone stumbles here and find he needs same thing, I've created separate event dispatcher library which doesn't restrict you in this way and is more appropriate for use cases like these https://github.com/ostrolucky/app-event-dispatcher |
This allows to easily make any custom class fit for EventDispatcher
I would like to use this to make some of our Doctrine entities (or some other object) events. Currently we have to encapsulate them to some Event class which means we cannot easily typehint against them in our listeners.
Let me know your suggestions regarding deprecations, this patch touching multiple components, backporting and changing the implementation also for current children of Event class. I will change the patch accordingly.