CleanupServiceProvider.php 2.5 KB

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