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

Skip to content

[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

Closed
wants to merge 1 commit into from

Conversation

ostrolucky
Copy link
Contributor

@ostrolucky ostrolucky commented Mar 1, 2017

This allows to easily make any custom class fit for EventDispatcher

Q A
Branch? master
Bug fix? no
New feature? yes
BC breaks? yes/no
Deprecations? no
Tests pass? yes
Fixed tickets -
License MIT
Doc PR -

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.

This allows to easily make any custom class fit for EventDispatcher
@fabpot
Copy link
Member

fabpot commented Mar 1, 2017

I'm 👎 for such a change.

@ostrolucky
Copy link
Contributor Author

ostrolucky commented Mar 1, 2017

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.

@unkind
Copy link
Contributor

unkind commented Mar 1, 2017

Currently we have to encapsulate them to some Event class which means we cannot easily typehint against them in our listeners.

See the example here: #18873.

@ostrolucky
Copy link
Contributor Author

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);
Copy link
Member

@xabbuh xabbuh Mar 3, 2017

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.

Copy link
Contributor Author

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.

Copy link
Member

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).

Copy link
Contributor Author

@ostrolucky ostrolucky Mar 3, 2017

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?

@chalasr
Copy link
Member

chalasr commented Mar 3, 2017

I've never seen anybody use the stopPropagation feature of this component.

I use it frequently (see e.g. lexik/LexikJWTAuthenticationBundle:JWTDecodedEvent#L47), like anyone setting the response of a kernel.request event since GetResponseEvent::setResponse() does stopPropagation().

@unkind
Copy link
Contributor

unkind commented Mar 3, 2017

@gadelat

I would like to use this to make some of our Doctrine entities (or some other object) events
I've never seen anybody use the stopPropagation feature of this component.

It seems like Symfony's contract doesn't satisfy your needs.
If you couple to a framework, you pay a price: you have to deal with their flaws.
You can make your own contract that would perfectly fit your needs, e.g.

namespace Gadelat;

interface EventDispatcher
{
    public function dispatch(Event $event);
}

interface Event
{
}

and use Symfony EventDispatcher as an infrastructure.

I agree that stopPropagation is a problem sometimes, events shouldn't contain this method, because it turns them into hooks. I explained my point here: php-fig/fig-standards#766 (in short, stopPropagation is not bad per se, but it should be separated from events).

@ostrolucky
Copy link
Contributor Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants