Schedule.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CatchAdmin [Just Like ~ ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
  8. // +----------------------------------------------------------------------
  9. // | Author: JaguarJack [ njphper@gmail.com ]
  10. // +----------------------------------------------------------------------
  11. namespace catcher\library\crontab;
  12. use catcher\exceptions\FailedException;
  13. class Schedule
  14. {
  15. protected $crons = [];
  16. /**
  17. * 新增 command 任务
  18. *
  19. * @time 2020年07月04日
  20. * @param $command
  21. * @param array $arguments
  22. * @return Cron
  23. */
  24. public function command($command, $arguments = []): Cron
  25. {
  26. $this->crons[] = $cron = new Cron($command);
  27. return $cron;
  28. }
  29. /**
  30. * 新增 task 任务
  31. *
  32. * @time 2020年07月04日
  33. * @param $task
  34. * @param array $argument
  35. * @return Cron
  36. */
  37. public function task($task, $argument = []): Cron
  38. {
  39. if (is_string($task)) {
  40. if (!class_exists($task)) {
  41. throw new FailedException("[$task] not found");
  42. }
  43. $task = new $task(...$argument);
  44. }
  45. $this->crons[] = $cron = new Cron($task);
  46. return $cron;
  47. }
  48. public function getCronTask()
  49. {
  50. return $this->crons;
  51. }
  52. }