ThirdPartyServiceProvider.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. namespace App\Module\ThirdParty\Providers;
  3. use Illuminate\Support\ServiceProvider;
  4. use Illuminate\Support\Facades\Route;
  5. /**
  6. * ThirdParty模块服务提供者
  7. *
  8. * 专注于第三方服务管理功能,提供统一的第三方服务接入和管理
  9. */
  10. class ThirdPartyServiceProvider extends ServiceProvider
  11. {
  12. /**
  13. * 注册服务
  14. *
  15. * @return void
  16. */
  17. public function register()
  18. {
  19. // 注册配置文件
  20. $this->mergeConfigFrom(
  21. __DIR__ . '/../Config/thirdparty.php',
  22. 'thirdparty'
  23. );
  24. // 注册服务
  25. $this->registerServices();
  26. }
  27. /**
  28. * 启动服务
  29. *
  30. * @return void
  31. */
  32. public function boot()
  33. {
  34. // 注册路由
  35. $this->registerRoutes();
  36. // 注册命令
  37. $this->registerCommands();
  38. // 注册事件监听器
  39. $this->registerEventListeners();
  40. // 注册命令
  41. $this->registerCommands();
  42. // 发布资源
  43. $this->publishResources();
  44. }
  45. /**
  46. * 注册服务
  47. *
  48. * @return void
  49. */
  50. protected function registerServices()
  51. {
  52. // 注册第三方服务核心服务
  53. $this->app->singleton('thirdparty.service', function () {
  54. return new \App\Module\ThirdParty\Services\ThirdPartyService();
  55. });
  56. // 注册凭证管理服务
  57. $this->app->singleton('thirdparty.credential', function () {
  58. return new \App\Module\ThirdParty\Services\CredentialService();
  59. });
  60. // 注册监控服务
  61. $this->app->singleton('thirdparty.monitor', function () {
  62. return new \App\Module\ThirdParty\Services\MonitorService();
  63. });
  64. // 注册日志服务
  65. $this->app->singleton('thirdparty.log', function () {
  66. return new \App\Module\ThirdParty\Services\LogService();
  67. });
  68. // 注册配额管理服务
  69. $this->app->singleton('thirdparty.quota', function () {
  70. return new \App\Module\ThirdParty\Services\QuotaService();
  71. });
  72. // 注册验证器
  73. $this->app->singleton(\App\Module\ThirdParty\Validators\ServiceValidator::class);
  74. $this->app->singleton(\App\Module\ThirdParty\Validators\CredentialValidator::class);
  75. }
  76. /**
  77. * 注册路由
  78. *
  79. * @return void
  80. */
  81. protected function registerRoutes()
  82. {
  83. // 注册后台管理路由
  84. if (file_exists($adminRoutes = __DIR__ . '/../Routes/admin.php')) {
  85. Route::middleware(['web', 'admin'])
  86. ->prefix(config('admin.route.prefix', 'admin') . '/thirdparty')
  87. ->group($adminRoutes);
  88. }
  89. // 注册API路由(如果需要)
  90. if (file_exists($apiRoutes = __DIR__ . '/../Routes/api.php')) {
  91. Route::middleware(['api'])
  92. ->prefix('api/thirdparty')
  93. ->group($apiRoutes);
  94. }
  95. }
  96. /**
  97. * 注册命令
  98. *
  99. * @return void
  100. */
  101. protected function registerCommands()
  102. {
  103. if ($this->app->runningInConsole()) {
  104. $this->commands([
  105. \App\Module\ThirdParty\Commands\InsertThirdPartyAdminMenu::class,
  106. \App\Module\ThirdParty\Commands\RestructureExternalManagementMenu::class,
  107. \App\Module\ThirdParty\Commands\HealthCheckCommand::class,
  108. \App\Module\ThirdParty\Commands\QuotaResetCommand::class,
  109. \App\Module\ThirdParty\Commands\CleanupLogsCommand::class,
  110. \App\Module\ThirdParty\Commands\SyncServicesCommand::class,
  111. \App\Module\ThirdParty\Commands\TestServiceCommand::class,
  112. ]);
  113. }
  114. }
  115. /**
  116. * 注册事件监听器
  117. *
  118. * @return void
  119. */
  120. protected function registerEventListeners()
  121. {
  122. // 注册事件监听器
  123. $events = [
  124. // API调用事件监听器
  125. \App\Module\ThirdParty\Events\ApiCallEvent::class => [
  126. \App\Module\ThirdParty\Listeners\ApiCallLogListener::class,
  127. \App\Module\ThirdParty\Listeners\QuotaManagementListener::class,
  128. ],
  129. // 服务状态变更事件监听器
  130. \App\Module\ThirdParty\Events\ServiceStatusChangedEvent::class => [
  131. \App\Module\ThirdParty\Listeners\ServiceStatusMonitorListener::class,
  132. \App\Module\ThirdParty\Listeners\AlertNotificationListener::class . '@handleServiceStatusChanged',
  133. ],
  134. // 配额告警事件监听器
  135. \App\Module\ThirdParty\Events\QuotaAlertEvent::class => [
  136. \App\Module\ThirdParty\Listeners\AlertNotificationListener::class . '@handleQuotaAlert',
  137. ],
  138. // 监控告警事件监听器
  139. \App\Module\ThirdParty\Events\MonitorAlertEvent::class => [
  140. \App\Module\ThirdParty\Listeners\AlertNotificationListener::class . '@handleMonitorAlert',
  141. ],
  142. ];
  143. foreach ($events as $event => $listeners) {
  144. foreach ($listeners as $listener) {
  145. \Illuminate\Support\Facades\Event::listen($event, $listener);
  146. }
  147. }
  148. }
  149. /**
  150. * 发布资源
  151. *
  152. * @return void
  153. */
  154. protected function publishResources()
  155. {
  156. if ($this->app->runningInConsole()) {
  157. // 发布配置文件
  158. $this->publishes([
  159. __DIR__ . '/../Config/thirdparty.php' => config_path('thirdparty.php'),
  160. ], 'thirdparty-config');
  161. // 发布数据库迁移文件
  162. $this->publishes([
  163. __DIR__ . '/../Databases/GenerateSql/thirdparty_tables.sql' => database_path('sql/thirdparty_tables.sql'),
  164. ], 'thirdparty-migrations');
  165. // 发布视图文件(如果有)
  166. if (is_dir(__DIR__ . '/../Resources/views')) {
  167. $this->publishes([
  168. __DIR__ . '/../Resources/views' => resource_path('views/vendor/thirdparty'),
  169. ], 'thirdparty-views');
  170. }
  171. // 发布前端资源(如果有)
  172. if (is_dir(__DIR__ . '/../Resources/assets')) {
  173. $this->publishes([
  174. __DIR__ . '/../Resources/assets' => public_path('vendor/thirdparty'),
  175. ], 'thirdparty-assets');
  176. }
  177. }
  178. }
  179. /**
  180. * 获取提供的服务
  181. *
  182. * @return array
  183. */
  184. public function provides()
  185. {
  186. return [
  187. 'thirdparty.service',
  188. 'thirdparty.credential',
  189. 'thirdparty.monitor',
  190. 'thirdparty.log',
  191. 'thirdparty.quota',
  192. ];
  193. }
  194. }