| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 |
- <?php
- namespace App\Module\ThirdParty\Providers;
- use Illuminate\Support\ServiceProvider;
- use Illuminate\Support\Facades\Route;
- /**
- * ThirdParty模块服务提供者
- *
- * 专注于第三方服务管理功能,提供统一的第三方服务接入和管理
- */
- class ThirdPartyServiceProvider extends ServiceProvider
- {
- /**
- * 注册服务
- *
- * @return void
- */
- public function register()
- {
- // 注册配置文件
- $this->mergeConfigFrom(
- __DIR__ . '/../Config/thirdparty.php',
- 'thirdparty'
- );
- // 注册服务
- $this->registerServices();
- }
- /**
- * 启动服务
- *
- * @return void
- */
- public function boot()
- {
- // 注册路由
- $this->registerRoutes();
- // 注册视图
- $this->registerViews();
- // 注册命令
- $this->registerCommands();
- // 注册事件监听器
- $this->registerEventListeners();
- // 发布资源
- $this->publishResources();
- }
- /**
- * 注册服务
- *
- * @return void
- */
- protected function registerServices()
- {
- // 注册第三方服务核心服务
- $this->app->singleton('thirdparty.service', function () {
- return new \App\Module\ThirdParty\Services\ThirdPartyService();
- });
- // 注册凭证管理服务
- $this->app->singleton('thirdparty.credential', function () {
- return new \App\Module\ThirdParty\Services\CredentialService();
- });
- // 注册监控服务
- $this->app->singleton('thirdparty.monitor', function () {
- return new \App\Module\ThirdParty\Services\MonitorService();
- });
- // 注册日志服务
- $this->app->singleton('thirdparty.log', function () {
- return new \App\Module\ThirdParty\Services\LogService();
- });
- // 注册配额管理服务
- $this->app->singleton('thirdparty.quota', function () {
- return new \App\Module\ThirdParty\Services\QuotaService();
- });
- // 注册Webhook分发服务
- $this->app->singleton('thirdparty.webhook', function () {
- return new \App\Module\ThirdParty\Services\WebhookDispatchService();
- });
- // 注册验证器
- $this->app->singleton(\App\Module\ThirdParty\Validators\ServiceValidator::class);
- $this->app->singleton(\App\Module\ThirdParty\Validators\CredentialValidator::class);
- }
- /**
- * 注册路由
- *
- * @return void
- */
- protected function registerRoutes()
- {
- // 注册后台管理路由
- if (file_exists($adminRoutes = __DIR__ . '/../Routes/admin.php')) {
- Route::middleware(['web', 'admin'])
- ->prefix(config('admin.route.prefix', 'admin') . '/thirdparty')
- ->group($adminRoutes);
- }
- // 注册Webhook路由
- if (file_exists($webhookRoutes = __DIR__ . '/../Routes/webhook.php')) {
- Route::middleware(['api'])
- ->prefix('thirdParty/webhook')
- ->name('thirdparty.webhook.')
- ->group($webhookRoutes);
- }
- // 注册API路由(如果需要)
- if (file_exists($apiRoutes = __DIR__ . '/../Routes/api.php')) {
- Route::middleware(['api'])
- ->prefix('api/thirdparty')
- ->group($apiRoutes);
- }
- }
- /**
- * 注册视图
- *
- * @return void
- */
- protected function registerViews()
- {
- // 注册视图命名空间
- $this->loadViewsFrom(__DIR__ . '/../Views', 'thirdparty');
- }
- /**
- * 注册命令
- *
- * @return void
- */
- protected function registerCommands()
- {
- if ($this->app->runningInConsole()) {
- $this->commands([
- \App\Module\ThirdParty\Commands\InsertThirdPartyAdminMenu::class,
- \App\Module\ThirdParty\Commands\RestructureExternalManagementMenu::class,
- \App\Module\ThirdParty\Commands\HealthCheckCommand::class,
- \App\Module\ThirdParty\Commands\QuotaResetCommand::class,
- \App\Module\ThirdParty\Commands\CleanupLogsCommand::class,
- \App\Module\ThirdParty\Commands\SyncServicesCommand::class,
- \App\Module\ThirdParty\Commands\TestServiceCommand::class,
- \App\Module\ThirdParty\Commands\TestBaseArchitectureCommand::class,
- \App\Module\ThirdParty\Commands\WebhookMappingCommand::class,
- \App\Module\ThirdParty\Commands\TestUrsRequestCommand::class,
- ]);
- }
- }
- /**
- * 注册事件监听器
- *
- * @return void
- */
- protected function registerEventListeners()
- {
- // 注册事件监听器
- $events = [
- // API调用事件监听器
- \App\Module\ThirdParty\Events\ApiCallEvent::class => [
- \App\Module\ThirdParty\Listeners\ApiCallLogListener::class,
- \App\Module\ThirdParty\Listeners\QuotaManagementListener::class,
- ],
- // 服务状态变更事件监听器
- \App\Module\ThirdParty\Events\ServiceStatusChangedEvent::class => [
- \App\Module\ThirdParty\Listeners\ServiceStatusMonitorListener::class,
- \App\Module\ThirdParty\Listeners\AlertNotificationListener::class . '@handleServiceStatusChanged',
- ],
- // 配额告警事件监听器
- \App\Module\ThirdParty\Events\QuotaAlertEvent::class => [
- \App\Module\ThirdParty\Listeners\AlertNotificationListener::class . '@handleQuotaAlert',
- ],
- // 监控告警事件监听器
- \App\Module\ThirdParty\Events\MonitorAlertEvent::class => [
- \App\Module\ThirdParty\Listeners\AlertNotificationListener::class . '@handleMonitorAlert',
- ],
- ];
- foreach ($events as $event => $listeners) {
- foreach ($listeners as $listener) {
- \Illuminate\Support\Facades\Event::listen($event, $listener);
- }
- }
- }
- /**
- * 发布资源
- *
- * @return void
- */
- protected function publishResources()
- {
- if ($this->app->runningInConsole()) {
- // 发布配置文件
- $this->publishes([
- __DIR__ . '/../Config/thirdparty.php' => config_path('thirdparty.php'),
- ], 'thirdparty-config');
- // 发布数据库迁移文件
- $this->publishes([
- __DIR__ . '/../Databases/GenerateSql/thirdparty_tables.sql' => database_path('sql/thirdparty_tables.sql'),
- ], 'thirdparty-migrations');
- // 发布视图文件(如果有)
- if (is_dir(__DIR__ . '/../Resources/views')) {
- $this->publishes([
- __DIR__ . '/../Resources/views' => resource_path('views/vendor/thirdparty'),
- ], 'thirdparty-views');
- }
- // 发布前端资源(如果有)
- if (is_dir(__DIR__ . '/../Resources/assets')) {
- $this->publishes([
- __DIR__ . '/../Resources/assets' => public_path('vendor/thirdparty'),
- ], 'thirdparty-assets');
- }
- }
- }
- /**
- * 获取提供的服务
- *
- * @return array
- */
- public function provides()
- {
- return [
- 'thirdparty.service',
- 'thirdparty.credential',
- 'thirdparty.monitor',
- 'thirdparty.log',
- 'thirdparty.quota',
- 'thirdparty.webhook',
- ];
- }
- }
|