Skip to content

Commit

Permalink
Prevent an infinite loop with afterFlush events
Browse files Browse the repository at this point in the history
  • Loading branch information
Paweł Kąkol committed Sep 19, 2024
1 parent d5b73fe commit f49063d
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/Service/DelayableEventDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class DelayableEventDispatcher
* @var list<AbstractEntityEvent>
*/
private array $eventsToDispatchAfterFlush = [];
private bool $dispatchingDelayed = false;

public function __construct(
private readonly EventDispatcherInterface $eventDispatcher,
Expand All @@ -32,11 +33,18 @@ public function dispatch(

public function submitDelayed(): void
{
if ($this->dispatchingDelayed) {
return; // prevent infinite loop with afterFlush events
}

$this->dispatchingDelayed = true;

foreach ($this->eventsToDispatchAfterFlush as $event) {
$this->eventDispatcher->dispatch($event);
$this->eventDispatcher->dispatch(new DispatchEvent($event));
}

$this->dispatchingDelayed = false;
$this->eventsToDispatchAfterFlush = [];
}

Expand Down

0 comments on commit f49063d

Please sign in to comment.