Skip to content

Commit 1ba4749

Browse files
committed
update5.2.0
1 parent b27b9d5 commit 1ba4749

15 files changed

+437
-45
lines changed

src/annotation/NotDebug.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace hg\apidoc\annotation;
4+
5+
use Attribute;
6+
use hg\apidoc\utils\AbstractAnnotation;
7+
8+
/**
9+
* 关闭接口调试
10+
* @package hg\apidoc\annotation
11+
* @Annotation
12+
* @Target({"METHOD","CLASS"})
13+
*/
14+
#[Attribute(Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
15+
class NotDebug extends AbstractAnnotation
16+
{
17+
18+
public function __construct(...$value)
19+
{
20+
parent::__construct(...$value);
21+
}
22+
}

src/annotation/NotDefaultAuthor.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace hg\apidoc\annotation;
4+
5+
use Attribute;
6+
use hg\apidoc\utils\AbstractAnnotation;
7+
8+
/**
9+
* 不使用默认作者
10+
* @package hg\apidoc\annotation
11+
* @Annotation
12+
* @Target({"METHOD"})
13+
*/
14+
#[Attribute(Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
15+
class NotDefaultAuthor extends AbstractAnnotation
16+
{
17+
18+
public function __construct(...$value)
19+
{
20+
parent::__construct(...$value);
21+
}
22+
}

src/annotation/NotHeaders.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace hg\apidoc\annotation;
4+
5+
use Attribute;
6+
use hg\apidoc\utils\AbstractAnnotation;
7+
8+
/**
9+
* 不使用配置中的全局请求Headers参数
10+
* @package hg\apidoc\annotation
11+
* @Annotation
12+
* @Target({"METHOD"})
13+
*/
14+
#[Attribute(Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
15+
class NotHeaders extends AbstractAnnotation
16+
{
17+
18+
public function __construct(...$value)
19+
{
20+
parent::__construct(...$value);
21+
}
22+
}

src/annotation/NotParams.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace hg\apidoc\annotation;
4+
5+
use Attribute;
6+
use hg\apidoc\utils\AbstractAnnotation;
7+
8+
/**
9+
* 不使用配置中的全局请求Body参数
10+
* @package hg\apidoc\annotation
11+
* @Annotation
12+
* @Target({"METHOD"})
13+
*/
14+
#[Attribute(Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
15+
class NotParams extends AbstractAnnotation
16+
{
17+
18+
public function __construct(...$value)
19+
{
20+
parent::__construct(...$value);
21+
}
22+
}

src/annotation/NotParse.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace hg\apidoc\annotation;
4+
5+
use Attribute;
6+
use hg\apidoc\utils\AbstractAnnotation;
7+
8+
/**
9+
* 标记不解析的控制器/方法
10+
* @package hg\apidoc\annotation
11+
* @Annotation
12+
* @Target({"METHOD","CLASS"})
13+
*/
14+
#[Attribute(Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
15+
class NotParse extends AbstractAnnotation
16+
{
17+
/**
18+
* @param string $value 不解析
19+
*/
20+
public function __construct(...$value)
21+
{
22+
parent::__construct(...$value);
23+
}
24+
}

src/annotation/NotQuerys.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace hg\apidoc\annotation;
4+
5+
use Attribute;
6+
use hg\apidoc\utils\AbstractAnnotation;
7+
8+
/**
9+
* 不使用配置中的全局请求Querys参数
10+
* @package hg\apidoc\annotation
11+
* @Annotation
12+
* @Target({"METHOD"})
13+
*/
14+
#[Attribute(Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
15+
class NotQuerys extends AbstractAnnotation
16+
{
17+
18+
public function __construct(...$value)
19+
{
20+
parent::__construct(...$value);
21+
}
22+
}

