TaskInfoService.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. namespace App\Crontab\Services;
  3. class TaskInfoService
  4. {
  5. /**
  6. * TaskInfoService constructor.
  7. */
  8. public function __construct()
  9. {
  10. }
  11. /**
  12. * 执行任务
  13. * todo 请维护README.md文件!!!
  14. * @param $params //module:执行代码所在的模块,service:执行代码所在的业务文件,method:执行代码所在的业务方法,params:执行代码的业务方法所携带的参数
  15. * @return int|void
  16. */
  17. public function execute($params)
  18. {
  19. try {
  20. $module = $params['module'] ?? '';
  21. $service = $params['service'] ?? '';
  22. $method = $params['method'] ?? '';
  23. $params = $params['params'] ?? [];
  24. if($module && $method){
  25. $module = ucfirst(convertUnderline($module));
  26. $method = convertUnderline($method);
  27. $service = ucfirst(convertUnderline($service));
  28. $reflection = new \ReflectionClass('App\\'.$module.'\\Services\\'.$service.'Service');
  29. $service = $reflection->newInstanceWithoutConstructor();
  30. if($reflection->hasMethod($method)){
  31. if(!empty($params)){
  32. return $service->$method(...$params);
  33. } else {
  34. return $service->$method();
  35. }
  36. }
  37. return 0;
  38. }
  39. } catch (\Exception $e){
  40. //print_r($e->getMessage());
  41. }
  42. }
  43. }