diff --git a/src/Illuminate/Console/Scheduling/Schedule.php b/src/Illuminate/Console/Scheduling/Schedule.php index d2c835d59757..c92180585629 100644 --- a/src/Illuminate/Console/Scheduling/Schedule.php +++ b/src/Illuminate/Console/Scheduling/Schedule.php @@ -101,6 +101,13 @@ class Schedule */ protected array $groupStack = []; + /** + * The schedule default output. + * + * @var string|null + */ + protected ?string $defaultOutput = null; + /** * Create a new schedule instance. * @@ -327,6 +334,8 @@ public function group(Closure $events) */ protected function mergePendingAttributes(Event $event) { + $this->mergeDefaultAttributes($event); + if (isset($this->attributes)) { $this->attributes->mergeAttributes($event); @@ -340,6 +349,13 @@ protected function mergePendingAttributes(Event $event) } } + protected function mergeDefaultAttributes(Event $event): void + { + if (! is_null($this->defaultOutput)) { + $event->appendOutputTo($this->defaultOutput); + } + } + /** * Compile parameters for a command. * @@ -462,6 +478,19 @@ protected function getDispatcher() return $this->dispatcher; } + /** + * Specify the default output events should use from now on. + * + * @param string|null $output + * @return $this + */ + public function setDefaultOutput(?string $output): Schedule + { + $this->defaultOutput = $output; + + return $this; + } + /** * Dynamically handle calls into the schedule instance. * diff --git a/tests/Console/Scheduling/ScheduleTest.php b/tests/Console/Scheduling/ScheduleTest.php index a939aaefee29..9dd8d7f0207a 100644 --- a/tests/Console/Scheduling/ScheduleTest.php +++ b/tests/Console/Scheduling/ScheduleTest.php @@ -67,4 +67,33 @@ public function testJobIsNotInstantiatedIfSuppliedAsClassname(): void self::assertSame(JobToTestWithSchedule::class, $scheduledJob->description); self::assertFalse($this->container->resolved(JobToTestWithSchedule::class)); } + + #[DataProvider('scheduledEventAppendsOutputToDefaultOutputProvider')] + public function testScheduledEventAppendsOutputToDefaultOutput(string $method, array $arguments): void + { + $schedule = new Schedule(); + + /** @var \Illuminate\Console\Scheduling\Event $oldEvent */ + $oldEvent = call_user_func_array([$schedule, $method], $arguments); + $oldEventOutput = $oldEvent->output; + + $schedule->setDefaultOutput($defaultOutput = '/custom/scheduler/output'); + + /** @var \Illuminate\Console\Scheduling\Event $newEvent */ + $newEvent = call_user_func_array([$schedule, $method], $arguments); + + self::assertSame($oldEventOutput, $oldEvent->output); + self::assertSame($defaultOutput, $newEvent->output); + self::assertTrue($newEvent->shouldAppendOutput); + } + + public static function scheduledEventAppendsOutputToDefaultOutputProvider(): array + { + return [ + ['job', [JobToTestWithSchedule::class]], + ['command', ['env']], + ['call', [fn () => 0]], + ['exec', ['whoami']], + ]; + } }