-
Notifications
You must be signed in to change notification settings - Fork 14
/
ListScheduler.php
104 lines (89 loc) · 2.46 KB
/
ListScheduler.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
declare(strict_types=1);
namespace Hmazter\LaravelScheduleList\Console;
use Hmazter\LaravelScheduleList\ScheduleEvent;
use Hmazter\LaravelScheduleList\ScheduleList;
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class ListScheduler extends Command
{
/**
* The console command name.
*
* @var string
*/
protected $name = 'schedule:list';
/**
* The console command description.
*
* @var string
*/
protected $description = 'List all scheduled commands in the task scheduler';
/**
* @var ScheduleList
*/
protected $scheduleList;
/**
* @param ScheduleList $scheduleList
*/
public function __construct(ScheduleList $scheduleList)
{
$this->scheduleList = $scheduleList;
parent::__construct();
}
/**
* Get the console command options.
*
* @return array
*/
protected function getOptions()
{
return [
['cron', null, InputOption::VALUE_NONE, 'Show output cron style', null],
];
}
/**
* Execute the console command.
*/
public function handle()
{
$events = $this->scheduleList->all();
if (count($events) === 0) {
$this->info('No tasks scheduled');
return;
}
if ($this->option('cron')) {
$this->outputCronStyle($events);
return;
}
$this->outputTableStyle($events);
}
/**
* @param array|ScheduleEvent[] $events
*/
protected function outputCronStyle($events)
{
foreach ($events as $event) {
$this->line($event->getExpression() . ' ' . $event->getFullCommand());
}
}
/**
* @param array|ScheduleEvent[] $events
*/
protected function outputTableStyle($events)
{
$isVerbosityNormal = $this->output->getVerbosity() === OutputInterface::VERBOSITY_NORMAL;
$rows = [];
foreach ($events as $event) {
$rows[] = [
'expression' => $event->getExpression(),
'next run at' => $event->getNextRunDate()->format('Y-m-d H:i:s'),
'command' => $isVerbosityNormal ? $event->getShortCommand() : $event->getFullCommand(),
'description' => $event->getDescription(),
];
}
$headers = array_keys($rows[0]);
$this->table($headers, $rows);
}
}