CleanupServiceProvider.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace App\Module\Cleanup;
  3. use Illuminate\Support\ServiceProvider;
  4. use Illuminate\Support\Facades\Event;
  5. use App\Module\Cleanup\Commands\ScanTablesCommand;
  6. use App\Module\Cleanup\Commands\ScanModelsCommand;
  7. use App\Module\Cleanup\Commands\TestModelCleanupCommand;
  8. use App\Module\Cleanup\Commands\ValidateModelCleanupCommand;
  9. use App\Module\Cleanup\Commands\CleanupDataCommand;
  10. use App\Module\Cleanup\Commands\InsertCleanupAdminMenuCommand;
  11. /**
  12. * Cleanup 模块服务提供者
  13. *
  14. * 注册模块的服务、命令、路由等
  15. */
  16. class CleanupServiceProvider extends ServiceProvider
  17. {
  18. /**
  19. * 注册服务
  20. */
  21. public function register(): void
  22. {
  23. // 注册配置文件
  24. $this->mergeConfigFrom(
  25. __DIR__ . '/config/cleanup.php',
  26. 'cleanup'
  27. );
  28. // 注册服务单例
  29. $this->app->singleton('cleanup.service', function ($app) {
  30. return new Services\CleanupService();
  31. });
  32. }
  33. /**
  34. * 启动服务
  35. */
  36. public function boot(): void
  37. {
  38. // 发布配置文件
  39. $this->publishes([
  40. __DIR__ . '/config/cleanup.php' => config_path('cleanup.php'),
  41. ], 'cleanup-config');
  42. // 注册命令
  43. if ($this->app->runningInConsole()) {
  44. $this->commands([
  45. ScanTablesCommand::class,
  46. ScanModelsCommand::class,
  47. TestModelCleanupCommand::class,
  48. ValidateModelCleanupCommand::class,
  49. CleanupDataCommand::class,
  50. InsertCleanupAdminMenuCommand::class,
  51. ]);
  52. }
  53. // 注册事件监听器
  54. $this->registerEventListeners();
  55. // 路由通过 route-attributes 自动注册,无需手动注册
  56. }
  57. /**
  58. * 注册事件监听器
  59. */
  60. protected function registerEventListeners(): void
  61. {
  62. // 任务状态变更事件
  63. Event::listen(
  64. \App\Module\Cleanup\Events\TaskStatusChanged::class,
  65. \App\Module\Cleanup\Listeners\TaskStatusChangedListener::class
  66. );
  67. // 备份完成事件
  68. Event::listen(
  69. \App\Module\Cleanup\Events\BackupCompleted::class,
  70. \App\Module\Cleanup\Listeners\BackupCompletedListener::class
  71. );
  72. // 清理完成事件
  73. Event::listen(
  74. \App\Module\Cleanup\Events\CleanupCompleted::class,
  75. \App\Module\Cleanup\Listeners\CleanupCompletedListener::class
  76. );
  77. }
  78. /**
  79. * 获取提供的服务
  80. */
  81. public function provides(): array
  82. {
  83. return [
  84. 'cleanup.service',
  85. ];
  86. }
  87. }