SystemServiceProvider.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace App\Module\System\Providers;
  3. use App\Module\System\Events\ConfigChangedEvent;
  4. use App\Module\System\Events\SystemLogCreatedEvent;
  5. use App\Module\System\Events\ViewConfigChangedEvent;
  6. use App\Module\System\Listeners\SystemEventListener;
  7. use Illuminate\Support\ServiceProvider;
  8. /**
  9. * 系统模块服务提供者
  10. */
  11. class SystemServiceProvider extends ServiceProvider
  12. {
  13. /**
  14. * 事件到监听器的映射
  15. *
  16. * @var array
  17. */
  18. protected $listen = [
  19. ConfigChangedEvent::class => [
  20. SystemEventListener::class . '@handleConfigChanged',
  21. ],
  22. ViewConfigChangedEvent::class => [
  23. SystemEventListener::class . '@handleViewConfigChanged',
  24. ],
  25. SystemLogCreatedEvent::class => [
  26. SystemEventListener::class . '@handleSystemLogCreated',
  27. ],
  28. ];
  29. /**
  30. * 需要注册的订阅者
  31. *
  32. * @var array
  33. */
  34. protected $subscribe = [
  35. SystemEventListener::class,
  36. ];
  37. /**
  38. * 注册服务
  39. *
  40. * @return void
  41. */
  42. public function register(): void
  43. {
  44. // 注册服务...
  45. $this->app->singleton('system.config.service', function ($app) {
  46. return new \App\Module\System\Services\ConfigService();
  47. });
  48. $this->app->singleton('system.view.config.service', function ($app) {
  49. return new \App\Module\System\Services\ViewConfigService();
  50. });
  51. $this->app->singleton('system.log.service', function ($app) {
  52. return new \App\Module\System\Services\LogService();
  53. });
  54. }
  55. /**
  56. * 启动服务
  57. *
  58. * @return void
  59. */
  60. public function boot(): void
  61. {
  62. // 注册事件监听器
  63. $this->registerEvents();
  64. }
  65. /**
  66. * 注册事件和监听器
  67. *
  68. * @return void
  69. */
  70. protected function registerEvents(): void
  71. {
  72. $events = $this->app['events'];
  73. foreach ($this->listen as $event => $listeners) {
  74. foreach ($listeners as $listener) {
  75. $events->listen($event, $listener);
  76. }
  77. }
  78. foreach ($this->subscribe as $subscriber) {
  79. $events->subscribe($subscriber);
  80. }
  81. }
  82. }