OpenAPIServiceProvider.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. namespace App\Module\OpenAPI\Providers;
  3. use App\Module\OpenAPI\Events\AppCreatedEvent;
  4. use App\Module\OpenAPI\Listeners\AppCreatedListener;
  5. use App\Module\OpenAPI\Middleware\ApiAuthMiddleware;
  6. use App\Module\OpenAPI\Middleware\RateLimitMiddleware;
  7. use App\Module\OpenAPI\Middleware\ScopeMiddleware;
  8. use App\Module\OpenAPI\Middleware\IpWhitelistMiddleware;
  9. use Illuminate\Support\Facades\Event;
  10. use Illuminate\Support\Facades\Route;
  11. use Illuminate\Support\ServiceProvider;
  12. /**
  13. * OpenAPI模块服务提供者
  14. *
  15. * 专注于后台管理功能,使用注解路由,不注册API路由和资源
  16. */
  17. class OpenAPIServiceProvider extends ServiceProvider
  18. {
  19. /**
  20. * 注册服务
  21. *
  22. * @return void
  23. */
  24. public function register()
  25. {
  26. // 注册配置文件
  27. $this->mergeConfigFrom(
  28. __DIR__ . '/../Config/openapi.php',
  29. 'openapi'
  30. );
  31. // 注册服务
  32. $this->registerServices();
  33. // 注册Handler
  34. $this->registerHandlers();
  35. }
  36. /**
  37. * 启动服务
  38. *
  39. * @return void
  40. */
  41. public function boot()
  42. {
  43. // 注册路由
  44. $this->registerRoutes();
  45. // 注册中间件
  46. $this->registerMiddleware();
  47. // 注册事件监听器
  48. $this->registerEventListeners();
  49. // 注册命令
  50. $this->registerCommands();
  51. }
  52. /**
  53. * 注册服务
  54. *
  55. * @return void
  56. */
  57. protected function registerServices()
  58. {
  59. // 注册OpenAPI服务
  60. $this->app->singleton('openapi.service', function () {
  61. return new \App\Module\OpenAPI\Services\OpenApiService();
  62. });
  63. // 注册认证服务
  64. $this->app->singleton('openapi.auth', function ($app) {
  65. return new \App\Module\OpenAPI\Services\AuthService(
  66. $app->make('openapi.service')
  67. );
  68. });
  69. // 注册日志服务
  70. $this->app->singleton('openapi.log', function () {
  71. return new \App\Module\OpenAPI\Services\LogService();
  72. });
  73. // 注册频率限制服务
  74. $this->app->singleton('openapi.rate_limit', function () {
  75. return new \App\Module\OpenAPI\Services\RateLimitService();
  76. });
  77. // 注册权限范围服务
  78. $this->app->singleton('openapi.scope', function () {
  79. return new \App\Module\OpenAPI\Services\ScopeService();
  80. });
  81. // 注册Webhook服务
  82. $this->app->singleton('openapi.webhook', function () {
  83. return new \App\Module\OpenAPI\Services\WebhookService();
  84. });
  85. }
  86. /**
  87. * 注册Handler
  88. *
  89. * @return void
  90. */
  91. protected function registerHandlers()
  92. {
  93. $handlerService = new \App\Module\OpenAPI\Services\HandlerRegistrationService();
  94. $handlerService->registerHandlers();
  95. }
  96. /**
  97. * 注册路由
  98. *
  99. * @return void
  100. */
  101. protected function registerRoutes()
  102. {
  103. // 使用路由注册服务来注册路由
  104. $routeService = new \App\Module\OpenAPI\Services\RouteRegistrationService();
  105. $routeService->registerAllRoutes();
  106. // 注册后台管理路由(保留原有的后台路由文件)
  107. if (file_exists($adminRoutes = __DIR__ . '/../Routes/admin.php')) {
  108. Route::middleware(['web', 'admin'])
  109. ->prefix(config('admin.route.prefix', 'admin'))
  110. ->group($adminRoutes);
  111. }
  112. }
  113. /**
  114. * 注册中间件
  115. *
  116. * @return void
  117. */
  118. protected function registerMiddleware()
  119. {
  120. $router = $this->app['router'];
  121. // 注册中间件别名
  122. $router->aliasMiddleware('openapi.auth', ApiAuthMiddleware::class);
  123. $router->aliasMiddleware('openapi.rate_limit', RateLimitMiddleware::class);
  124. $router->aliasMiddleware('openapi.scope', ScopeMiddleware::class);
  125. $router->aliasMiddleware('openapi.ip_whitelist', IpWhitelistMiddleware::class);
  126. // 注册中间件组
  127. $router->middlewareGroup('openapi', [
  128. 'openapi.auth',
  129. 'openapi.rate_limit',
  130. 'openapi.scope',
  131. 'openapi.ip_whitelist',
  132. ]);
  133. }
  134. /**
  135. * 注册事件监听器
  136. *
  137. * @return void
  138. */
  139. protected function registerEventListeners()
  140. {
  141. Event::listen(
  142. AppCreatedEvent::class,
  143. AppCreatedListener::class
  144. );
  145. }
  146. /**
  147. * 注册命令
  148. *
  149. * @return void
  150. */
  151. protected function registerCommands()
  152. {
  153. if ($this->app->runningInConsole()) {
  154. $this->commands([
  155. \App\Module\OpenAPI\Commands\GenerateApiKeyCommand::class,
  156. \App\Module\OpenAPI\Commands\CleanExpiredTokensCommand::class,
  157. \App\Module\OpenAPI\Commands\GenerateStatsCommand::class,
  158. \App\Module\OpenAPI\Commands\CleanDataCommand::class,
  159. ]);
  160. }
  161. }
  162. /**
  163. * 获取提供的服务
  164. *
  165. * @return array
  166. */
  167. public function provides()
  168. {
  169. return [
  170. 'openapi.service',
  171. 'openapi.auth',
  172. 'openapi.log',
  173. 'openapi.rate_limit',
  174. 'openapi.scope',
  175. 'openapi.webhook',
  176. ];
  177. }
  178. }