diff --git a/src/Symfony/Component/Scheduler/Schedule.php b/src/Symfony/Component/Scheduler/Schedule.php index 4ccd88ff7a6e9..6b1ecf1635c96 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 "%s" listener, 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 "%s" listener, 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 "%s" listener, you need to set an event dispatcher on the Schedule.', __METHOD__)); + } + $this->dispatcher->addListener(FailureEvent::class, $listener, $priority); return $this;