-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenAPI.php
More file actions
104 lines (92 loc) · 3.13 KB
/
Copy pathOpenAPI.php
File metadata and controls
104 lines (92 loc) · 3.13 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
98
99
100
101
102
103
104
<?php
namespace App\Controller;
use App\Controller;
use App\Router;
use App\Request;
use App\Response;
class OpenAPI extends Controller {
protected $app;
public function __construct(Router $app) {
$this->app = $app;
}
public function generateDocument(Request $request, Response $response, array $args = []) {
$document = [
'swagger' => '2.0',
'info' => (object)[
"description" => "",
"version" => "1.0.0",
"title" => "",
"termsOfService" => "",
"contact" => (object)[
"email" => ""
],
"license" => (object)[
"name" => "Apache 2.0",
"url" => "http://www.apache.org/licenses/LICENSE-2.0.html"
]
],
'host' => parse_url($request->url, PHP_URL_HOST),
'basePath' => $request->path,
'schemes' => [ parse_url($request->url, PHP_URL_SCHEME) ],
'tags' => [],
'paths' => [],
'definitions' => []
];
$this->detectEndpoints($document);
return $response
->format('json')
->body(json_encode($document));
}
private function detectEndpoints(&$document) {
foreach ($this->app->getRoutes() as $url => $route) {
foreach ($route as $method => $action) {
list ($controller, $middlewares) = $action;
if (is_array($controller) && class_exists($controller[0])) {
if (!array_search($controller[0], array_column($document['tags'], 'name'))) {
$document['tags'][] = (object)[
'name' => $controller[0],
'description' => ''
];
}
$attachedModel = call_user_func([$controller[0], 'getAttachedModel']);
if (is_object($attachedModel) && !array_search($attachedModel, array_keys($document['definitions']))) {
$document['definitions'][$attachedModel] = [
'type' => 'object',
'properties' => (object)$attachedModel->attributes,
'xml' => (object)['name' => $attachedModel]
];
}
}
if (!isset($document['paths'][$url])) $document['paths'][$url] = [];
$document['paths'][$url][$method] = (object)[
"tags" => (is_array($controller) && class_exists($controller[0])) ? [ $controller[0] ] : [],
"summary" => "",
"description" => "",
"operationId" => (is_array($controller) && class_exists($controller[0])) ? $controller[1] : '',
"consumes" => [ "application/json", "application/xml" ],
"produces" => [ "application/json", "application/xml" ],
"parameters" => [
/*
(object)[
"in" => "body",
"name" => "body",
"description" => "Pet object that needs to be added to the store",
"required" => true,
"schema" => (object)[
'$ref' => "#/definitions/Pet"
]
]
*/
],
"responses" => (object)[
"405" => (object)[
"description" => "Invalid input"
]
],
"security" => []
];
}
}
return $document;
}
}