| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- <?php
- namespace App\Module\System\Providers;
- use App\Module\System\Commands\CleanJobRunsCommand;
- use App\Module\System\Events\ConfigChangedEvent;
- use App\Module\System\Events\SystemLogCreatedEvent;
- use App\Module\System\Events\ViewConfigChangedEvent;
- use App\Module\System\Listeners\SystemEventListener;
- use Illuminate\Console\Scheduling\Schedule;
- use Illuminate\Support\ServiceProvider;
- /**
- * 系统模块服务提供者
- */
- class SystemServiceProvider extends ServiceProvider
- {
- /**
- * 事件到监听器的映射
- *
- * @var array
- */
- protected $listen = [
- ConfigChangedEvent::class => [
- SystemEventListener::class . '@handleConfigChanged',
- ],
- ViewConfigChangedEvent::class => [
- SystemEventListener::class . '@handleViewConfigChanged',
- ],
- SystemLogCreatedEvent::class => [
- SystemEventListener::class . '@handleSystemLogCreated',
- ],
- ];
- /**
- * 需要注册的订阅者
- *
- * @var array
- */
- protected $subscribe = [
- SystemEventListener::class,
- ];
- /**
- * 注册服务
- *
- * @return void
- */
- public function register(): void
- {
- // 不注册服务
- // // 注册服务...
- // $this->app->singleton('system.config.service', function ($app) {
- // return new \App\Module\System\Services\ConfigService();
- // });
- //
- // $this->app->singleton('system.view.config.service', function ($app) {
- // return new \App\Module\System\Services\ViewConfigService();
- // });
- //
- // $this->app->singleton('system.log.service', function ($app) {
- // return new \App\Module\System\Services\LogService();
- // });
- }
- /**
- * 启动服务
- *
- * @return void
- */
- public function boot(): void
- {
- // 注册事件监听器
- $this->registerEvents();
- // 注册命令
- $this->registerCommands();
- // 注册定时任务
- $this->registerSchedules();
- }
- /**
- * 注册事件和监听器
- *
- * @return void
- */
- protected function registerEvents(): void
- {
- $events = $this->app['events'];
- foreach ($this->listen as $event => $listeners) {
- foreach ($listeners as $listener) {
- $events->listen($event, $listener);
- }
- }
- foreach ($this->subscribe as $subscriber) {
- $events->subscribe($subscriber);
- }
- }
- /**
- * 注册命令
- *
- * @return void
- */
- protected function registerCommands(): void
- {
- if ($this->app->runningInConsole()) {
- $this->commands([
- CleanJobRunsCommand::class,
- ]);
- }
- }
- /**
- * 注册System模块相关的定时任务
- *
- * @return void
- */
- protected function registerSchedules(): void
- {
- // 在应用完全启动后注册定时任务
- $this->app->booted(function () {
- $schedule = $this->app->make(Schedule::class);
- // 每小时清理job_runs表,保留5天数据
- $schedule->command('system:clean-job-runs')
- ->hourly()
- ->description('清理job_runs表过期记录(保留5天)')
- ->withoutOverlapping() // 防止重复执行
- ->runInBackground(); // 后台运行
- });
- }
- }
|