Skip to content

Commit

Permalink
[9.x] Improve ScheduleListCommand (#41552)
Browse files Browse the repository at this point in the history
* 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 <taylor@laravel.com>
  • Loading branch information
lucasmichot and taylorotwell authored Mar 18, 2022
1 parent 91dfbd0 commit 9598024
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
33 changes: 19 additions & 14 deletions src/Illuminate/Console/Scheduling/ScheduleListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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()
Expand Down Expand Up @@ -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()
);
}
Expand All @@ -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(' ');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 9598024

Please sign in to comment.