From 77ebf5081c4606d4672d401f57f062261cbba58b Mon Sep 17 00:00:00 2001 From: Theo Malboeuf Date: Thu, 24 Sep 2026 12:18:24 +0200 Subject: [PATCH] #189 - Fix EventDispatcherTask ignoring the event_name option The event was dispatched without a name, so it was only dispatched under its class name and listeners registered on the configured `event_name` were never called. Regression from a833456 (v4.0). - Dispatch the event under `event_name` again - Make `event_name` optional and nullable, consistently with EventDispatcherInterface::dispatch(): when null (default), the event is dispatched under its class name, as before - Update the reference documentation Co-Authored-By: Claude Opus 5.5 (1M context) --- CHANGELOG.md | 1 + docs/reference/tasks/event_dispatcher_task.md | 26 ++++++++++++++----- src/Task/Event/EventDispatcherTask.php | 6 ++--- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3422c1d4..b238c4df 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ Latest ## Fixes * [#192](https://github.com/cleverage/process-bundle/issues/192) Fix CommandRunnerTask: only pass the `options` option to `Process::setOptions()`, support string `commandline` through `Process::fromShellCommandline()`, validate option types. Update documentation, add tests. * [#194](https://github.com/cleverage/process-bundle/issues/194) Fix ProcessLauncherTask: the `process_options` normalizer returned an array despite its scalar return type, so the task always failed with a `TypeError`. Update documentation, add tests. +* [#189](https://github.com/cleverage/process-bundle/issues/189) Fix EventDispatcherTask: dispatch the event under the `event_name` option (regression since v4.0). `event_name` is now optional: when `null` (default), the event is dispatched under its class name. Update documentation. v5.0 ----- diff --git a/docs/reference/tasks/event_dispatcher_task.md b/docs/reference/tasks/event_dispatcher_task.md index 23ac3f98..0a0479f9 100644 --- a/docs/reference/tasks/event_dispatcher_task.md +++ b/docs/reference/tasks/event_dispatcher_task.md @@ -24,10 +24,10 @@ Possible outputs Options ------- -| Code | Type | Required | Default | Description | -|--------------|----------|:--------:|---------|---------------------------------------------------------------| -| `event_name` | `string` | **X** | | Name of the event (see Notes: currently not used to dispatch) | -| `passive` | `bool` | | `true` | If `true`, the input is passed to the output before dispatch | +| Code | Type | Required | Default | Description | +|--------------|----------------|:--------:|---------|----------------------------------------------------------------------------| +| `event_name` | `string\|null` | | `null` | Name of the dispatched event, `null` to use the event class name (see Notes) | +| `passive` | `bool` | | `true` | If `true`, the input is passed to the output before dispatch | Examples -------- @@ -50,6 +50,18 @@ push_data_event: Notes ----- -The event is dispatched without an explicit name (`$eventDispatcher->dispatch($event)`), so its name is the event -class name. Listeners must therefore subscribe to `CleverAge\ProcessBundle\Event\EventDispatcherTaskEvent`; the -`event_name` option is required and validated but not used for dispatching. +The event is dispatched with `$eventDispatcher->dispatch($event, $eventName)`: + +* when `event_name` is set, listeners must subscribe to that name: + +```php +#[AsEventListener(event: 'myapp.data_queue')] +public function onDataQueue(EventDispatcherTaskEvent $event): void +{ + $input = $event->getState()->getInput(); +} +``` + +* when `event_name` is `null`, the event name is the event class name, so listeners must subscribe to + `CleverAge\ProcessBundle\Event\EventDispatcherTaskEvent`. Every `EventDispatcherTask` without `event_name` then + triggers the same listeners. diff --git a/src/Task/Event/EventDispatcherTask.php b/src/Task/Event/EventDispatcherTask.php index fcf8bc92..e33373f9 100644 --- a/src/Task/Event/EventDispatcherTask.php +++ b/src/Task/Event/EventDispatcherTask.php @@ -39,14 +39,14 @@ public function execute(ProcessState $state): void $event = new EventDispatcherTaskEvent($state); - $this->eventDispatcher->dispatch($event); + $this->eventDispatcher->dispatch($event, $options['event_name']); } protected function configureOptions(OptionsResolver $resolver): void { - $resolver->setRequired(['event_name']); + $resolver->setDefault('event_name', null); $resolver->setDefault('passive', true); - $resolver->setAllowedTypes('event_name', ['string']); + $resolver->setAllowedTypes('event_name', ['null', 'string']); $resolver->setAllowedTypes('passive', ['boolean']); } }