src/annotation/NotResponseError.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace hg\apidoc\annotation;
4+
5+
use Attribute;
6+
use hg\apidoc\utils\AbstractAnnotation;
7+
8+
/**
9+
* 不使用异常响应体返回数据
10+
* @package hg\apidoc\annotation
11+
* @Annotation
12+
* @Target({"METHOD"})
13+
*/
14+
#[Attribute(Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
15+
class NotResponseError extends AbstractAnnotation
16+
{
17+
18+
public function __construct(...$value)
19+
{
20+
parent::__construct(...$value);
21+
}
22+
}

src/annotation/NotResponseSuccess.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace hg\apidoc\annotation;
4+
5+
use Attribute;
6+
use hg\apidoc\utils\AbstractAnnotation;
7+
8+
/**
9+
* 不使用成功响应体返回数据
10+
* @package hg\apidoc\annotation
11+
* @Annotation
12+
* @Target({"METHOD"})
13+
*/
14+
#[Attribute(Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
15+
class NotResponseSuccess extends AbstractAnnotation
16+
{
17+
18+
public function __construct(...$value)
19+
{
20+
parent::__construct(...$value);
21+
}
22+
}

src/annotation/NotResponses.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace hg\apidoc\annotation;
4+
5+
use Attribute;
6+
use hg\apidoc\utils\AbstractAnnotation;
7+
8+
/**
9+
* 不使用统一响应体返回数据
10+
* @package hg\apidoc\annotation
11+
* @Annotation
12+
* @Target({"METHOD"})
13+
*/
14+
#[Attribute(Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
15+
class NotResponses extends AbstractAnnotation
16+
{
17+
18+
public function __construct(...$value)
19+
{
20+
parent::__construct(...$value);
21+
}
22+
}

src/annotation/Property.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace hg\apidoc\annotation;
4+
5+
use Attribute;
6+
7+
/**
8+
* 类的属性
9+
* @Annotation
10+
* @Target({"METHOD","ANNOTATION","PROPERTY"})
11+
*/
12+
#[Attribute(Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE | Attribute::TARGET_PROPERTY)]
13+
class Property extends ParamBase
14+
{
15+
16+
/**
17+
* mock
18+
* @var string
19+
*/
20+
public $mock;
21+
22+
/**
23+
* @param string $name 字段名
24+
* @param string $type 字段类型
25+
* @param bool $require 是否必须
26+
* @param string|int|bool $default 默认值
27+
* @param string|array $ref 引用注解/模型
28+
* @param string $table 引用数据表
29+
* @param string|array $field 指定Ref引入的字段
30+
* @param string|array $withoutField 排除Ref引入的字段
31+
* @param string $desc 字段名称
32+
* @param string $md Md文本内容
33+
* @param string $mock Mock规则
34+
* @param array $children 子参数
35+
* @param string $childrenField 为tree类型时指定children字段
36+
* @param string $childrenDesc 为tree类型时指定children字段说明
37+
* @param string $childrenType 为array类型时指定子节点类型
38+
*/
39+
public function __construct(
40+
$name = '',
41+
string $type = '',
42+
bool $require = false,
43+
$default = "",
44+
$ref = "",
45+
$table = "",
46+
$field = "",
47+
$withoutField = "",
48+
string $desc = '',
49+
string $md = "",
50+
string $mock = "",
51+
array $children = [],
52+
string $childrenField = "",
53+
string $childrenDesc = "children",
54+
string $childrenType = "",
55+
...$attrs
56+
)
57+
{
58+
parent::__construct(...func_get_args());
59+
}
60+
61+
62+
}

src/parses/ParseAnnotation.php

Lines changed: 81 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
use ReflectionAttribute;
99
use hg\apidoc\exception\ErrorException;
1010
use ReflectionMethod;
11-
use think\facade\Log;
11+
use ReflectionParameter;
12+
use support\Log;
1213

