SystemServiceProvider.php 3.5 KB

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