SystemServiceProvider.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace App\Module\System\Providers;
  3. use App\Module\System\Commands\CleanJobRunsCommand;
  4. use App\Module\System\Events\ConfigChangedEvent;
  5. use App\Module\System\Events\SystemLogCreatedEvent;
  6. use App\Module\System\Events\ViewConfigChangedEvent;
  7. use App\Module\System\Listeners\SystemEventListener;
  8. use Illuminate\Console\Scheduling\Schedule;
  9. use Illuminate\Support\ServiceProvider;
  10. /**
  11. * 系统模块服务提供者
  12. */
  13. class SystemServiceProvider extends ServiceProvider
  14. {
  15. /**
  16. * 事件到监听器的映射
  17. *
  18. * @var array
  19. */
  20. protected $listen = [
  21. ConfigChangedEvent::class => [
  22. SystemEventListener::class . '@handleConfigChanged',
  23. ],
  24. ViewConfigChangedEvent::class => [
  25. SystemEventListener::class . '@handleViewConfigChanged',
  26. ],
  27. SystemLogCreatedEvent::class => [
  28. SystemEventListener::class . '@handleSystemLogCreated',
  29. ],
  30. ];
  31. /**
  32. * 需要注册的订阅者
  33. *
  34. * @var array
  35. */
  36. protected $subscribe = [
  37. SystemEventListener::class,
  38. ];
  39. /**
  40. * 注册服务
  41. *
  42. * @return void
  43. */
  44. public function register(): void
  45. {
  46. // 不注册服务
  47. // // 注册服务...
  48. // $this->app->singleton('system.config.service', function ($app) {
  49. // return new \App\Module\System\Services\ConfigService();
  50. // });
  51. //
  52. // $this->app->singleton('system.view.config.service', function ($app) {
  53. // return new \App\Module\System\Services\ViewConfigService();
  54. // });
  55. //
  56. // $this->app->singleton('system.log.service', function ($app) {
  57. // return new \App\Module\System\Services\LogService();
  58. // });
  59. }
  60. /**
  61. * 启动服务
  62. *
  63. * @return void
  64. */
  65. public function boot(): void
  66. {
  67. // 注册事件监听器
  68. $this->registerEvents();
  69. // 注册命令
  70. $this->registerCommands();
  71. // 注册定时任务
  72. $this->registerSchedules();
  73. }
  74. /**
  75. * 注册事件和监听器
  76. *
  77. * @return void
  78. */
  79. protected function registerEvents(): void
  80. {
  81. $events = $this->app['events'];
  82. foreach ($this->listen as $event => $listeners) {
  83. foreach ($listeners as $listener) {
  84. $events->listen($event, $listener);
  85. }
  86. }
  87. foreach ($this->subscribe as $subscriber) {
  88. $events->subscribe($subscriber);
  89. }
  90. }
  91. /**
  92. * 注册命令
  93. *
  94. * @return void
  95. */
  96. protected function registerCommands(): void
  97. {
  98. if ($this->app->runningInConsole()) {
  99. $this->commands([
  100. CleanJobRunsCommand::class,
  101. ]);
  102. }
  103. }
  104. /**
  105. * 注册System模块相关的定时任务
  106. *
  107. * @return void
  108. */
  109. protected function registerSchedules(): void
  110. {
  111. // 在应用完全启动后注册定时任务
  112. $this->app->booted(function () {
  113. $schedule = $this->app->make(Schedule::class);
  114. // 每小时清理job_runs表,保留5天数据
  115. $schedule->command('system:clean-job-runs')
  116. ->hourly()
  117. ->description('清理job_runs表过期记录(保留5天)')
  118. ->withoutOverlapping() // 防止重复执行
  119. ->runInBackground(); // 后台运行
  120. });
  121. }
  122. }