From 9598024898a71ea6d4791c5f8c422878bd1dc502 Mon Sep 17 00:00:00 2001 From: Lucas Michot Date: Fri, 18 Mar 2022 15:05:15 +0100 Subject: [PATCH] [9.x] Improve ScheduleListCommand (#41552) * Add an extra test. * Promote early return if the schedule is empty. * Call getTerminalWidth statically. * Call str_replace only once. * No need to create a new DateTimeZone for each item in the map. * Use line() method. * Clean up formatCronExpression method. * Fix CS. * formatting Co-authored-by: Taylor Otwell --- .../Scheduling/ScheduleListCommand.php | 33 +++++++++++-------- .../Scheduling/ScheduleListCommandTest.php | 7 ++++ 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/src/Illuminate/Console/Scheduling/ScheduleListCommand.php b/src/Illuminate/Console/Scheduling/ScheduleListCommand.php index e86839f25e61..ba855095ca34 100644 --- a/src/Illuminate/Console/Scheduling/ScheduleListCommand.php +++ b/src/Illuminate/Console/Scheduling/ScheduleListCommand.php @@ -45,21 +45,30 @@ class ScheduleListCommand extends Command public function handle(Schedule $schedule) { $events = collect($schedule->events()); - $terminalWidth = $this->getTerminalWidth(); + + if ($events->isEmpty()) { + $this->comment('No scheduled tasks have been defined.'); + + return; + } + + $terminalWidth = self::getTerminalWidth(); + $expressionSpacing = $this->getCronExpressionSpacing($events); - $events = $events->map(function ($event) use ($terminalWidth, $expressionSpacing) { + $timezone = new DateTimeZone($this->option('timezone') ?? config('app.timezone')); + + $events = $events->map(function ($event) use ($terminalWidth, $expressionSpacing, $timezone) { $expression = $this->formatCronExpression($event->expression, $expressionSpacing); $command = $event->command; $description = $event->description; if (! $this->output->isVerbose()) { - $command = str_replace( - Application::artisanBinary(), + $command = str_replace([Application::phpBinary(), Application::artisanBinary()], [ + 'php', preg_replace("#['\"]#", '', Application::artisanBinary()), - str_replace(Application::phpBinary(), 'php', $event->command) - ); + ], $event->command); } if ($event instanceof CallbackEvent) { @@ -77,7 +86,7 @@ public function handle(Schedule $schedule) $nextDueDate = Carbon::create((new CronExpression($event->expression)) ->getNextRunDate(Carbon::now()->setTimezone($event->timezone)) - ->setTimezone(new DateTimeZone($this->option('timezone') ?? config('app.timezone'))) + ->setTimezone($timezone) ); $nextDueDate = $this->output->isVerbose() @@ -109,11 +118,7 @@ public function handle(Schedule $schedule) ) : '']; }); - if ($events->isEmpty()) { - return $this->comment('No scheduled tasks have been defined.'); - } - - $this->output->writeln( + $this->line( $events->flatten()->filter()->prepend('')->push('')->toArray() ); } @@ -140,10 +145,10 @@ private function getCronExpressionSpacing($events) */ private function formatCronExpression($expression, $spacing) { - $expression = explode(' ', $expression); + $expressions = explode(' ', $expression); return collect($spacing) - ->map(fn ($length, $index) => $expression[$index] = str_pad($expression[$index], $length)) + ->map(fn ($length, $index) => str_pad($expressions[$index], $length)) ->implode(' '); } diff --git a/tests/Integration/Console/Scheduling/ScheduleListCommandTest.php b/tests/Integration/Console/Scheduling/ScheduleListCommandTest.php index eed124db3270..891be7452413 100644 --- a/tests/Integration/Console/Scheduling/ScheduleListCommandTest.php +++ b/tests/Integration/Console/Scheduling/ScheduleListCommandTest.php @@ -21,6 +21,13 @@ protected function setUp(): void $this->schedule = $this->app->make(Schedule::class); } + public function testDisplayEmptySchedule() + { + $this->artisan(ScheduleListCommand::class) + ->assertSuccessful() + ->expectsOutput('No scheduled tasks have been defined.'); + } + public function testDisplaySchedule() { $this->schedule->command(FooCommand::class)->quarterly();