| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- <?php
- namespace App\Module\Cleanup;
- use Illuminate\Support\ServiceProvider;
- use App\Module\Cleanup\Commands\ScanTablesCommand;
- use App\Module\Cleanup\Commands\CleanupDataCommand;
- /**
- * 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');
- // 发布数据库迁移文件
- $this->publishes([
- __DIR__ . '/Databases/GenerateSql/' => database_path('sql/cleanup/'),
- ], 'cleanup-migrations');
- // 注册命令
- if ($this->app->runningInConsole()) {
- $this->commands([
- ScanTablesCommand::class,
- CleanupDataCommand::class,
- ]);
- }
- // 注册路由
- $this->loadRoutesFrom(__DIR__ . '/routes/web.php');
- $this->loadRoutesFrom(__DIR__ . '/routes/api.php');
- // 注册视图
- $this->loadViewsFrom(__DIR__ . '/resources/views', 'cleanup');
- // 发布视图文件
- $this->publishes([
- __DIR__ . '/resources/views' => resource_path('views/vendor/cleanup'),
- ], 'cleanup-views');
- // 发布前端资源
- $this->publishes([
- __DIR__ . '/resources/assets' => public_path('vendor/cleanup'),
- ], 'cleanup-assets');
- // 注册事件监听器
- $this->registerEventListeners();
- // 注册中间件
- $this->registerMiddleware();
- // 注册调度任务
- $this->registerScheduledTasks();
- }
- /**
- * 注册事件监听器
- */
- 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
- );
- }
- /**
- * 注册中间件
- */
- protected function registerMiddleware(): void
- {
- // 注册清理权限中间件
- $this->app['router']->aliasMiddleware(
- 'cleanup.permission',
- \App\Module\Cleanup\Middleware\CleanupPermissionMiddleware::class
- );
- // 注册任务状态检查中间件
- $this->app['router']->aliasMiddleware(
- 'cleanup.task-status',
- \App\Module\Cleanup\Middleware\TaskStatusMiddleware::class
- );
- }
- /**
- * 注册调度任务
- */
- protected function registerScheduledTasks(): void
- {
- // 注册定时清理过期备份的任务
- $this->app->booted(function () {
- $schedule = $this->app->make(\Illuminate\Console\Scheduling\Schedule::class);
-
- // 每天凌晨2点清理过期备份
- $schedule->call(function () {
- \App\Module\Cleanup\Services\CleanupService::cleanExpiredBackups(
- config('cleanup.backup.retention_days', 30)
- );
- })->dailyAt('02:00')->name('cleanup-expired-backups');
- // 每天凌晨3点清理历史日志
- $schedule->call(function () {
- \App\Module\Cleanup\Services\CleanupService::cleanHistoryLogs(
- config('cleanup.log.retention_days', 30)
- );
- })->dailyAt('03:00')->name('cleanup-history-logs');
- // 每小时更新表统计信息
- $schedule->call(function () {
- \App\Module\Cleanup\Services\CleanupService::scanTables(false);
- })->hourly()->name('update-table-stats');
- });
- }
- /**
- * 获取提供的服务
- */
- public function provides(): array
- {
- return [
- 'cleanup.service',
- ];
- }
- }
|