有没有办法从控制器获取计划任务作为数组?

Is there a way to get the scheduled task as an array from a controller?

我想从控制器获取计划任务列表。 Some packages, articles and even 解释了如何通过命令显示它,但我没有找到如何在没有命令的情况下显示它。我的目标是获取一系列计划任务及其日期和描述。

有没有办法从控制器获取计划任务作为数组(或对象列表,或任何可以轻松处理的东西)?

这是获取所有计划任务的方法:

app()->make(\Illuminate\Contracts\Console\Kernel::class);
$schedule = app()->make(\Illuminate\Console\Scheduling\Schedule::class);

$events = collect($schedule->events())->map(function($event) {
    $cron = CronExpression::factory($event->expression);
    $date = Carbon::now();
    if ($event->timezone) {
        $date->setTimezone($event->timezone);
    }
    return (object)[
        'expression' => $event->expression,
        'command' => str_after($event->command, '\'artisan\' '),
        'next_run_at' => $cron->getNextRunDate()->format('Y-m-d H:i:s'),
    ];
});

您有一组对象(在 $events 中)具有三个属性:

  • 表达式 - 示例:12 1 * * *
  • 命令 - 示例:mypackage:mycommand
  • next_run_at - 示例:2018-01-10 16:50:49