50 lines
3.2 KiB
Markdown
50 lines
3.2 KiB
Markdown
# Symfony events
|
|
|
|
The **Events** window (right tool bar, or Tools) is there in a Symfony project (one with `bin/console`). It lists the events of the
|
|
application, who listens to them in which order, and writes the listener, the subscriber or a new event for you.
|
|
|
|
Every change is **shown as diffs first** (one editor tab per file) and written by **Write the files**. If a file changed meanwhile,
|
|
nothing is written: preview again. PHP files are edited with php-parser: a `use`, a method or an array item is inserted where a
|
|
person would write it, and the rest of the file stays byte for byte.
|
|
|
|
## Events tab
|
|
|
|

|
|
|
|
- **The picker**: a searchable list (name, constant, class) of the framework's events: kernel (`KernelEvents::*`), security
|
|
(`LoginSuccessEvent`, `CheckPassportEvent`…), forms, console, Messenger workers, Mailer and Doctrine (`Events::prePersist`…). Only
|
|
the events of the packages present in `vendor/` are shown, each with the class of its event object, so the method written has the
|
|
right type (`RequestEvent $event`, `PreUpdateEventArgs $args`). The project's own events are listed too: classes of `src/` that
|
|
extend `Event`, and what `src/` dispatches (`->dispatch(new X)`); a click opens the places where one is dispatched. The Messenger
|
|
bus's `dispatch` is not an event.
|
|
- **For the event chosen**, the window shows its **listeners in the code, in the order they are called** (the priority can be edited
|
|
in place, and a subscription removed: the method stays), then the form **Listen to this event**:
|
|
- a new **EventSubscriber** (`getSubscribedEvents()` with priorities; several methods for one event are supported);
|
|
- a new listener with `#[AsEventListener]` on a method or on the class;
|
|
- a Doctrine listener (`#[AsDoctrineListener]`) or entity listener (`#[AsEntityListener]`);
|
|
- a method added to an existing subscriber or listener.
|
|
|
|
Click **Preview the code**, check the diff, **Write the files**. **One more event in this class** collects several events into the
|
|
class being written.
|
|
- **New event**: the event class (properties as readonly constructor parameters, an optional `NAME`), its **dispatch** inserted at
|
|
the end of a method of a class you choose (before its last `return`; `EventDispatcherInterface` is injected, and a constructor is
|
|
written when the class has none), and optionally a listener for it.
|
|
|
|
## Listeners tab
|
|
|
|

|
|
|
|
Who listens to what, in which order:
|
|
|
|
- **From the code**: what the sources of the project declare; priorities editable in place.
|
|
- **From the application**: the result of `debug:event-dispatcher --format=json`, run through the [Symfony console](symfony-console.md)
|
|
runner (the site's container or the computer's PHP). It includes the vendors' listeners, located through Composer's PSR-4 map. A
|
|
click on a listener opens its method.
|
|
|
|
The filter takes an event name or a class.
|
|
|
|
## Limits
|
|
|
|
- The picker only knows events of installed packages; run `composer install` first.
|
|
- Dispatches are found by reading the code: an event name built at run time is not seen.
|
|
- The application view needs a working `bin/console`; when it fails, the error is shown.
|