Skip to content

Commit 9598024

Browse files
[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 <[email protected]>
1 parent 91dfbd0 commit 9598024

File tree

2 files changed

+26
-14
lines changed

2 files changed

+26
-14
lines changed

src/Illuminate/Console/Scheduling/ScheduleListCommand.php

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,30 @@ class ScheduleListCommand extends Command
4545
public function handle(Schedule $schedule)
4646
{
4747
$events = collect($schedule->events());
48-
$terminalWidth = $this->getTerminalWidth();
48+
49+
if ($events->isEmpty()) {
50+
$this->comment('No scheduled tasks have been defined.');
51+
52+
return;
53+
}
54+
55+
$terminalWidth = self::getTerminalWidth();
56+
4957
$expressionSpacing = $this->getCronExpressionSpacing($events);
5058

51-
$events = $events->map(function ($event) use ($terminalWidth, $expressionSpacing) {
59+
$timezone = new DateTimeZone($this->option('timezone') ?? config('app.timezone'));
60+
61+
$events = $events->map(function ($event) use ($terminalWidth, $expressionSpacing, $timezone) {
5262
$expression = $this->formatCronExpression($event->expression, $expressionSpacing);
5363

5464
$command = $event->command;
5565
$description = $event->description;
5666

5767
if (! $this->output->isVerbose()) {
58-
$command = str_replace(
59-
Application::artisanBinary(),
68+
$command = str_replace([Application::phpBinary(), Application::artisanBinary()], [
69+
'php',
6070
preg_replace("#['\"]#", '', Application::artisanBinary()),
61-
str_replace(Application::phpBinary(), 'php', $event->command)
62-
);
71+
], $event->command);
6372
}
6473

6574
if ($event instanceof CallbackEvent) {
@@ -77,7 +86,7 @@ public function handle(Schedule $schedule)
7786

7887
$nextDueDate = Carbon::create((new CronExpression($event->expression))
7988
->getNextRunDate(Carbon::now()->setTimezone($event->timezone))
80-
->setTimezone(new DateTimeZone($this->option('timezone') ?? config('app.timezone')))
89+
->setTimezone($timezone)
8190
);
8291

8392
$nextDueDate = $this->output->isVerbose()
@@ -109,11 +118,7 @@ public function handle(Schedule $schedule)
109118
) : ''];
110119
});
111120

112-
if ($events->isEmpty()) {
113-
return $this->comment('No scheduled tasks have been defined.');
114-
}
115-
116-
$this->output->writeln(
121+
$this->line(
117122
$events->flatten()->filter()->prepend('')->push('')->toArray()
118123
);
119124
}
@@ -140,10 +145,10 @@ private function getCronExpressionSpacing($events)
140145
*/
141146
private function formatCronExpression($expression, $spacing)
142147
{
143-
$expression = explode(' ', $expression);
148+
$expressions = explode(' ', $expression);
144149

145150
return collect($spacing)
146-
->map(fn ($length, $index) => $expression[$index] = str_pad($expression[$index], $length))
151+
->map(fn ($length, $index) => str_pad($expressions[$index], $length))
147152
->implode(' ');
148153
}
149154

tests/Integration/Console/Scheduling/ScheduleListCommandTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ protected function setUp(): void
2121
$this->schedule = $this->app->make(Schedule::class);
2222
}
2323

24+
public function testDisplayEmptySchedule()
25+
{
26+
$this->artisan(ScheduleListCommand::class)
27+
->assertSuccessful()
28+
->expectsOutput('No scheduled tasks have been defined.');
29+
}
30+
2431
public function testDisplaySchedule()
2532
{
2633
$this->schedule->command(FooCommand::class)->quarterly();

0 commit comments

Comments
 (0)