Skip to content

Commit 361847e

Browse files
committed
Initial 3.0 Component
0 parents  commit 361847e

14 files changed

+363
-0
lines changed

.gitattributes

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/tests export-ignore
2+
/.github export-ignore
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
name: Close Pull Request
2+
3+
permissions: write-all
4+
5+
on:
6+
pull_request_target:
7+
types: [ opened ]
8+
9+
jobs:
10+
run:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: superbrothers/close-pull-request@v3
14+
with:
15+
comment: "Hi, this is a READ-ONLY repository, please submit your PR on the https://github.com/mineadmin/components repository.<br><br> This Pull Request will close automatically.<br><br> Thanks! "

.github/workflows/release.yml

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
on:
2+
push:
3+
tags:
4+
- 'v*' # Push events to matching v*, i.e. v1.0, v20.15.10
5+
6+
name: Release
7+
permissions: write-all
8+
9+
jobs:
10+
release:
11+
name: Release
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: Checkout code
15+
uses: actions/checkout@v4
16+
- name: Create Release
17+
id: create_release
18+
uses: actions/create-release@v1
19+
env:
20+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
21+
with:
22+
tag_name: ${{ github.ref }}
23+
release_name: Release ${{ github.ref }}
24+
draft: false
25+
prerelease: false

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
composer.lock
2+
.idea
3+
vendor
4+
*.cache
5+
runtime

Attributes/ApiOperation.php

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of MineAdmin.
6+
*
7+
* @link https://www.mineadmin.com
8+
* @document https://doc.mineadmin.com
9+
* @contact [email protected]
10+
* @license https://github.com/mineadmin/MineAdmin/blob/master/LICENSE
11+
*/
12+
13+
namespace Mine\Swagger\Attributes;
14+
15+
use OpenApi\Annotations\Operation;
16+
use OpenApi\Attributes\OperationTrait;
17+
18+
#[\Attribute(\Attribute::TARGET_METHOD)]
19+
class ApiOperation extends Operation
20+
{
21+
use OperationTrait;
22+
}

Attributes/FormRequest.php

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of MineAdmin.
6+
*
7+
* @link https://www.mineadmin.com
8+
* @document https://doc.mineadmin.com
9+
* @contact [email protected]
10+
* @license https://github.com/mineadmin/MineAdmin/blob/master/LICENSE
11+
*/
12+
13+
namespace Mine\Swagger\Attributes;
14+
15+
use Hyperf\Collection\Arr;
16+
use Hyperf\Swagger\Annotation\Property;
17+
use Hyperf\Swagger\Annotation\Schema;
18+
19+
#[\Attribute(\Attribute::TARGET_CLASS)]
20+
class FormRequest extends Schema
21+
{
22+
public function __construct(
23+
?string $schema = null,
24+
?string $title = null,
25+
?string $description = null,
26+
?array $required = null,
27+
?array $properties = null,
28+
array $only = []
29+
) {
30+
$properties = $this->parserProperties($properties, $schema);
31+
if ($only) {
32+
$properties = Arr::where($properties, static function (Property $item) use ($only) {
33+
return \in_array($item->property, $only, true);
34+
});
35+
}
36+
parent::__construct(
37+
title: $title,
38+
description: $description,
39+
required: $required,
40+
properties: $properties
41+
);
42+
}
43+
44+
protected function parserSchemaProperties(\ReflectionClass $reflectionClass, string $schema): array
45+
{
46+
$result = [];
47+
foreach ($reflectionClass->getProperties() as $reflectionProperty) {
48+
$propertyList = $reflectionProperty->getAttributes(Property::class);
49+
if ($propertyList) {
50+
$result[] = $propertyList[0]->newInstance();
51+
}
52+
}
53+
return $result;
54+
}
55+
56+
private function parserProperties(?array $properties, string $schema): array
57+
{
58+
$result = [];
59+
if (empty($properties)) {
60+
$reflectionClass = new \ReflectionClass($schema);
61+
return $this->parserSchemaProperties($reflectionClass, $schema);
62+
}
63+
return $result;
64+
}
65+
}

Attributes/ListResponse.php

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of MineAdmin.
6+
*
7+
* @link https://www.mineadmin.com
8+
* @document https://doc.mineadmin.com
9+
* @contact [email protected]
10+
* @license https://github.com/mineadmin/MineAdmin/blob/master/LICENSE
11+
*/
12+
13+
namespace Mine\Swagger\Attributes;
14+
15+
use Hyperf\Swagger\Annotation\Items;
16+
use Hyperf\Swagger\Annotation\Property;
17+
18+
#[\Attribute(\Attribute::IS_REPEATABLE | \Attribute::TARGET_METHOD)]
19+
class ListResponse extends ResultResponse
20+
{
21+
protected function parserInstance(object|string $instance): array
22+
{
23+
$result[] = new Property(property: 'list', type: 'array', items: new Items(ref: $instance, description: '数据列表'));
24+
return $result;
25+
}
26+
}

