-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
[EventDispatcher] Document event name aliases #12420
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
OskarStark
merged 1 commit into
symfony:4.4
from
derrabus:improvement/custom-aliased-events
Feb 7, 2020
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -246,6 +246,73 @@ there are some minor advantages for each of them: | |
* **Listeners are more flexible** because bundles can enable or disable each of | ||
them conditionally depending on some configuration value. | ||
|
||
.. _event-aliases: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be removed as it is not used and the anchor does not differ from the current headline. |
||
|
||
Event Aliases | ||
------------- | ||
|
||
When configuring event listeners and subscribers via dependency injection, | ||
Symfony's core events can also be referred to by the fully qualified class | ||
name (FQCN) of the corresponding event class:: | ||
|
||
// src/EventSubscriber/RequestSubscriber.php | ||
namespace App\EventSubscriber; | ||
derrabus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Symfony\Component\HttpKernel\Event\RequestEvent; | ||
|
||
class RequestSubscriber implements EventSubscriberInterface | ||
{ | ||
public static function getSubscribedEvents(): array | ||
{ | ||
return [ | ||
RequestEvent::class => 'onKernelRequest', | ||
]; | ||
} | ||
|
||
public function onKernelRequest(RequestEvent $event) | ||
{ | ||
// ... | ||
} | ||
} | ||
|
||
.. versionadded:: 4.3 | ||
|
||
Referring Symfony's core events via the FQCN of the event class is possible | ||
since Symfony 4.3. | ||
|
||
Internally, the event FQCN are treated as aliases for the original event names. | ||
Since the mapping already happens when compiling the service container, event | ||
listeners and subscribers using FQCN instead of event names will appear under | ||
the original event name when inspecting the event dispatcher. | ||
|
||
This alias mapping can be extended for custom events by registering the | ||
compiler pass ``AddEventAliasesPass``:: | ||
|
||
// src/Kernel.php | ||
use App\Event\MyCustomEvent; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\EventDispatcher\DependencyInjection\AddEventAliasesPass; | ||
use Symfony\Component\HttpKernel\Kernel as BaseKernel; | ||
|
||
class Kernel extends BaseKernel | ||
{ | ||
protected function build(ContainerBuilder $container) | ||
{ | ||
$container->addCompilerPass(new AddEventAliasesPass([ | ||
MyCustomEvent::class => 'my_custom_event', | ||
])); | ||
} | ||
} | ||
|
||
The compiler pass will always extend the existing list of aliases. Because of | ||
that, it is safe to register multiple instances of the pass with different | ||
configurations. | ||
|
||
.. versionadded:: 4.4 | ||
|
||
The ``AddEventAliasesPass`` class was introduced in Symfony 4.4. | ||
|
||
Debugging Event Listeners | ||
------------------------- | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
is able to resolve
->resolves
?