| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- <?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\ScanModelsCommand;
- use App\Module\Cleanup\Commands\TestModelCleanupCommand;
- use App\Module\Cleanup\Commands\ValidateModelCleanupCommand;
- 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,
- ScanModelsCommand::class,
- TestModelCleanupCommand::class,
- ValidateModelCleanupCommand::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',
- ];
- }
- }
|