|
1 | 1 | <?php |
2 | 2 | declare(strict_types=1); |
3 | 3 |
|
| 4 | +use Laminas\Diactoros\Response\HtmlResponse; |
| 5 | +use Laminas\Diactoros\Response\JsonResponse; |
| 6 | +use Laminas\Diactoros\Response\TextResponse; |
4 | 7 | use Mezzio\Application; |
5 | 8 | use Mezzio\MiddlewareFactory; |
6 | 9 | use Psr\Http\Message\ResponseInterface; |
7 | 10 | use Psr\Http\Message\ServerRequestInterface; |
| 11 | +use WebProject\PhpOpenApiMockServer\Middleware\MockMiddleware\Utils\RemoteSpecificationLoader; |
8 | 12 |
|
9 | | -return static function (Application $app, MiddlewareFactory $factory): void { |
10 | | - // Fallback for routes not in the spec but defined in Mezzio |
11 | | - $app->get('/', static function (ServerRequestInterface $request): ResponseInterface { |
12 | | - return new Laminas\Diactoros\Response\JsonResponse([ |
13 | | - 'message' => 'OpenAPI Mock Server is running!', |
14 | | - 'instructions' => 'Point your requests to endpoints defined in your OpenAPI spec.', |
15 | | - 'spec_path' => getenv('OPENAPI_SPEC') ?: 'data/openapi.yaml', |
16 | | - ]); |
| 13 | +return static function (Application $application, MiddlewareFactory $middlewareFactory): void { |
| 14 | + $specPath = getenv('OPENAPI_SPEC') ?: 'data/openapi.yaml'; |
| 15 | + $projectRoot = realpath(__DIR__ . '/..') ?: '/app'; |
| 16 | + |
| 17 | + $specHandler = static function (ServerRequestInterface $serverRequest) use ($specPath, $projectRoot): ResponseInterface { |
| 18 | + try { |
| 19 | + $path = $specPath; |
| 20 | + if (!str_starts_with((string) $path, '/') && !str_starts_with((string) $path, 'http')) { |
| 21 | + $path = $projectRoot . DIRECTORY_SEPARATOR . $path; |
| 22 | + } |
| 23 | + |
| 24 | + if (str_starts_with((string) $path, 'http')) { |
| 25 | + $content = file_get_contents($path, false, RemoteSpecificationLoader::createStreamContext()); |
| 26 | + } else { |
| 27 | + $content = file_exists((string) $path) ? file_get_contents((string) $path) : null; |
| 28 | + } |
| 29 | + |
| 30 | + if ($content === false || $content === null) { |
| 31 | + return new TextResponse('OpenAPI spec not found at ' . $path, 404); |
| 32 | + } |
| 33 | + |
| 34 | + $contentType = str_ends_with((string) $path, '.json') ? 'application/json' : 'text/yaml'; |
| 35 | + if (str_ends_with($serverRequest->getUri()->getPath(), '.json')) { |
| 36 | + $contentType = 'application/json'; |
| 37 | + } |
| 38 | + |
| 39 | + return new TextResponse($content, 200, [ |
| 40 | + 'Content-Type' => $contentType, |
| 41 | + ]); |
| 42 | + } catch (Throwable $e) { |
| 43 | + return new TextResponse('Error loading spec: ' . $e->getMessage(), 500); |
| 44 | + } |
| 45 | + }; |
| 46 | + |
| 47 | + // Route to serve the raw OpenAPI specification |
| 48 | + $application->get('/openapi.yaml', $specHandler); |
| 49 | + $application->get('/openapi.json', $specHandler); |
| 50 | + |
| 51 | + // Swagger UI at root |
| 52 | + $application->get('/', static function (ServerRequestInterface $serverRequest) use ($specPath): ResponseInterface { |
| 53 | + $accept = $serverRequest->getHeaderLine('Accept'); |
| 54 | + if (str_contains($accept, 'application/json')) { |
| 55 | + return new JsonResponse([ |
| 56 | + 'message' => 'OpenAPI Mock Server is running!', |
| 57 | + 'instructions' => 'Point your requests to endpoints defined in your OpenAPI spec.', |
| 58 | + 'spec_path' => $specPath, |
| 59 | + ]); |
| 60 | + } |
| 61 | + |
| 62 | + $html = <<<HTML |
| 63 | +<!DOCTYPE html> |
| 64 | +<html lang="en"> |
| 65 | +<head> |
| 66 | + <meta charset="utf-8" /> |
| 67 | + <meta name="viewport" content="width=device-width, initial-scale=1" /> |
| 68 | + <meta name="description" content="SwaggerUI" /> |
| 69 | + <title>OpenAPI Mock Server - Swagger UI</title> |
| 70 | + <link rel="stylesheet" href="https://unpkg.com/swagger-ui-dist@5/swagger-ui.css" /> |
| 71 | + <style> |
| 72 | + html { box-sizing: border-box; overflow: -moz-scrollbars-vertical; overflow-y: scroll; } |
| 73 | + *, *:before, *:after { box-sizing: inherit; } |
| 74 | + body { margin: 0; background: #fafafa; } |
| 75 | + </style> |
| 76 | +</head> |
| 77 | +<body> |
| 78 | +<div id="swagger-ui"></div> |
| 79 | +<script src="https://unpkg.com/swagger-ui-dist@5/swagger-ui-bundle.js" crossorigin></script> |
| 80 | +<script src="https://unpkg.com/swagger-ui-dist@5/swagger-ui-standalone-preset.js" crossorigin></script> |
| 81 | +<script> |
| 82 | + window.onload = () => { |
| 83 | + window.ui = SwaggerUIBundle({ |
| 84 | + url: '/openapi.yaml', |
| 85 | + dom_id: '#swagger-ui', |
| 86 | + deepLinking: true, |
| 87 | + presets: [ |
| 88 | + SwaggerUIBundle.presets.apis, |
| 89 | + SwaggerStandalonePreset |
| 90 | + ], |
| 91 | + plugins: [ |
| 92 | + SwaggerUIBundle.plugins.DownloadUrl |
| 93 | + ], |
| 94 | + layout: "BaseLayout", |
| 95 | + }); |
| 96 | + }; |
| 97 | +</script> |
| 98 | +</body> |
| 99 | +</html> |
| 100 | +HTML; |
| 101 | + return new HtmlResponse($html); |
17 | 102 | }); |
18 | 103 | }; |
0 commit comments