-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[EventDispatcher] swap arguments of dispatch() to allow registering events by FQCN #28920
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
09246f2
to
a9abb1c
Compare
I have some questions
|
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.
Some test failures are caused by this change, please fix. Other than that, this is pretty clever, I like it.
src/Symfony/Component/EventDispatcher/EventDispatcherInterface.php
Outdated
Show resolved
Hide resolved
26df57b
to
b3d854a
Compare
Thanks for the early reviews. I still have a few edges to fix (as spotted by the CI).
two sides: allowing one to subscribe to events using FQCN:
that's what happens now :)
at least for BC, to map |
I prefer this new implementation. This new DX is better. |
b3d854a
to
001dba6
Compare
src/Symfony/Component/EventDispatcher/LegacyEventDispatcherProxy.php
Outdated
Show resolved
Hide resolved
The aliases being compile-time only mean that any code meant to work with components only and not FrameworkBundle cannot use them, and that Form aliases are useless (as these are not dispatched on the dispatcher configured by FrameworkBundle as each Form instance has its own EventDispatcher) |
Idea: look at listeners bound to the interfaces of an event. |
I like this step a lot. This was one of my "favorite" drawback of the I can share some thoughts/ideas of my event handling experience, probably it may help to improve the component. I tried to not discuss it here because you are locked with BC policies. But if you want to change something... I think you are aware of debates regarding using term "event" for this kind of service. In my opinion, Symfony provides hook dispatcher/manager, not event one. You can say that it's just matter of taste, but it has architecture impacts. You lose ability to make async event handlers without evidence that abstraction is very leaky. It was discussed here, for example: #23441. And regarding naming here, for instance: https://twitter.com/mathiasverraes/status/1013654989682741248. Probably, you will never change it, but it's very noticeable thing that comes in mind when we talk about this component. This little "terminology disagreement" leads to 2 completely different concepts of EventDispatcher/Bus. "That" event bus:
Naming matters.
I prefer the following logic:
P.S. Avoiding string identifiers for events gives another advantage: developers start to be more reasonable regarding event classes and make them more "fine-grained": not |
@unkind I think you confuse two concepts here, the Symfony Event Dispatcher is based on the https://en.wikipedia.org/wiki/Mediator_pattern which allows "events" to be mutable, it's naming is definitely off :) changing this fundamental logic is a major BC break, instead Symfony would have to introduce another component or class that truly implements immutable events. Async handling requires more work as you need to handle serialization and transportation which the Messenger component already solves nicely. Yes, you don't have priorities, but this feature could be added. |
What @unkind describes here looks like the Messenger component 😄 |
I'm not sure about this, I explicitly stated that
It was just a little rant about naming in the first place. Naming disregard is a problem. |
001dba6
to
d581c40
Compare
01531f3
to
b2406c3
Compare
b2406c3
to
75369da
Compare
Thank you @nicolas-grekas. |
…w registering events by FQCN (nicolas-grekas) This PR was merged into the 4.3-dev branch. Discussion ---------- [EventDispatcher] swap arguments of dispatch() to allow registering events by FQCN | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | yes | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - PR green and ready. From UPGRADE files: EventDispatcher --------------- * The signature of the `EventDispatcherInterface::dispatch()` method should be updated to `dispatch($event, string $eventName = null)`, not doing so is deprecated HttpKernel ---------- * Renamed `FilterControllerArgumentsEvent` to `ControllerArgumentsEvent` * Renamed `FilterControllerEvent` to `ControllerEvent` * Renamed `FilterResponseEvent` to `ResponseEvent` * Renamed `GetResponseEvent` to `RequestEvent` * Renamed `GetResponseForControllerResultEvent` to `ViewEvent` * Renamed `GetResponseForExceptionEvent` to `ExceptionEvent` * Renamed `PostResponseEvent` to `TerminateEvent` Security --------- * The `ListenerInterface` is deprecated, turn your listeners into callables instead. * The `Firewall::handleRequest()` method is deprecated, use `Firewall::callListeners()` instead. Commits ------- 75369da [EventDispatcher] swap arguments of dispatch() to allow registering events by FQCN
namespace Symfony\Component\Workflow; | ||
|
||
/** | ||
* To learn more about how form events work check the documentation |
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.
Copy paste error: form events
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.
fixed in b8ab296 thanks
If people are asking, this is how we define event listeners (that use event FQCN) in services:
App\EventListener\MyEventListener:
tags:
- { name: kernel.event_listener, event: 'App\Event\MyEvent', method: onEvent } $eventDispatcher->dispatch(new App\Event\MyEvent()); |
This PR was squashed before being merged into the 4.3 branch (closes #12007). Discussion ---------- Use new Event names Based on symfony/symfony#28920 @symfony/team-symfony-docs shall we add `versionadded` directives? Commits ------- 00f3807 Use new Event names
…lexislefebvre) This PR was merged into the 4.3 branch. Discussion ---------- [4.3] Update UPGRADE guide of 4.3 for EventDispatcher | Q | A | ------------- | --- | Branch? | 4.3 | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | no | License | MIT | Doc PR | no I updated `UPGRADE-4.3.md` to ease upgrading `EventDispatcher` to 4.3, related changes were introduced in #28920 This code is used in Symfony: https://github.com/symfony/symfony/blob/f8302262714674e3efbd775c9b107294d89fb77e/src/Symfony/Component/Security/Guard/GuardAuthenticatorHandler.php#L49 Thanks to @nicolas-grekas for the hint: 75369da#r34480203 Commits ------- 8b9b39d Update UPGRADE guide of 4.3 for EventDispatcher
… (derrabus) This PR was merged into the 5.1-dev branch. Discussion ---------- [EventDispatcher] Deprecate LegacyEventDispatcherProxy | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | no | Deprecations? | yes | Tickets | Cleanup of #28920 | License | MIT | Doc PR | N/A > This class should be deprecated in Symfony 5.1 Well, here you go. 😃 Commits ------- c7e612d [EventDispatcher] Deprecate LegacyEventDispatcherProxy.
$listeners = $this->getListeners($eventName); | ||
|
||
foreach ($listeners as $listener) { | ||
if ($event->isPropagationStopped()) { | ||
break; | ||
} | ||
$listener($event, $eventName, $this); | ||
} | ||
|
||
return $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.
Old news, but shouldn't this just be a call to dispatch with the args swapped? What if there's custom logic in the decorated dispatcher's dispatch method that is no longer called?
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.
These lines should be replaced with return $this->dispatch($eventName, $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.
@nicolas-grekas @fabpot this is technically a BC break and should be fixed in the next release of 4 (assuming there is one)
PR green and ready.
Bundles/packages that want to provide FC/BC compat with the previous way of using the dispatcher should use
LegacyEventDispatcherProxy::decorate()
(see example in the PR)From UPGRADE files:
EventDispatcher
EventDispatcherInterface::dispatch()
method should be updated todispatch($event, string $eventName = null)
, not doing so is deprecatedHttpKernel
FilterControllerArgumentsEvent
toControllerArgumentsEvent
FilterControllerEvent
toControllerEvent
FilterResponseEvent
toResponseEvent
GetResponseEvent
toRequestEvent
GetResponseForControllerResultEvent
toViewEvent
GetResponseForExceptionEvent
toExceptionEvent
PostResponseEvent
toTerminateEvent
Security
ListenerInterface
is deprecated, turn your listeners into callables instead.Firewall::handleRequest()
method is deprecated, useFirewall::callListeners()
instead.