Repository navigation
Expand file tree
/
Copy pathOpenApiController.php
More file actions
361 lines (314 loc) · 12.9 KB
/
Copy pathOpenApiController.php
File metadata and controls
361 lines (314 loc) · 12.9 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
<?php
namespace app\adminapi\controller;
use app\common\config\ConfigRepository;
use app\common\http\Response;
use app\common\openapi\ApiDocsAccess;
use app\common\openapi\ApiField;
use app\common\response\ApiResponseFactory;
use app\common\router\GetMapping;
use app\common\router\RestController;
/**
* OpenAPI 文档接口。
*
* 从注解路由(RouteScanner 扫描结果)自动生成 OpenAPI 3.0 文档,路由即文档,
* 新增接口无需手动维护;输出原始 JSON 供 Swagger UI 调试台加载。
* Author: qiufeng
*/
#[RestController('/adminapi', tag: '基础')]
final class OpenApiController extends BaseController
{
/** 已生成的 VO schema,输出到 components/schemas */
private array $voSchemas = [];
public function __construct(
ApiResponseFactory $responseFactory,
ConfigRepository $config,
private readonly ApiDocsAccess $docsAccess,
private readonly array $routes = []
) {
parent::__construct($responseFactory, $config);
}
#[GetMapping('/openapi.json', summary: 'OpenAPI 文档', auth: false, raw: true, response: 'object OpenAPI 3.0 文档原始 JSON,不包统一响应结构')]
public function json(): Response
{
// 仅调试模式开放,可选 Basic Auth 账号密码保护
$denied = $this->docsAccess->deny($this->request);
if ($denied !== null) {
return $denied;
}
$paths = [];
foreach ($this->routes as [$method, $path, $handler, $meta]) {
$operation = [
'summary' => $meta['summary'] ?? $handler,
'tags' => [$meta['tag'] ?? '默认'],
'responses' => [
'200' => [
'description' => 'OK',
'content' => [
'application/json' => [
// response_raw 的接口(如本文档接口)不包统一响应结构
'schema' => ($meta['response_raw'] ?? false)
? $this->dataSchema($meta['response'] ?? null)
: $this->envelopeSchema($this->dataSchema($meta['response'] ?? null)),
],
],
],
],
];
if (($meta['auth'] ?? true) !== false) {
$operation['security'] = [['bearerAuth' => []]];
$operation['responses']['401'] = ['description' => '未登录或 token 失效'];
}
if (($meta['permission'] ?? '') !== '') {
$operation['description'] = '权限标识:`' . $meta['permission'] . '`';
$operation['responses']['403'] = ['description' => '无权限'];
}
if (preg_match_all('/\{(\w+)\}/', $path, $matches)) {
foreach ($matches[1] as $param) {
$operation['parameters'][] = [
'name' => $param,
'in' => 'path',
'required' => true,
'schema' => ['type' => 'string'],
];
}
}
// 注解 query 声明 → query 参数(让 GET 接口的查询参数能在 Swagger UI 填写)
foreach ($meta['query'] ?? [] as $field => $def) {
$operation['parameters'][] = $this->queryParameter((string) $field, $def);
}
// 通用响应格式参数
$operation['parameters'][] = [
'name' => 'format',
'in' => 'query',
'required' => false,
'description' => '响应格式,默认走系统配置',
'schema' => ['type' => 'string', 'enum' => ['json', 'xml']],
];
if (in_array($method, ['POST', 'PUT', 'PATCH'], true)) {
$bodyDef = $meta['body'] ?? [];
$operation['requestBody'] = [
'required' => $bodyDef !== [],
'content' => [
'application/json' => [
'schema' => $this->requestBodySchema($bodyDef),
],
],
];
}
$paths[$path][strtolower($method)] = $operation;
}
$document = [
'openapi' => '3.0.3',
'info' => [
'title' => 'TLAdmin API',
'version' => '1.0.0',
'description' => "TLAdmin 管理端 API 文档,由路由表自动生成。\n\n统一响应结构:`{code, message, data, request_id, timestamp}`;支持 `format=json|xml` 切换响应格式。",
],
'servers' => [['url' => '/']],
'paths' => $paths,
'components' => [
'schemas' => $this->voSchemas,
'securitySchemes' => [
'bearerAuth' => [
'type' => 'http',
'scheme' => 'bearer',
'description' => '登录接口返回的 access_token',
],
],
],
];
return new Response(
json_encode($document, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT) ?: '{}',
200,
[
'Content-Type' => 'application/json; charset=utf-8',
'Access-Control-Allow-Origin' => '*',
]
);
}
/**
* 由注解 query 单条声明生成一个 OpenAPI query 参数。
* 声明格式与 body 字段一致:详细数组(type/description/required/example)或简单字符串(描述,默认 string)。
*
* @param string $name 参数名
* @param array<string,mixed>|string $def 字段声明
* @return array<string,mixed>
*/
private function queryParameter(string $name, array|string $def): array
{
$param = [
'name' => $name,
'in' => 'query',
'required' => false,
];
if (is_array($def)) {
$schema = ['type' => (string) ($def['type'] ?? 'string')];
if (array_key_exists('example', $def)) {
$schema['example'] = $def['example'];
}
$param['schema'] = $schema;
if (isset($def['description'])) {
$param['description'] = (string) $def['description'];
}
$param['required'] = ($def['required'] ?? false) === true;
} else {
$param['schema'] = ['type' => 'string'];
$param['description'] = $def;
}
return $param;
}
/**
* 由注解 body 声明生成请求体 schema(让 Swagger UI 显示具体参数)。
* 未声明时回退为任意对象。
*/
private function requestBodySchema(array $body): array
{
if ($body === []) {
return ['type' => 'object', 'additionalProperties' => true];
}
$properties = [];
$required = [];
foreach ($body as $field => $def) {
$name = (string) $field;
if (is_array($def)) {
$schema = ['type' => (string) ($def['type'] ?? 'string')];
if (isset($def['description'])) {
$schema['description'] = (string) $def['description'];
}
if (array_key_exists('example', $def)) {
$schema['example'] = $def['example'];
}
$properties[$name] = $schema;
if (($def['required'] ?? false) === true) {
$required[] = $name;
}
} else {
$properties[$name] = ['type' => 'string', 'description' => (string) $def];
}
}
$schema = ['type' => 'object', 'properties' => $properties];
if ($required !== []) {
$schema['required'] = $required;
}
return $schema;
}
/** 统一响应结构 {code, message, data, request_id, timestamp} 包装 data schema */
private function envelopeSchema(array $dataSchema): array
{
return [
'type' => 'object',
'properties' => [
'code' => ['type' => 'integer', 'description' => '业务码,0 表示成功'],
'message' => ['type' => 'string', 'description' => '提示信息'],
'data' => $dataSchema,
'request_id' => ['type' => 'string', 'description' => '请求追踪 ID'],
'timestamp' => ['type' => 'integer', 'description' => '服务端时间戳(秒)'],
],
];
}
/**
* 路由 meta 的 response 声明转 OpenAPI schema。
*
* 声明格式:
* 'field' => SomeVo::class VO 类,反射属性类型和 ApiField 注解生成
* 'field' => 'int 描述' 标量,类型和描述用空格分隔
* 'field' => [...] 关联数组 = 嵌套对象
* 'field' => [[...]] 单元素列表 = 对象数组
* 'field' => ['string 描述'] 单元素列表 = 标量数组
*/
private function dataSchema(mixed $definition): array
{
if ($definition === null) {
return ['description' => '业务数据,字段见接口说明'];
}
if (is_string($definition) && class_exists($definition)) {
return $this->voRef($definition);
}
if (is_string($definition)) {
$parts = explode(' ', $definition, 2);
$schema = $this->scalarType($parts[0]);
if (isset($parts[1]) && $parts[1] !== '') {
$schema['description'] = $parts[1];
}
return $schema;
}
if (is_array($definition) && array_is_list($definition) && count($definition) === 1) {
return ['type' => 'array', 'items' => $this->dataSchema($definition[0])];
}
if (is_array($definition)) {
$properties = [];
foreach ($definition as $field => $fieldDefinition) {
$properties[(string) $field] = $this->dataSchema($fieldDefinition);
}
return ['type' => 'object', 'properties' => $properties];
}
return ['description' => '业务数据'];
}
/** VO 类注册到 components/schemas 并返回 $ref;先占位再生成,支持递归 VO(如菜单树) */
private function voRef(string $class): array
{
$name = substr($class, (int) strrpos($class, '\\') + 1);
if (!isset($this->voSchemas[$name])) {
$this->voSchemas[$name] = true;
$this->voSchemas[$name] = $this->voSchema($class);
}
return ['$ref' => "#/components/schemas/{$name}"];
}
/** 反射 VO 公开属性(含继承)生成 object schema */
private function voSchema(string $class): array
{
$properties = [];
foreach ((new \ReflectionClass($class))->getProperties(\ReflectionProperty::IS_PUBLIC) as $property) {
if ($property->isStatic()) {
continue;
}
$schema = $this->voPropertySchema($property);
$attributes = $property->getAttributes(ApiField::class);
if ($attributes !== []) {
/** @var ApiField $field */
$field = $attributes[0]->newInstance();
if ($field->description !== '') {
$schema['description'] = $field->description;
}
if ($field->example !== null) {
$schema['example'] = $field->example;
}
if ($field->listOf !== null) {
$schema['type'] = 'array';
$schema['items'] = class_exists($field->listOf)
? $this->voRef($field->listOf)
: $this->scalarType($field->listOf);
}
}
$properties[$property->getName()] = $schema;
}
return ['type' => 'object', 'properties' => $properties];
}
private function voPropertySchema(\ReflectionProperty $property): array
{
$type = $property->getType();
if (!$type instanceof \ReflectionNamedType) {
return [];
}
$schema = $type->isBuiltin()
? $this->scalarType($type->getName())
: $this->voRef($type->getName());
if ($type->allowsNull() && !isset($schema['$ref'])) {
$schema['nullable'] = true;
}
return $schema;
}
private function scalarType(string $type): array
{
return match ($type) {
'int', 'integer' => ['type' => 'integer'],
'float', 'number' => ['type' => 'number'],
'bool', 'boolean' => ['type' => 'boolean'],
'string' => ['type' => 'string'],
'object' => ['type' => 'object', 'additionalProperties' => true],
'array' => ['type' => 'array', 'items' => []],
'mixed' => [],
default => ['description' => $type],
};
}
}