Skip to content

[EventDispatcher] A compiler pass for aliased userland events #33793

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

Merged

Conversation

derrabus
Copy link
Member

@derrabus derrabus commented Oct 1, 2019

Q A
Branch? 4.4
Bug fix? no
New feature? yes
Deprecations? no
Tickets N/A
License MIT
Doc PR symfony/symfony-docs#12420

Since 4.3, the EventDispatcher component allows to register events via the FQCN of the class name instead of a dedicated event name.

Earlier this year I have worked with a team that used the event dispatcher for own custom events. When 4.3 was released, the team decided to use the new mechanism for new events. For the sake of consistency, we also wanted to migrate existing event subscribers to FQCN events.

While FrameworkBundle implements a nice aliasing mechanism for its own events, we couldn't find an obvious way to make use of FQCN event aliases for our own events. The best way we could find is registering a compiler pass that would extend an internal parameter that stores all event aliases. But that made us feel like we're fiddling with an implementation detail of the framework.

This PR aims to provide a standard way for applications and third-party bundles to register their own event aliases.

$container->addCompilerPass(new AddEventAliasesPass([
    MyCustomEvent::class => 'my_custom_event',
]));

Furthermore, it adds tests for class aliasing to the component's test suite. Additionally, the newly introduced pass is dogfooded by the SecurityBundle, so FrameworkBundle doesn't need to know about events fired by the security components.

@@ -78,6 +78,7 @@
"symfony/messenger": "<4.3",
"symfony/mime": "<4.4",
"symfony/property-info": "<3.4",
"symfony/security-bundle": "<4.4",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we actually need such conflict rule ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you have a Symfony 4.3 application with an event subscriber registered to, let's say, SwitchUserEvent::class, that application would break if you upgraded FrameworkBundle to 4.4 without upgrading SecurityBundle as well.

@derrabus
Copy link
Member Author

derrabus commented Oct 1, 2019

I've backported the new tests to 4.3: #33795

@nicolas-grekas nicolas-grekas added this to the next milestone Oct 2, 2019
nicolas-grekas added a commit that referenced this pull request Oct 2, 2019
This PR was merged into the 4.3 branch.

Discussion
----------

[EventDispatcher] Added tests for aliased events

| Q             | A
| ------------- | ---
| Branch?       | 4.3
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | N/A
| License       | MIT
| Doc PR        | N/A

While working on #33793 I discovered that I could remove the event alias feature of `RegisterListenersPass` without breaking the component's tests. This PR adds the missing tests.

Commits
-------

8e8a6ed [EventDispatcher] Added tests for aliased events.
@nicolas-grekas
Copy link
Member

(rebase needed)

@derrabus derrabus force-pushed the improvement/custom-aliased-events branch 3 times, most recently from 6a93842 to 23f570d Compare October 2, 2019 20:05
@nicolas-grekas
Copy link
Member

can you please update the CHANGELOG of the component?

@derrabus derrabus force-pushed the improvement/custom-aliased-events branch from 23f570d to 34efe40 Compare October 4, 2019 11:01
@derrabus
Copy link
Member Author

derrabus commented Oct 4, 2019

can you please update the CHANGELOG of the component?

@nicolas-grekas
Copy link
Member

Thank you @derrabus.

nicolas-grekas added a commit that referenced this pull request Oct 4, 2019
… events (derrabus)

This PR was merged into the 4.4 branch.

Discussion
----------

[EventDispatcher] A compiler pass for aliased userland events

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       | N/A
| License       | MIT
| Doc PR        | TODO

Since 4.3, the EventDispatcher component allows to register events via the FQCN of the class name instead of a dedicated event name.

Earlier this year I have worked with a team that used the event dispatcher for own custom events. When 4.3 was released, the team decided to use the new mechanism for new events. For the sake of consistency, we also wanted to migrate existing event subscribers to FQCN events.

While FrameworkBundle implements a nice aliasing mechanism for its own events, we couldn't find an obvious way to make use of FQCN event aliases for our own events. The best way we could find is registering a compiler pass that would extend an internal parameter that stores all event aliases. But that made us feel like we're fiddling with an implementation detail of the framework.

This PR aims to provide a standard way for applications and third-party bundles to register their own event aliases.

```php
$container->addCompilerPass(new EventAliasesPass([
    MyCustomEvent::class => 'my_custom_event',
]));
```

Furthermore, it adds tests for class aliasing to the component's test suite. Additionally, the newly introduced pass is dogfooded by the SecurityBundle, so FrameworkBundle doesn't need to know about events fired by the security components.

Commits
-------

34efe40 [EventDispatcher] A compiler pass for aliased userland events.
@nicolas-grekas nicolas-grekas merged commit 34efe40 into symfony:4.4 Oct 4, 2019
@derrabus derrabus deleted the improvement/custom-aliased-events branch October 4, 2019 11:48
@nicolas-grekas nicolas-grekas modified the milestones: next, 4.4 Oct 27, 2019
This was referenced Nov 12, 2019
OskarStark added a commit to symfony/symfony-docs that referenced this pull request Feb 7, 2020
This PR was merged into the 4.4 branch.

Discussion
----------

[EventDispatcher] Document event name aliases

This PR fixes #12418 by documenting symfony/symfony#33793

Question: On the component's doc page, the `sidebar` block "Registering Event Listeners and Subscribers in the Service Container" has grown a bit larger with my changes. Would it make sense to move that block to a dedicated page now?

Commits
-------

c87632c [EventDispatcher] Document event name aliases.
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.

5 participants