AdminServiceProvider.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. namespace App\Module\Admin\Providers;
  3. use App\Module\Admin\Events\AdminActionEvent;
  4. use App\Module\Admin\Listeners\AdminActionListener;
  5. use Illuminate\Support\Facades\Event;
  6. use Illuminate\Support\ServiceProvider;
  7. /**
  8. * Admin模块服务提供者
  9. *
  10. * 负责注册Admin模块的服务、事件监听器等
  11. */
  12. class AdminServiceProvider extends ServiceProvider
  13. {
  14. /**
  15. * 注册服务
  16. *
  17. * @return void
  18. */
  19. public function register()
  20. {
  21. // 注册配置文件
  22. $this->mergeConfigFrom(
  23. __DIR__ . '/../Config/admin.php',
  24. 'admin_module'
  25. );
  26. // 注册服务
  27. $this->registerServices();
  28. }
  29. /**
  30. * 启动服务
  31. *
  32. * @return void
  33. */
  34. public function boot()
  35. {
  36. // 注册事件监听器
  37. $this->registerEventListeners();
  38. // 注册命令
  39. $this->registerCommands();
  40. // 发布资源
  41. $this->publishResources();
  42. }
  43. /**
  44. * 注册服务
  45. *
  46. * @return void
  47. */
  48. protected function registerServices()
  49. {
  50. // 注册Admin服务
  51. $this->app->singleton('admin.service', function ($app) {
  52. return new \App\Module\Admin\Services\AdminService();
  53. });
  54. // 注册缓存服务
  55. $this->app->singleton('admin.cache', function ($app) {
  56. return new \App\Module\Admin\Services\CacheService();
  57. });
  58. // 注册日志服务
  59. $this->app->singleton('admin.log', function ($app) {
  60. return new \App\Module\Admin\Services\LogService();
  61. });
  62. }
  63. /**
  64. * 注册事件监听器
  65. *
  66. * @return void
  67. */
  68. protected function registerEventListeners()
  69. {
  70. Event::listen(
  71. AdminActionEvent::class,
  72. AdminActionListener::class
  73. );
  74. }
  75. /**
  76. * 注册命令
  77. *
  78. * @return void
  79. */
  80. protected function registerCommands()
  81. {
  82. if ($this->app->runningInConsole()) {
  83. // 暂时注释掉不存在的命令类
  84. // $this->commands([
  85. // \App\Module\Admin\Commands\AdminCacheCommand::class,
  86. // \App\Module\Admin\Commands\AdminMaintenanceCommand::class,
  87. // ]);
  88. }
  89. }
  90. /**
  91. * 发布资源
  92. *
  93. * @return void
  94. */
  95. protected function publishResources()
  96. {
  97. // 发布配置文件
  98. $this->publishes([
  99. __DIR__ . '/../Config/admin.php' => config_path('admin_module.php'),
  100. ], 'admin-config');
  101. // 发布视图文件
  102. $this->publishes([
  103. __DIR__ . '/../Resources/views' => resource_path('views/admin'),
  104. ], 'admin-views');
  105. // 发布静态资源
  106. $this->publishes([
  107. __DIR__ . '/../Resources/assets' => public_path('vendor/admin'),
  108. ], 'admin-assets');
  109. }
  110. /**
  111. * 获取提供的服务
  112. *
  113. * @return array
  114. */
  115. public function provides()
  116. {
  117. return [
  118. 'admin.service',
  119. 'admin.cache',
  120. 'admin.log',
  121. ];
  122. }
  123. }