CleanupServiceProvider.php 2.3 KB

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