Attributes/PageResponse.php

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of MineAdmin.
6+
*
7+
* @link https://www.mineadmin.com
8+
* @document https://doc.mineadmin.com
9+
* @contact [email protected]
10+
* @license https://github.com/mineadmin/MineAdmin/blob/master/LICENSE
11+
*/
12+
13+
namespace Mine\Swagger\Attributes;
14+
15+
use Hyperf\Swagger\Annotation\Items;
16+
use Hyperf\Swagger\Annotation\Property;
17+
18+
#[\Attribute(\Attribute::IS_REPEATABLE | \Attribute::TARGET_METHOD)]
19+
class PageResponse extends ResultResponse
20+
{
21+
protected function parserInstance(object|string $instance): array
22+
{
23+
$result = [
24+
new Property(property: 'total', description: '总数量', type: 'int'),
25+
];
26+
$result[] = new Property(property: 'list', type: 'array', items: new Items(ref: $instance, description: '数据列表'));
27+
return $result;
28+
}
29+
}

Attributes/ResultResponse.php

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of MineAdmin.
6+
*
7+
* @link https://www.mineadmin.com
8+
* @document https://doc.mineadmin.com
9+
* @contact [email protected]
10+
* @license https://github.com/mineadmin/MineAdmin/blob/master/LICENSE
11+
*/
12+
13+
namespace Mine\Swagger\Attributes;
14+
15+
use Hyperf\Swagger\Annotation\JsonContent;
16+
use Hyperf\Swagger\Annotation\Property;
17+
use Hyperf\Swagger\Annotation\Response as Base;
18+
use OpenApi\Generator;
19+
20+
#[\Attribute(\Attribute::IS_REPEATABLE | \Attribute::TARGET_METHOD)]
21+
class ResultResponse extends Base
22+
{
23+
public function __construct(
24+
object|string $instance,
25+
?string $title = null,
26+
?array $examples = null,
27+
?string $description = null,
28+
mixed $example = Generator::UNDEFINED,
29+
?array $headers = null,
30+
?int $response = 200,
31+
) {
32+
parent::__construct(
33+
response: $response,
34+
description: $description,
35+
headers: $headers,
36+
content: $this->generatorResponse($instance, $title, $example, $examples),
37+
);
38+
}
39+
40+
protected function parserInstance(object|string $instance): array
41+
{
42+
$result = [];
43+
$reflectionClass = new \ReflectionClass($instance);
44+
foreach ($reflectionClass->getProperties() as $property) {
45+
$result[] = $this->parserProperty($property, \is_string($instance) ? $instance : $instance->{$property->getName()});
46+
}
47+
return $result;
48+
}
49+
50+
protected function parserProperty(\ReflectionProperty $reflectionProperty, mixed $value): Property
51+
{
52+
$property = new Property();
53+
$property->property = $reflectionProperty->getName();
54+
/** @phpstan-ignore-next-line */
55+
$typeName = $reflectionProperty->getType()?->getName();
56+
if ($property->ref === Generator::UNDEFINED && class_exists($typeName)) {
57+
$property->ref = $typeName;
58+
}
59+
if ($property->ref === Generator::UNDEFINED && \is_object($value)) {
60+
$property->ref = $value::class;
61+
}
62+
if ($property->ref === Generator::UNDEFINED && \is_string($value) && class_exists($value)) {
63+
$property->ref = $value;
64+
}
65+
return $property;
66+
}
67+
68+
private function generatorResponse(object|string $instance, ?string $title, mixed $example, ?array $examples = []): JsonContent
69+
{
70+
$properties = $this->parserInstance($instance);
71+
return new JsonContent(examples: $examples, title: $title, properties: $properties, example: $example);
72+
}
73+
}

ConfigProvider.php

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Mine\Swagger;
4+
5+
final class ConfigProvider
6+
{
7+
public function __invoke(): array
8+
{
9+
return [
10+
'annotations' => [
11+
'scan' => [
12+
'paths' => [
13+
__DIR__
14+
],
15+
],
16+
],
17+
];
18+
}
19+
}

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 MineAdmin
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

Processor/AbstractProcessor.php

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of MineAdmin.
6+
*
7+
* @link https://www.mineadmin.com
8+
* @document https://doc.mineadmin.com
9+
* @contact [email protected]
10+
* @license https://github.com/mineadmin/MineAdmin/blob/master/LICENSE
11+
*/
12+
13+
namespace Mine\Swagger\Processor;
14+
15+
use OpenApi\Analysis;
16+
use OpenApi\Processors\ProcessorInterface;
17+
18+
abstract class AbstractProcessor implements ProcessorInterface
19+
{
20+
public function __invoke(Analysis $analysis)
21+
{
22+
$this->handle($analysis);
23+
return $analysis;
24+
}
25+
26+
abstract public function handle(Analysis $analysis);
27+
}

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# MineAdmin
2+
3+
[Documentation](https://doc.mineadmin.com)

composer.json

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "mineadmin/swagger",
3+
"description": "Mineadmin Swagger component",
4+
"license": "MIT",
5+
"type": "library",
6+
"authors": [
7+
{
8+
"name": "xmo",
9+
"email": "[email protected]",
10+
"role": "Developer"
11+
},
12+
{
13+
"name": "zds",
14+
"role": "Developer"
15+
}
16+
],
17+
"require": {
18+
"hyperf/swagger": "~3.1",
19+
"hyperf/di": "~3.1"
20+
},
21+
"autoload": {
22+
"psr-4": {
23+
"Mine\\Swagger\\": "./"
24+
}
25+
},
26+
"extra": {
27+
"hyperf": {
28+
"config": "Mine\\Swagger\\ConfigProvider"
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)