ThirdPartyServiceProvider.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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->publishResources();
  42. }
  43. /**
  44. * 注册服务
  45. *
  46. * @return void
  47. */
  48. protected function registerServices()
  49. {
  50. // 注册第三方服务核心服务
  51. $this->app->singleton('thirdparty.service', function () {
  52. return new \App\Module\ThirdParty\Services\ThirdPartyService();
  53. });
  54. // 注册凭证管理服务
  55. $this->app->singleton('thirdparty.credential', function () {
  56. return new \App\Module\ThirdParty\Services\CredentialService();
  57. });
  58. // 注册监控服务
  59. $this->app->singleton('thirdparty.monitor', function () {
  60. return new \App\Module\ThirdParty\Services\MonitorService();
  61. });
  62. // 注册日志服务
  63. $this->app->singleton('thirdparty.log', function () {
  64. return new \App\Module\ThirdParty\Services\LogService();
  65. });
  66. // 注册配额管理服务
  67. $this->app->singleton('thirdparty.quota', function () {
  68. return new \App\Module\ThirdParty\Services\QuotaService();
  69. });
  70. // 注册验证器
  71. $this->app->singleton(\App\Module\ThirdParty\Validators\ServiceValidator::class);
  72. $this->app->singleton(\App\Module\ThirdParty\Validators\CredentialValidator::class);
  73. }
  74. /**
  75. * 注册路由
  76. *
  77. * @return void
  78. */
  79. protected function registerRoutes()
  80. {
  81. // 注册后台管理路由
  82. if (file_exists($adminRoutes = __DIR__ . '/../Routes/admin.php')) {
  83. Route::middleware(['web', 'admin'])
  84. ->prefix(config('admin.route.prefix', 'admin'))
  85. ->group($adminRoutes);
  86. }
  87. // 注册API路由(如果需要)
  88. if (file_exists($apiRoutes = __DIR__ . '/../Routes/api.php')) {
  89. Route::middleware(['api'])
  90. ->prefix('api/thirdparty')
  91. ->group($apiRoutes);
  92. }
  93. }
  94. /**
  95. * 注册命令
  96. *
  97. * @return void
  98. */
  99. protected function registerCommands()
  100. {
  101. if ($this->app->runningInConsole()) {
  102. $this->commands([
  103. \App\Module\ThirdParty\Commands\HealthCheckCommand::class,
  104. \App\Module\ThirdParty\Commands\QuotaResetCommand::class,
  105. \App\Module\ThirdParty\Commands\CleanupLogsCommand::class,
  106. \App\Module\ThirdParty\Commands\SyncServicesCommand::class,
  107. \App\Module\ThirdParty\Commands\TestServiceCommand::class,
  108. ]);
  109. }
  110. }
  111. /**
  112. * 注册事件监听器
  113. *
  114. * @return void
  115. */
  116. protected function registerEventListeners()
  117. {
  118. // 注册事件监听器
  119. $events = [
  120. \App\Module\ThirdParty\Events\ServiceCreatedEvent::class => [
  121. \App\Module\ThirdParty\Listeners\ServiceCreatedListener::class,
  122. ],
  123. \App\Module\ThirdParty\Events\ServiceStatusChangedEvent::class => [
  124. \App\Module\ThirdParty\Listeners\ServiceStatusChangedListener::class,
  125. ],
  126. \App\Module\ThirdParty\Events\QuotaExceededEvent::class => [
  127. \App\Module\ThirdParty\Listeners\QuotaExceededListener::class,
  128. ],
  129. \App\Module\ThirdParty\Events\HealthCheckFailedEvent::class => [
  130. \App\Module\ThirdParty\Listeners\HealthCheckFailedListener::class,
  131. ],
  132. \App\Module\ThirdParty\Events\ApiCallFailedEvent::class => [
  133. \App\Module\ThirdParty\Listeners\ApiCallFailedListener::class,
  134. ],
  135. ];
  136. foreach ($events as $event => $listeners) {
  137. foreach ($listeners as $listener) {
  138. \Illuminate\Support\Facades\Event::listen($event, $listener);
  139. }
  140. }
  141. }
  142. /**
  143. * 发布资源
  144. *
  145. * @return void
  146. */
  147. protected function publishResources()
  148. {
  149. if ($this->app->runningInConsole()) {
  150. // 发布配置文件
  151. $this->publishes([
  152. __DIR__ . '/../Config/thirdparty.php' => config_path('thirdparty.php'),
  153. ], 'thirdparty-config');
  154. // 发布数据库迁移文件
  155. $this->publishes([
  156. __DIR__ . '/../Databases/GenerateSql/thirdparty_tables.sql' => database_path('sql/thirdparty_tables.sql'),
  157. ], 'thirdparty-migrations');
  158. // 发布视图文件(如果有)
  159. if (is_dir(__DIR__ . '/../Resources/views')) {
  160. $this->publishes([
  161. __DIR__ . '/../Resources/views' => resource_path('views/vendor/thirdparty'),
  162. ], 'thirdparty-views');
  163. }
  164. // 发布前端资源(如果有)
  165. if (is_dir(__DIR__ . '/../Resources/assets')) {
  166. $this->publishes([
  167. __DIR__ . '/../Resources/assets' => public_path('vendor/thirdparty'),
  168. ], 'thirdparty-assets');
  169. }
  170. }
  171. }
  172. /**
  173. * 获取提供的服务
  174. *
  175. * @return array
  176. */
  177. public function provides()
  178. {
  179. return [
  180. 'thirdparty.service',
  181. 'thirdparty.credential',
  182. 'thirdparty.monitor',
  183. 'thirdparty.log',
  184. 'thirdparty.quota',
  185. ];
  186. }
  187. }