CleanupServiceProvider.php 2.1 KB

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