| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- <?php
- namespace App\Module\Cleanup;
- use Illuminate\Support\ServiceProvider;
- use Illuminate\Support\Facades\Event;
- use App\Module\Cleanup\Commands\ScanTablesCommand;
- use App\Module\Cleanup\Commands\CleanupDataCommand;
- use App\Module\Cleanup\Commands\InsertCleanupAdminMenuCommand;
- /**
- * Cleanup 模块服务提供者
- *
- * 注册模块的服务、命令、路由等
- */
- class CleanupServiceProvider extends ServiceProvider
- {
- /**
- * 注册服务
- */
- public function register(): void
- {
- // 注册配置文件
- $this->mergeConfigFrom(
- __DIR__ . '/config/cleanup.php',
- 'cleanup'
- );
- // 注册服务单例
- $this->app->singleton('cleanup.service', function ($app) {
- return new Services\CleanupService();
- });
- }
- /**
- * 启动服务
- */
- public function boot(): void
- {
- // 发布配置文件
- $this->publishes([
- __DIR__ . '/config/cleanup.php' => config_path('cleanup.php'),
- ], 'cleanup-config');
- // 注册命令
- if ($this->app->runningInConsole()) {
- $this->commands([
- ScanTablesCommand::class,
- CleanupDataCommand::class,
- InsertCleanupAdminMenuCommand::class,
- ]);
- }
- // 注册事件监听器
- $this->registerEventListeners();
- // 路由通过 route-attributes 自动注册,无需手动注册
- }
- /**
- * 注册事件监听器
- */
- protected function registerEventListeners(): void
- {
- // 任务状态变更事件
- Event::listen(
- \App\Module\Cleanup\Events\TaskStatusChanged::class,
- \App\Module\Cleanup\Listeners\TaskStatusChangedListener::class
- );
- // 备份完成事件
- Event::listen(
- \App\Module\Cleanup\Events\BackupCompleted::class,
- \App\Module\Cleanup\Listeners\BackupCompletedListener::class
- );
- // 清理完成事件
- Event::listen(
- \App\Module\Cleanup\Events\CleanupCompleted::class,
- \App\Module\Cleanup\Listeners\CleanupCompletedListener::class
- );
- }
- /**
- * 获取提供的服务
- */
- public function provides(): array
- {
- return [
- 'cleanup.service',
- ];
- }
- }
|