diff --git a/src/Swagger/SwaggerController.php b/src/Swagger/SwaggerController.php index f8989a4..3c27819 100644 --- a/src/Swagger/SwaggerController.php +++ b/src/Swagger/SwaggerController.php @@ -27,7 +27,7 @@ class SwaggerController protected array $swaggerFileList; - public function __construct(protected SwaggerConfig $swaggerConfig, protected ResponseInterface $response) + public function __construct(protected SwaggerConfig $swaggerConfig, protected ResponseInterface $response,protected SwaggerOpenApi $swaggerOpenApi,) { $this->outputDir = $this->swaggerConfig->getOutputDir(); $this->uiFileList = is_dir($this->swaggerUiPath) ? scandir($this->swaggerUiPath) : []; @@ -36,7 +36,7 @@ public function __construct(protected SwaggerConfig $swaggerConfig, protected Re public function getFile(string $file): PsrResponseInterface { - if (! in_array($file, $this->uiFileList)) { + if (!in_array($file, $this->uiFileList)) { throw new ApiDocsException('File does not exist'); } $file = $this->swaggerUiPath . '/' . $file; @@ -46,7 +46,7 @@ public function getFile(string $file): PsrResponseInterface public function getJsonFile(string $httpName): PsrResponseInterface { $file = $httpName . '.json'; - if (! in_array($file, $this->swaggerFileList)) { + if (!in_array($file, $this->swaggerFileList)) { throw new ApiDocsException('File does not exist'); } $filePath = $this->outputDir . '/' . $file; @@ -56,7 +56,7 @@ public function getJsonFile(string $httpName): PsrResponseInterface public function getYamlFile(string $httpName): PsrResponseInterface { $file = $httpName . '.yaml'; - if (! in_array($file, $this->swaggerFileList)) { + if (!in_array($file, $this->swaggerFileList)) { throw new ApiDocsException('File does not exist'); } $filePath = $this->outputDir . '/' . $file; @@ -65,7 +65,7 @@ public function getYamlFile(string $httpName): PsrResponseInterface protected function fileResponse(string $filePath) { - if (! Phar::running() && Constant::ENGINE == 'Swoole') { // phar报错 + if (!$this->pharRunning() && Constant::ENGINE == 'Swoole') { // phar报错 $stream = new SwooleFileStream($filePath); } elseif (Constant::ENGINE == 'Swow') { /* @phpstan-ignore-next-line */ @@ -92,4 +92,9 @@ protected function getSwaggerFileUrl($serverName): string { return $this->swaggerConfig->getPrefixUrl() . '/' . $serverName . '.' . $this->swaggerConfig->getFormat(); } + + private function pharRunning(): bool + { + return class_exists('Phar') && Phar::running(); + } } diff --git a/src/Swagger/SwaggerOpenApi.php b/src/Swagger/SwaggerOpenApi.php index 758bc71..8f9bfa7 100644 --- a/src/Swagger/SwaggerOpenApi.php +++ b/src/Swagger/SwaggerOpenApi.php @@ -15,6 +15,8 @@ class SwaggerOpenApi { public ?SplPriorityQueue $queueTags; + public array $serverNameAll = []; + protected ?OpenApi $openApi = null; protected ?SplPriorityQueue $queuePaths; @@ -137,6 +139,7 @@ public function save(string $serverName): void } $outputFile = $outputDir . '/' . $serverName . '.' . $this->swaggerConfig->getFormat(); $this->openApi->saveAs($outputFile); + $this->serverNameAll[] = $serverName; } protected function setInfo(): void diff --git a/src/Swagger/SwaggerUiController.php b/src/Swagger/SwaggerUiController.php index 94bdd25..7df44f6 100644 --- a/src/Swagger/SwaggerUiController.php +++ b/src/Swagger/SwaggerUiController.php @@ -20,11 +20,17 @@ public function swagger(): PsrResponseInterface $contents = file_get_contents($filePath); $contents = str_replace('{{$prefixUrl}}', $this->swaggerConfig->getPrefixUrl(), $contents); $contents = str_replace('{{$path}}', $this->swaggerConfig->getPrefixSwaggerResources(), $contents); - $contents = str_replace('{{$url}}', $this->getSwaggerFileUrl(BootAppRouteListener::$httpServerName), $contents); + // $contents = str_replace('{{$url}}', $this->getSwaggerFileUrl(BootAppRouteListener::$httpServerName), $contents); + $serverNameAll = array_reverse($this->swaggerOpenApi->serverNameAll); + $urls = ''; + foreach ($serverNameAll as $serverName) { + $url = $this->getSwaggerFileUrl($serverName); + $urls .= "{url: '{$url}', name: '{$serverName} server'},"; + } + $contents = str_replace('"{{$urls}}"', $urls, $contents); return $this->response->withAddedHeader('content-type', 'text/html')->withBody(new SwooleStream($contents)); } - public function redoc(): PsrResponseInterface { $filePath = $this->docsWebPath . '/redoc.html'; diff --git a/src/web/swagger.html b/src/web/swagger.html index c8cc095..acc49e5 100644 --- a/src/web/swagger.html +++ b/src/web/swagger.html @@ -38,7 +38,7 @@ window.onload = function() { // Begin Swagger UI call region const ui = SwaggerUIBundle({ - url: "{{$url}}", + urls: ["{{$urls}}"], dom_id: '#swagger-ui', deepLinking: true, presets: [