-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[EventDispatcher] Allow EventDispatcher to dispatch anonymous events #21962
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
{ | ||
if (null === $event) { | ||
$event = new Event(); | ||
} | ||
|
||
if (!is_object($event)) { |
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.
Why even forcing an object
as a type?
There was similar ticket few days ago: #21827 As I said in previous ticket, the Symfony EventDispatcher has its own philosophy, if it doesn't fit your needs, then it's better to change the tool. |
My intention was to maintain the same philosophy, but allowing us to dispatch other objets. I think that It would make the component more attractive to be used as standalone. I'm currently familiar with other tools, I was just trying to help. Thanks for your time and links! |
This won't be merged anyway, because changing interface signature is a BC break. However, it might be possible to raise the question about |
Dunno if another approach is possible, but as @unkind explained, this is a BC break, so no go. |
Who says this have to be merged in 3.x. What's the procedure for submitting BC break features? In this case would we have to create a copy of EventDispatcher class, implement new interface and deprecate the old one? |
@gadelat I guess the implementation should be reworked to make it work with new methods/interfaces (and deprecate the old). |
- Added AnonymousEventDispatcherInterface to allow event dispatching without the need of specifying a name - Added EventPropagationTrackerInterface to remove the need of anonymous events to be coupled to Event class - Implemented new interfaces in EventDispatcher
Thank you all for your comments. To achieve the goals I have worked in another solution. For more details look at the updated PR description. |
public function stopPropagation($event) | ||
{ | ||
if (!$this->isPropagationStopped($event)) { | ||
$this->stoppedEvents[] = $event; |
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.
making the EventDispatcher stateful is a nogo (this is leaking memory by keeping a reference to the object btw)
*/ | ||
public function isPropagationStopped($event) | ||
{ | ||
return in_array($event, $this->stoppedEvents); |
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.
comparing the whole object graph for equality is a nightmare for performance
* | ||
* @param object $event The event to pass to the event handlers/listeners | ||
*/ | ||
public function dispatchAnonymousEvent($event); |
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.
why removing the event name ? This limits the use cases, as it forces creating a new class for each place dispatching an event as the event class is used as event identifier.
I'm -1 for the removal of event names. This would make the dispatcher unusable for Symfony itself in several cases (all form events are using the same FormEvent class for the object for instance). And the way stopping the propagation is reimplemented is worse than currently:
|
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 |
Feture description
This PR allows
EventDispatcher
to dispatch anonymous events. Internally the fully qualified event object class name is used.To add a listener or subscribe to an anonymous event, simply use the event object fully qualified name:
To dispatch an anonymous event:
To stop event propagation:
I have removed the coupling to
Event
class for these reasons: