OpenAPIServiceProvider.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. }
  34. /**
  35. * 启动服务
  36. *
  37. * @return void
  38. */
  39. public function boot()
  40. {
  41. // 注册路由
  42. $this->registerRoutes();
  43. // 注册中间件
  44. $this->registerMiddleware();
  45. // 注册事件监听器
  46. $this->registerEventListeners();
  47. // 注册命令
  48. $this->registerCommands();
  49. }
  50. /**
  51. * 注册服务
  52. *
  53. * @return void
  54. */
  55. protected function registerServices()
  56. {
  57. // 注册OpenAPI服务
  58. $this->app->singleton('openapi.service', function () {
  59. return new \App\Module\OpenAPI\Services\OpenApiService();
  60. });
  61. // 注册认证服务
  62. $this->app->singleton('openapi.auth', function ($app) {
  63. return new \App\Module\OpenAPI\Services\AuthService(
  64. $app->make('openapi.service')
  65. );
  66. });
  67. // 注册日志服务
  68. $this->app->singleton('openapi.log', function () {
  69. return new \App\Module\OpenAPI\Services\LogService();
  70. });
  71. // 注册频率限制服务
  72. $this->app->singleton('openapi.rate_limit', function () {
  73. return new \App\Module\OpenAPI\Services\RateLimitService();
  74. });
  75. // 注册权限范围服务
  76. $this->app->singleton('openapi.scope', function () {
  77. return new \App\Module\OpenAPI\Services\ScopeService();
  78. });
  79. // 注册Webhook服务
  80. $this->app->singleton('openapi.webhook', function () {
  81. return new \App\Module\OpenAPI\Services\WebhookService();
  82. });
  83. }
  84. /**
  85. * 注册路由
  86. *
  87. * @return void
  88. */
  89. protected function registerRoutes()
  90. {
  91. // 注册后台管理路由
  92. if (file_exists($adminRoutes = __DIR__ . '/../Routes/admin.php')) {
  93. Route::middleware(['web', 'admin'])
  94. ->prefix(config('admin.route.prefix', 'admin'))
  95. ->group($adminRoutes);
  96. }
  97. // 注册API路由
  98. if (file_exists($apiRoutes = __DIR__ . '/../Routes/api.php')) {
  99. Route::middleware('api')
  100. ->prefix('api')
  101. ->group($apiRoutes);
  102. }
  103. }
  104. /**
  105. * 注册中间件
  106. *
  107. * @return void
  108. */
  109. protected function registerMiddleware()
  110. {
  111. $router = $this->app['router'];
  112. // 注册中间件别名
  113. $router->aliasMiddleware('openapi.auth', ApiAuthMiddleware::class);
  114. $router->aliasMiddleware('openapi.rate_limit', RateLimitMiddleware::class);
  115. $router->aliasMiddleware('openapi.scope', ScopeMiddleware::class);
  116. $router->aliasMiddleware('openapi.ip_whitelist', IpWhitelistMiddleware::class);
  117. // 注册中间件组
  118. $router->middlewareGroup('openapi', [
  119. 'openapi.auth',
  120. 'openapi.rate_limit',
  121. 'openapi.scope',
  122. 'openapi.ip_whitelist',
  123. ]);
  124. }
  125. /**
  126. * 注册事件监听器
  127. *
  128. * @return void
  129. */
  130. protected function registerEventListeners()
  131. {
  132. Event::listen(
  133. AppCreatedEvent::class,
  134. AppCreatedListener::class
  135. );
  136. }
  137. /**
  138. * 注册命令
  139. *
  140. * @return void
  141. */
  142. protected function registerCommands()
  143. {
  144. if ($this->app->runningInConsole()) {
  145. $this->commands([
  146. \App\Module\OpenAPI\Commands\GenerateApiKeyCommand::class,
  147. \App\Module\OpenAPI\Commands\CleanExpiredTokensCommand::class,
  148. \App\Module\OpenAPI\Commands\GenerateStatsCommand::class,
  149. \App\Module\OpenAPI\Commands\CleanDataCommand::class,
  150. ]);
  151. }
  152. }
  153. /**
  154. * 获取提供的服务
  155. *
  156. * @return array
  157. */
  158. public function provides()
  159. {
  160. return [
  161. 'openapi.service',
  162. 'openapi.auth',
  163. 'openapi.log',
  164. 'openapi.rate_limit',
  165. 'openapi.scope',
  166. 'openapi.webhook',
  167. ];
  168. }
  169. }