1314
class ParseAnnotation
1415
{
@@ -27,9 +28,10 @@ public function __construct($config)
2728
/**
2829
* 解析非@注解的文本注释
2930
* @param $refMethod
31+
* @param $isAll bool 是否获取全部,true则将带@开头的注释也包含
3032
* @return array|false
3133
*/
32-
public static function parseTextAnnotation($refMethod): array
34+
public static function parseTextAnnotation($refMethod,$isAll=false): array
3335
{
3436
$annotation = $refMethod->getDocComment();
3537
if (empty($annotation)) {
@@ -43,7 +45,7 @@ public static function parseTextAnnotation($refMethod): array
4345
$data = [];
4446
foreach ($lines[1] as $line) {
4547
$line = trim($line);
46-
if (!empty ($line) && strpos($line, '@') !== 0) {
48+
if (!empty ($line) && ($isAll===true || ($isAll===false && strpos($line, '@') !== 0))) {
4749
$data[] = $line;
4850
}
4951
}
@@ -92,8 +94,8 @@ protected function getParameters($attrList){
9294
}else{
9395
$name = $this->getClassName(get_class($item));
9496
$valueObj = Helper::objectToArray($item);
95-
if (isset($valueObj['name']) && count($valueObj)===1){
96-
$value = $valueObj['name'];
97+
if (array_key_exists('name',$valueObj) && count($valueObj)===1){
98+
$value = $valueObj['name']===null?true: $valueObj['name'];
9799
}else{
98100
$value = $valueObj;
99101
}
@@ -139,4 +141,78 @@ public function getMethodAnnotation(ReflectionMethod $refMethod){
139141
return $this->getParameters(array_merge($attributes,$readerAttributes));
140142
}
141143

144+
/**
145+
* 获取属性的注解参数
146+
* @param $property
147+
* @return array
148+
*/
149+
public function getPropertyAnnotation($property){
150+
if (method_exists($property,'getAttributes')){
151+
$attributes = $property->getAttributes();
152+
}else{
153+
$attributes = [];
154+
}
155+
$readerAttributes = $this->parser->getPropertyAnnotations($property);
156+
return $this->getParameters(array_merge($attributes,$readerAttributes));
157+
}
158+
159+
/**
160+
* 解析类的属性文本注释的var
161+
* @param $propertyTextAnnotations
162+
* @return array
163+
*/
164+
protected static function parsesPropertyTextAnnotation($propertyTextAnnotations){
165+
$varLine = "";
166+
foreach ($propertyTextAnnotations as $item) {
167+
if (strpos($item, '@var') !== false){
168+
$varLine = $item;
169+
break;
170+
}
171+
}
172+
$type = "";
173+
$desc = "";
174+
if ($varLine){
175+
$varLineArr = preg_split('/\\s+/', $varLine);
176+
$type = !empty($varLineArr[1])?$varLineArr[1]:"";
177+
$desc = !empty($varLineArr[2])?$varLineArr[2]:"";
178+
}
179+
if (empty($desc) && strpos($propertyTextAnnotations[0], '@var') === false){
180+
$desc = $propertyTextAnnotations[0];
181+
}
182+
return [
183+
'type'=>$type,
184+
'desc'=>$desc,
185+
];
186+
}
187+
188+
/**
189+
* 获取类的属性参数
190+
* @param $classReflect
191+
* @return array
192+
*/
193+
public function getClassPropertiesy($classReflect){
194+
$publicProperties = $classReflect->getProperties(\ReflectionProperty::IS_PUBLIC);
195+
$arr=[];
196+
foreach ($publicProperties as $property) {
197+
$propertyAnnotations = $this->getPropertyAnnotation($property);
198+
$item = [];
199+
if (!empty($propertyAnnotations['property'])){
200+
// 有apidoc注解
201+
$arr[] = $propertyAnnotations['property'];
202+
continue;
203+
}
204+
$propertyTextAnnotations = self::parseTextAnnotation($property,true);
205+
if (empty($propertyTextAnnotations)){
206+
// 无注释
207+
continue;
208+
}
209+
$textAnnotationsParams=static::parsesPropertyTextAnnotation($propertyTextAnnotations);
210+
$textAnnotationsParams['name'] =$property->getName();
211+
$arr[]=$textAnnotationsParams;
212+
}
213+
return $arr;
214+
}
215+
216+
217+
142218
}

0 commit comments

Comments
 (0)