| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- <?php
- namespace App\Module\System\Providers;
- 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\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();
- }
- /**
- * 注册事件和监听器
- *
- * @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);
- }
- }
- }
|