-
Notifications
You must be signed in to change notification settings - Fork 10.7k
/
ScheduleTestCommand.php
71 lines (56 loc) · 1.85 KB
/
ScheduleTestCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
namespace Illuminate\Console\Scheduling;
use Illuminate\Console\Command;
use Illuminate\Support\Str;
class ScheduleTestCommand extends Command
{
/**
* The console command name.
*
* @var string
*/
protected $signature = 'schedule:test {--name= : The name of the scheduled command to run}';
/**
* The name of the console command.
*
* This name is used to identify the command during lazy loading.
*
* @var string|null
*/
protected static $defaultName = 'schedule:test';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Run a scheduled command';
/**
* Execute the console command.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
public function handle(Schedule $schedule)
{
$commands = $schedule->events();
$commandNames = [];
foreach ($commands as $command) {
$commandNames[] = $command->command ?? $command->getSummaryForDisplay();
}
if (empty($commandNames)) {
return $this->comment('No scheduled commands have been defined.');
}
if (! empty($name = $this->option('name'))) {
$matches = array_filter($commandNames, fn ($commandName) => Str::endsWith($commandName, $name));
if (count($matches) !== 1) {
return $this->error('No matching scheduled command found.');
}
$index = key($matches);
} else {
$index = array_search($this->choice('Which command would you like to run?', $commandNames), $commandNames);
}
$event = $commands[$index];
$this->line('<info>['.date('c').'] Running scheduled command:</info> '.$event->getSummaryForDisplay());
$event->run($this->laravel);
}
}