|
25 | 25 |
|
26 | 26 | $specHandler = static function (ServerRequestInterface $serverRequest) use ($specPath): ResponseInterface { |
27 | 27 | try { |
28 | | - $path = $specPath; |
| 28 | + $path = (string) $specPath; |
29 | 29 |
|
30 | | - if (str_starts_with((string) $path, 'http')) { |
| 30 | + if (str_starts_with($path, 'http')) { |
31 | 31 | $content = file_get_contents($path, false, RemoteSpecificationLoader::createStreamContext()); |
32 | 32 | } else { |
33 | | - $content = file_exists((string) $path) ? file_get_contents((string) $path) : null; |
| 33 | + $content = file_exists($path) ? file_get_contents($path) : null; |
34 | 34 | } |
35 | 35 |
|
36 | 36 | if ($content === false || $content === null) { |
37 | 37 | return new TextResponse('OpenAPI spec not found at ' . $path, 404); |
38 | 38 | } |
39 | 39 |
|
40 | | - $contentType = str_ends_with((string) $path, '.json') ? 'application/json' : 'text/yaml'; |
41 | | - if (str_ends_with($serverRequest->getUri()->getPath(), '.json')) { |
42 | | - $contentType = 'application/json'; |
43 | | - } |
| 40 | + $requestedPath = $serverRequest->getUri()->getPath(); |
| 41 | + $isJsonRequest = str_ends_with($requestedPath, '.json'); |
| 42 | + |
| 43 | + // Serve the correct Content-Type for the requested route. |
| 44 | + $contentType = $isJsonRequest ? 'application/json' : 'text/yaml'; |
44 | 45 |
|
45 | 46 | return new TextResponse($content, 200, [ |
46 | 47 | 'Content-Type' => $contentType, |
|
50 | 51 | } |
51 | 52 | }; |
52 | 53 |
|
53 | | - // Route to serve the raw OpenAPI specification |
54 | | - $application->get('/openapi.yaml', $specHandler); |
55 | | - $application->get('/openapi.json', $specHandler); |
| 54 | + // Route registrations |
| 55 | + $application->get('/openapi.yaml', $specHandler, 'openapi_yaml'); |
| 56 | + $application->get('/openapi.json', $specHandler, 'openapi_json'); |
56 | 57 |
|
57 | 58 | // Swagger UI at root |
58 | 59 | $application->get('/', static function (ServerRequestInterface $serverRequest) use ($specPath): ResponseInterface { |
|
65 | 66 | ]); |
66 | 67 | } |
67 | 68 |
|
| 69 | + // Determine the correct spec URL for Swagger UI |
| 70 | + $path = (string) $specPath; |
| 71 | + $parsedPath = parse_url($path, PHP_URL_PATH) ?: $path; |
| 72 | + $extension = strtolower(pathinfo($parsedPath, PATHINFO_EXTENSION)); |
| 73 | + $specUrl = $extension === 'json' ? '/openapi.json' : '/openapi.yaml'; |
| 74 | + |
68 | 75 | $html = <<<HTML |
69 | 76 | <!DOCTYPE html> |
70 | 77 | <html lang="en"> |
|
87 | 94 | <script> |
88 | 95 | window.onload = () => { |
89 | 96 | window.ui = SwaggerUIBundle({ |
90 | | - url: '/openapi.yaml', |
| 97 | + url: '$specUrl', |
91 | 98 | dom_id: '#swagger-ui', |
92 | 99 | deepLinking: true, |
93 | 100 | presets: [ |
94 | 101 | SwaggerUIBundle.presets.apis, |
95 | | - SwaggerStandalonePreset |
| 102 | + SwaggerUIStandalonePreset |
96 | 103 | ], |
97 | 104 | plugins: [ |
98 | 105 | SwaggerUIBundle.plugins.DownloadUrl |
|
105 | 112 | </html> |
106 | 113 | HTML; |
107 | 114 | return new HtmlResponse($html); |
108 | | - }); |
| 115 | + }, 'home'); |
109 | 116 | }; |
0 commit comments