AdminServiceProvider.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. $this->commands([
  84. \App\Module\Admin\Commands\AdminCacheCommand::class,
  85. \App\Module\Admin\Commands\AdminMaintenanceCommand::class,
  86. ]);
  87. }
  88. }
  89. /**
  90. * 发布资源
  91. *
  92. * @return void
  93. */
  94. protected function publishResources()
  95. {
  96. // 发布配置文件
  97. $this->publishes([
  98. __DIR__ . '/../Config/admin.php' => config_path('admin_module.php'),
  99. ], 'admin-config');
  100. // 发布视图文件
  101. $this->publishes([
  102. __DIR__ . '/../Resources/views' => resource_path('views/admin'),
  103. ], 'admin-views');
  104. // 发布静态资源
  105. $this->publishes([
  106. __DIR__ . '/../Resources/assets' => public_path('vendor/admin'),
  107. ], 'admin-assets');
  108. }
  109. /**
  110. * 获取提供的服务
  111. *
  112. * @return array
  113. */
  114. public function provides()
  115. {
  116. return [
  117. 'admin.service',
  118. 'admin.cache',
  119. 'admin.log',
  120. ];
  121. }
  122. }