From dfd65d667f8a75ed5cdbc2a20f1feb1f5c7ddaab 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..bd413892bc671 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(sprintf('To register a listener with "%s()", you need to set an event dispatcher on the Schedule.', __METHOD__)); + } + $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(sprintf('To register a listener with "%s()", you need to set an event dispatcher on the Schedule.', __METHOD__)); + } + $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(sprintf('To register a listener with "%s()", you need to set an event dispatcher on the Schedule.', __METHOD__)); + } + $this->dispatcher->addListener(FailureEvent::class, $listener, $priority); return $this;