ThirdPartyServiceProvider.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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->registerViews();
  38. // 注册命令
  39. $this->registerCommands();
  40. // 注册事件监听器
  41. $this->registerEventListeners();
  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. // 注册Webhook分发服务
  73. $this->app->singleton('thirdparty.webhook', function () {
  74. return new \App\Module\ThirdParty\Services\WebhookDispatchService();
  75. });
  76. // 注册验证器
  77. $this->app->singleton(\App\Module\ThirdParty\Validators\ServiceValidator::class);
  78. $this->app->singleton(\App\Module\ThirdParty\Validators\CredentialValidator::class);
  79. }
  80. /**
  81. * 注册路由
  82. *
  83. * @return void
  84. */
  85. protected function registerRoutes()
  86. {
  87. // 注册后台管理路由
  88. if (file_exists($adminRoutes = __DIR__ . '/../Routes/admin.php')) {
  89. Route::middleware(['web', 'admin'])
  90. ->prefix(config('admin.route.prefix', 'admin') . '/thirdparty')
  91. ->group($adminRoutes);
  92. }
  93. // 注册Webhook路由
  94. if (file_exists($webhookRoutes = __DIR__ . '/../Routes/webhook.php')) {
  95. Route::middleware(['api'])
  96. ->prefix('thirdParty/webhook')
  97. ->name('thirdparty.webhook.')
  98. ->group($webhookRoutes);
  99. }
  100. // 注册API路由(如果需要)
  101. if (file_exists($apiRoutes = __DIR__ . '/../Routes/api.php')) {
  102. Route::middleware(['api'])
  103. ->prefix('api/thirdparty')
  104. ->group($apiRoutes);
  105. }
  106. }
  107. /**
  108. * 注册视图
  109. *
  110. * @return void
  111. */
  112. protected function registerViews()
  113. {
  114. // 注册视图命名空间
  115. $this->loadViewsFrom(__DIR__ . '/../Views', 'thirdparty');
  116. }
  117. /**
  118. * 注册命令
  119. *
  120. * @return void
  121. */
  122. protected function registerCommands()
  123. {
  124. if ($this->app->runningInConsole()) {
  125. $this->commands([
  126. \App\Module\ThirdParty\Commands\InsertThirdPartyAdminMenu::class,
  127. \App\Module\ThirdParty\Commands\RestructureExternalManagementMenu::class,
  128. \App\Module\ThirdParty\Commands\HealthCheckCommand::class,
  129. \App\Module\ThirdParty\Commands\QuotaResetCommand::class,
  130. \App\Module\ThirdParty\Commands\CleanupLogsCommand::class,
  131. \App\Module\ThirdParty\Commands\SyncServicesCommand::class,
  132. \App\Module\ThirdParty\Commands\TestServiceCommand::class,
  133. \App\Module\ThirdParty\Commands\TestBaseArchitectureCommand::class,
  134. \App\Module\ThirdParty\Commands\WebhookMappingCommand::class,
  135. \App\Module\ThirdParty\Commands\TestUrsRequestCommand::class,
  136. ]);
  137. }
  138. }
  139. /**
  140. * 注册事件监听器
  141. *
  142. * @return void
  143. */
  144. protected function registerEventListeners()
  145. {
  146. // 注册事件监听器
  147. $events = [
  148. // API调用事件监听器
  149. \App\Module\ThirdParty\Events\ApiCallEvent::class => [
  150. \App\Module\ThirdParty\Listeners\ApiCallLogListener::class,
  151. \App\Module\ThirdParty\Listeners\QuotaManagementListener::class,
  152. ],
  153. // 服务状态变更事件监听器
  154. \App\Module\ThirdParty\Events\ServiceStatusChangedEvent::class => [
  155. \App\Module\ThirdParty\Listeners\ServiceStatusMonitorListener::class,
  156. \App\Module\ThirdParty\Listeners\AlertNotificationListener::class . '@handleServiceStatusChanged',
  157. ],
  158. // 配额告警事件监听器
  159. \App\Module\ThirdParty\Events\QuotaAlertEvent::class => [
  160. \App\Module\ThirdParty\Listeners\AlertNotificationListener::class . '@handleQuotaAlert',
  161. ],
  162. // 监控告警事件监听器
  163. \App\Module\ThirdParty\Events\MonitorAlertEvent::class => [
  164. \App\Module\ThirdParty\Listeners\AlertNotificationListener::class . '@handleMonitorAlert',
  165. ],
  166. ];
  167. foreach ($events as $event => $listeners) {
  168. foreach ($listeners as $listener) {
  169. \Illuminate\Support\Facades\Event::listen($event, $listener);
  170. }
  171. }
  172. }
  173. /**
  174. * 发布资源
  175. *
  176. * @return void
  177. */
  178. protected function publishResources()
  179. {
  180. if ($this->app->runningInConsole()) {
  181. // 发布配置文件
  182. $this->publishes([
  183. __DIR__ . '/../Config/thirdparty.php' => config_path('thirdparty.php'),
  184. ], 'thirdparty-config');
  185. // 发布数据库迁移文件
  186. $this->publishes([
  187. __DIR__ . '/../Databases/GenerateSql/thirdparty_tables.sql' => database_path('sql/thirdparty_tables.sql'),
  188. ], 'thirdparty-migrations');
  189. // 发布视图文件(如果有)
  190. if (is_dir(__DIR__ . '/../Resources/views')) {
  191. $this->publishes([
  192. __DIR__ . '/../Resources/views' => resource_path('views/vendor/thirdparty'),
  193. ], 'thirdparty-views');
  194. }
  195. // 发布前端资源(如果有)
  196. if (is_dir(__DIR__ . '/../Resources/assets')) {
  197. $this->publishes([
  198. __DIR__ . '/../Resources/assets' => public_path('vendor/thirdparty'),
  199. ], 'thirdparty-assets');
  200. }
  201. }
  202. }
  203. /**
  204. * 获取提供的服务
  205. *
  206. * @return array
  207. */
  208. public function provides()
  209. {
  210. return [
  211. 'thirdparty.service',
  212. 'thirdparty.credential',
  213. 'thirdparty.monitor',
  214. 'thirdparty.log',
  215. 'thirdparty.quota',
  216. 'thirdparty.webhook',
  217. ];
  218. }
  219. }