Skip to content

Commit c888e58

Browse files
author
liutao
committed
feat: swagger
1 parent a813010 commit c888e58

File tree

6 files changed

+86
-20
lines changed

6 files changed

+86
-20
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,11 @@ php bin/console start
4545
```shell
4646
composer cs ./app
4747
```
48+
49+
### 生成Swagger文档
50+
```shell
51+
发布配置
52+
php bin/console config:publish swagger
53+
生成文档
54+
php bin/console swagger:gen
55+
```

app/Controller/Index.php

Lines changed: 0 additions & 17 deletions
This file was deleted.

app/Controller/IndexController.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Controller;
6+
7+
use App\Dto\Request\IndexGetReq;
8+
use App\Dto\Response\IndexGetRes;
9+
use MicroPHP\Framework\Controller;
10+
use MicroPHP\Framework\Http\Response;
11+
use MicroPHP\Framework\Http\ServerRequest;
12+
use MicroPHP\Swagger\Schema\Get;
13+
use MicroPHP\Swagger\Schema\Post;
14+
use MicroPHP\Swagger\Schema\RequestBody;
15+
use MicroPHP\Swagger\Schema\SuccessJsonResponse;
16+
use OpenApi\Attributes\Info;
17+
use OpenApi\Attributes\OpenApi;
18+
use OpenApi\Attributes\Server;
19+
20+
#[OpenApi(
21+
info: new Info(version: '1.0', title: 'micro-php'),
22+
servers: [new Server(url: 'http://127.0.0.1:8080')]
23+
)]
24+
class IndexController extends Controller
25+
{
26+
public const TAG = 'Index';
27+
28+
#[Get(summary: '首页', tags: [self::TAG])]
29+
#[SuccessJsonResponse(ref: IndexGetRes::class)]
30+
public function index(ServerRequest $request): Response
31+
{
32+
return $this->json('Hello World');
33+
}
34+
35+
#[Post(summary: '获取', tags: [self::TAG])]
36+
#[RequestBody(ref: IndexGetReq::class)]
37+
#[SuccessJsonResponse(ref: IndexGetRes::class)]
38+
public function get(ServerRequest $request): Response
39+
{
40+
return $this->json(new IndexGetRes(['id' => $request->input('id'), 'name' => 'hello']));
41+
}
42+
}

app/Dto/Request/IndexGetReq.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace App\Dto\Request;
5+
6+
use MicroPHP\Data\Data;
7+
use MicroPHP\Swagger\Schema\Property;
8+
use MicroPHP\Swagger\Schema\Schema;
9+
10+
#[Schema(title: 'index请求参数')]
11+
class IndexGetReq extends Data
12+
{
13+
#[Property(title: 'ID', example: 1)]
14+
public int $id = 1;
15+
}

app/Dto/Response/IndexGetRes.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace App\Dto\Response;
5+
6+
use MicroPHP\Data\Data;
7+
use MicroPHP\Swagger\Schema\Property;
8+
use MicroPHP\Swagger\Schema\Schema;
9+
10+
#[Schema(title: 'index响应参数')]
11+
class IndexGetRes extends Data
12+
{
13+
#[Property(title: 'ID', example: 1)]
14+
public int $id;
15+
16+
#[Property(title: '名字', example: 'test')]
17+
public string $name;
18+
}

config/routes.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
declare(strict_types=1);
44

5-
use App\Controller\Index;
5+
use App\Controller\IndexController;
66
use App\Middleware\NothingMiddleware;
77
use MicroPHP\Framework\Http\Response;
88
use MicroPHP\Framework\Router\Router;
99

1010
$router = new Router();
1111

12-
$router->get('/', [Index::class, 'index'])->middleware(new NothingMiddleware());
13-
$router->post('/', [Index::class, 'index'])->middleware(new NothingMiddleware());
12+
$router->get('/', [IndexController::class, 'index'])->middleware(new NothingMiddleware());
13+
$router->post('/get', [IndexController::class, 'get'])->middleware(new NothingMiddleware());
1414

1515
$router->get('/favicon.ico', function () {
1616
return new Response(200, [], '');

0 commit comments

Comments
 (0)