CleanupServiceProvider.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. namespace App\Module\Cleanup;
  3. use Illuminate\Support\ServiceProvider;
  4. use App\Module\Cleanup\Commands\ScanTablesCommand;
  5. use App\Module\Cleanup\Commands\CleanupDataCommand;
  6. /**
  7. * Cleanup 模块服务提供者
  8. *
  9. * 注册模块的服务、命令、路由等
  10. */
  11. class CleanupServiceProvider extends ServiceProvider
  12. {
  13. /**
  14. * 注册服务
  15. */
  16. public function register(): void
  17. {
  18. // 注册配置文件
  19. $this->mergeConfigFrom(
  20. __DIR__ . '/config/cleanup.php',
  21. 'cleanup'
  22. );
  23. // 注册服务单例
  24. $this->app->singleton('cleanup.service', function ($app) {
  25. return new Services\CleanupService();
  26. });
  27. }
  28. /**
  29. * 启动服务
  30. */
  31. public function boot(): void
  32. {
  33. // 发布配置文件
  34. $this->publishes([
  35. __DIR__ . '/config/cleanup.php' => config_path('cleanup.php'),
  36. ], 'cleanup-config');
  37. // 发布数据库迁移文件
  38. $this->publishes([
  39. __DIR__ . '/Databases/GenerateSql/' => database_path('sql/cleanup/'),
  40. ], 'cleanup-migrations');
  41. // 注册命令
  42. if ($this->app->runningInConsole()) {
  43. $this->commands([
  44. ScanTablesCommand::class,
  45. CleanupDataCommand::class,
  46. ]);
  47. }
  48. // 注册路由
  49. $this->loadRoutesFrom(__DIR__ . '/routes/web.php');
  50. $this->loadRoutesFrom(__DIR__ . '/routes/api.php');
  51. // 注册视图
  52. $this->loadViewsFrom(__DIR__ . '/resources/views', 'cleanup');
  53. // 发布视图文件
  54. $this->publishes([
  55. __DIR__ . '/resources/views' => resource_path('views/vendor/cleanup'),
  56. ], 'cleanup-views');
  57. // 发布前端资源
  58. $this->publishes([
  59. __DIR__ . '/resources/assets' => public_path('vendor/cleanup'),
  60. ], 'cleanup-assets');
  61. // 注册事件监听器
  62. $this->registerEventListeners();
  63. // 注册中间件
  64. $this->registerMiddleware();
  65. // 注册调度任务
  66. $this->registerScheduledTasks();
  67. }
  68. /**
  69. * 注册事件监听器
  70. */
  71. protected function registerEventListeners(): void
  72. {
  73. // 任务状态变更事件
  74. \Event::listen(
  75. \App\Module\Cleanup\Events\TaskStatusChanged::class,
  76. \App\Module\Cleanup\Listeners\TaskStatusChangedListener::class
  77. );
  78. // 备份完成事件
  79. \Event::listen(
  80. \App\Module\Cleanup\Events\BackupCompleted::class,
  81. \App\Module\Cleanup\Listeners\BackupCompletedListener::class
  82. );
  83. // 清理完成事件
  84. \Event::listen(
  85. \App\Module\Cleanup\Events\CleanupCompleted::class,
  86. \App\Module\Cleanup\Listeners\CleanupCompletedListener::class
  87. );
  88. }
  89. /**
  90. * 注册中间件
  91. */
  92. protected function registerMiddleware(): void
  93. {
  94. // 注册清理权限中间件
  95. $this->app['router']->aliasMiddleware(
  96. 'cleanup.permission',
  97. \App\Module\Cleanup\Middleware\CleanupPermissionMiddleware::class
  98. );
  99. // 注册任务状态检查中间件
  100. $this->app['router']->aliasMiddleware(
  101. 'cleanup.task-status',
  102. \App\Module\Cleanup\Middleware\TaskStatusMiddleware::class
  103. );
  104. }
  105. /**
  106. * 注册调度任务
  107. */
  108. protected function registerScheduledTasks(): void
  109. {
  110. // 注册定时清理过期备份的任务
  111. $this->app->booted(function () {
  112. $schedule = $this->app->make(\Illuminate\Console\Scheduling\Schedule::class);
  113. // 每天凌晨2点清理过期备份
  114. $schedule->call(function () {
  115. \App\Module\Cleanup\Services\CleanupService::cleanExpiredBackups(
  116. config('cleanup.backup.retention_days', 30)
  117. );
  118. })->dailyAt('02:00')->name('cleanup-expired-backups');
  119. // 每天凌晨3点清理历史日志
  120. $schedule->call(function () {
  121. \App\Module\Cleanup\Services\CleanupService::cleanHistoryLogs(
  122. config('cleanup.log.retention_days', 30)
  123. );
  124. })->dailyAt('03:00')->name('cleanup-history-logs');
  125. // 每小时更新表统计信息
  126. $schedule->call(function () {
  127. \App\Module\Cleanup\Services\CleanupService::scanTables(false);
  128. })->hourly()->name('update-table-stats');
  129. });
  130. }
  131. /**
  132. * 获取提供的服务
  133. */
  134. public function provides(): array
  135. {
  136. return [
  137. 'cleanup.service',
  138. ];
  139. }
  140. }