From 6dc9691544200424b0c29ab748e69e35f0374d07 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sat, 1 Jun 2024 08:25:18 +0200 Subject: [PATCH] [Scheduler] Throw an exception when no dispatcher has been passed to a Schedule --- src/Symfony/Component/Scheduler/Schedule.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/Symfony/Component/Scheduler/Schedule.php b/src/Symfony/Component/Scheduler/Schedule.php index 4ccd88ff7a6e9..67d3fd0cde7b2 100644 --- a/src/Symfony/Component/Scheduler/Schedule.php +++ b/src/Symfony/Component/Scheduler/Schedule.php @@ -141,6 +141,10 @@ public function getSchedule(): static public function before(callable $listener, int $priority = 0): static { + if (!$this->dispatcher) { + throw new LogicException('To register a "before" listener, you need to set an event dispatcher on the Schedule.'); + } + $this->dispatcher->addListener(PreRunEvent::class, $listener, $priority); return $this; @@ -148,6 +152,10 @@ public function before(callable $listener, int $priority = 0): static public function after(callable $listener, int $priority = 0): static { + if (!$this->dispatcher) { + throw new LogicException('To register a "before" listener, you need to set an event dispatcher on the Schedule.'); + } + $this->dispatcher->addListener(PostRunEvent::class, $listener, $priority); return $this; @@ -155,6 +163,10 @@ public function after(callable $listener, int $priority = 0): static public function onFailure(callable $listener, int $priority = 0): static { + if (!$this->dispatcher) { + throw new LogicException('To register a "before" listener, you need to set an event dispatcher on the Schedule.'); + } + $this->dispatcher->addListener(FailureEvent::class, $listener, $priority); return $this;