forked from yzPeedro/SugoiAPI
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSwaggerController.php
More file actions
97 lines (88 loc) · 2.75 KB
/
SwaggerController.php
File metadata and controls
97 lines (88 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
namespace App\Controller;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
class SwaggerController
{
/**
* Redirect root to documentation.
*
* @return RedirectResponse
*/
#[Route('/', name: 'root', methods: ['GET'])]
public function root(): RedirectResponse
{
return new RedirectResponse('/api/doc', Response::HTTP_MOVED_PERMANENTLY);
}
/**
* Display Swagger UI documentation.
*
* @return Response
*/
#[Route('/api/doc', name: 'swagger_ui', methods: ['GET'])]
public function swagger(): Response
{
$html = <<<'HTML'
<!DOCTYPE html>
<html>
<head>
<title>Sugoi API - Swagger UI</title>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://unpkg.com/swagger-ui-dist@4/swagger-ui.css">
<style>
html { box-sizing: border-box; overflow: -moz-scrollbars-vertical; overflow-y: scroll; }
*, *:before, *:after { box-sizing: inherit; }
body { margin: 0; background: #fafafa; }
</style>
</head>
<body>
<div id="swagger-ui"></div>
<script src="https://unpkg.com/swagger-ui-dist@4/swagger-ui-bundle.js"></script>
<script src="https://unpkg.com/swagger-ui-dist@4/swagger-ui-standalone-preset.js"></script>
<script>
window.onload = function() {
const ui = SwaggerUIBundle({
url: "/openapi.json",
dom_id: '#swagger-ui',
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
],
layout: "StandaloneLayout",
deepLinking: true
});
window.ui = ui;
};
</script>
</body>
</html>
HTML;
return new Response($html, Response::HTTP_OK, ['Content-Type' => 'text/html']);
}
/**
* Serve OpenAPI JSON schema.
*
* @return Response
*/
#[Route('/api/doc.json', name: 'openapi_json', methods: ['GET'])]
#[Route('/openapi.json', name: 'openapi_json_public', methods: ['GET'])]
public function openapi(): Response
{
$filePath = dirname(__DIR__) . '/../public/openapi.json';
if (!file_exists($filePath)) {
return new Response(
json_encode(['error' => 'OpenAPI schema not found']),
Response::HTTP_NOT_FOUND,
['Content-Type' => 'application/json']
);
}
$content = file_get_contents($filePath);
return new Response(
$content,
Response::HTTP_OK,
['Content-Type' => 'application/json']
);
}
}