| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- <?php
- namespace App\Module\OpenAPI\Providers;
- use App\Module\OpenAPI\Events\AppCreatedEvent;
- use App\Module\OpenAPI\Listeners\AppCreatedListener;
- use App\Module\OpenAPI\Middleware\ApiAuthMiddleware;
- use App\Module\OpenAPI\Middleware\RateLimitMiddleware;
- use App\Module\OpenAPI\Middleware\ScopeMiddleware;
- use App\Module\OpenAPI\Middleware\IpWhitelistMiddleware;
- use Illuminate\Support\Facades\Event;
- use Illuminate\Support\Facades\Route;
- use Illuminate\Support\ServiceProvider;
- /**
- * OpenAPI模块服务提供者
- *
- * 专注于后台管理功能,使用注解路由,不注册API路由和资源
- */
- class OpenAPIServiceProvider extends ServiceProvider
- {
- /**
- * 注册服务
- *
- * @return void
- */
- public function register()
- {
- // 注册配置文件
- $this->mergeConfigFrom(
- __DIR__ . '/../Config/openapi.php',
- 'openapi'
- );
- // 注册服务
- $this->registerServices();
- // 注册Handler
- $this->registerHandlers();
- }
- /**
- * 启动服务
- *
- * @return void
- */
- public function boot()
- {
- // 注册路由
- $this->registerRoutes();
- // 注册中间件
- $this->registerMiddleware();
- // 注册事件监听器
- $this->registerEventListeners();
- // 注册命令
- $this->registerCommands();
- }
- /**
- * 注册服务
- *
- * @return void
- */
- protected function registerServices()
- {
- // 注册OpenAPI服务
- $this->app->singleton('openapi.service', function () {
- return new \App\Module\OpenAPI\Services\OpenApiService();
- });
- // 注册认证服务
- $this->app->singleton('openapi.auth', function ($app) {
- return new \App\Module\OpenAPI\Services\AuthService(
- $app->make('openapi.service')
- );
- });
- // 注册日志服务
- $this->app->singleton('openapi.log', function () {
- return new \App\Module\OpenAPI\Services\LogService();
- });
- // 注册频率限制服务
- $this->app->singleton('openapi.rate_limit', function () {
- return new \App\Module\OpenAPI\Services\RateLimitService();
- });
- // 注册权限范围服务
- $this->app->singleton('openapi.scope', function () {
- return new \App\Module\OpenAPI\Services\ScopeService();
- });
- // 注册Webhook服务
- $this->app->singleton('openapi.webhook', function () {
- return new \App\Module\OpenAPI\Services\WebhookService();
- });
- }
- /**
- * 注册Handler
- *
- * @return void
- */
- protected function registerHandlers()
- {
- $handlerService = new \App\Module\OpenAPI\Services\HandlerRegistrationService();
- $handlerService->registerHandlers();
- }
- /**
- * 注册路由
- *
- * @return void
- */
- protected function registerRoutes()
- {
- // 使用路由注册服务来注册路由
- $routeService = new \App\Module\OpenAPI\Services\RouteRegistrationService();
- $routeService->registerAllRoutes();
- // 注册后台管理路由(保留原有的后台路由文件)
- if (file_exists($adminRoutes = __DIR__ . '/../Routes/admin.php')) {
- Route::middleware(['web', 'admin'])
- ->prefix(config('admin.route.prefix', 'admin'))
- ->group($adminRoutes);
- }
- }
- /**
- * 注册中间件
- *
- * @return void
- */
- protected function registerMiddleware()
- {
- $router = $this->app['router'];
- // 注册中间件别名
- $router->aliasMiddleware('openapi.auth', ApiAuthMiddleware::class);
- $router->aliasMiddleware('openapi.rate_limit', RateLimitMiddleware::class);
- $router->aliasMiddleware('openapi.scope', ScopeMiddleware::class);
- $router->aliasMiddleware('openapi.ip_whitelist', IpWhitelistMiddleware::class);
- // 注册中间件组
- $router->middlewareGroup('openapi', [
- 'openapi.auth',
- 'openapi.rate_limit',
- 'openapi.scope',
- 'openapi.ip_whitelist',
- ]);
- }
- /**
- * 注册事件监听器
- *
- * @return void
- */
- protected function registerEventListeners()
- {
- Event::listen(
- AppCreatedEvent::class,
- AppCreatedListener::class
- );
- }
- /**
- * 注册命令
- *
- * @return void
- */
- protected function registerCommands()
- {
- if ($this->app->runningInConsole()) {
- $this->commands([
- \App\Module\OpenAPI\Commands\GenerateApiKeyCommand::class,
- \App\Module\OpenAPI\Commands\CleanExpiredTokensCommand::class,
- \App\Module\OpenAPI\Commands\GenerateStatsCommand::class,
- \App\Module\OpenAPI\Commands\CleanDataCommand::class,
- ]);
- }
- }
- /**
- * 获取提供的服务
- *
- * @return array
- */
- public function provides()
- {
- return [
- 'openapi.service',
- 'openapi.auth',
- 'openapi.log',
- 'openapi.rate_limit',
- 'openapi.scope',
- 'openapi.webhook',
- ];
- }
- }
|