Skip to content

Commit

Permalink
swagger add Select a definition
Browse files Browse the repository at this point in the history
  • Loading branch information
tw2066 committed Nov 21, 2024
1 parent 16bcc70 commit 13eb247
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
15 changes: 10 additions & 5 deletions src/Swagger/SwaggerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) : [];
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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 */
Expand All @@ -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();
}
}
3 changes: 3 additions & 0 deletions src/Swagger/SwaggerOpenApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class SwaggerOpenApi
{
public ?SplPriorityQueue $queueTags;

public array $serverNameAll = [];

protected ?OpenApi $openApi = null;

protected ?SplPriorityQueue $queuePaths;
Expand Down Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions src/Swagger/SwaggerUiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
2 changes: 1 addition & 1 deletion src/web/swagger.html
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down

0 comments on commit 13eb247

Please sign in to comment.