33
44namespace HPlus \Swagger \Swagger ;
55
6- use Doctrine \ Common \ Annotations \ AnnotationReader ;
6+ use Exception ;
77use HPlus \Route \Annotation \ApiController ;
88use HPlus \Route \Annotation \AdminController ;
9+ use HPlus \Route \Annotation \GetApi ;
10+ use HPlus \Route \Annotation \Mapping ;
911use HPlus \Route \Annotation \Query ;
1012use HPlus \Swagger \Annotation \ApiDefinition ;
1113use HPlus \Swagger \Annotation \ApiDefinitions ;
1820use HPlus \Swagger \ApiAnnotation ;
1921use Hyperf \Contract \ConfigInterface ;
2022use Hyperf \Di \ReflectionManager ;
21- use Hyperf \HttpServer \Annotation \Mapping ;
2223use Hyperf \Utils \ApplicationContext ;
2324use Hyperf \Utils \Arr ;
2425use Hyperf \Utils \Str ;
@@ -42,10 +43,7 @@ public function __construct($server)
4243
4344 public function addPath ($ className , $ methodName , $ path )
4445 {
45- $ ignores = $ this ->config ->get ('annotations.scan.ignore_annotations ' , []);
46- foreach ($ ignores as $ ignore ) {
47- AnnotationReader::addGlobalIgnoredName ($ ignore );
48- }
46+
4947 $ classAnnotation = ApiAnnotation::classMetadata ($ className );
5048 $ controlerAnno = $ classAnnotation [ApiController::class] ?? $ classAnnotation [AdminController::class] ?? null ;
5149 $ serverAnno = $ classAnnotation [ApiServer::class] ?? null ;
@@ -56,19 +54,20 @@ public function addPath($className, $methodName, $path)
5654 $ servers = $ this ->config ->get ('server.servers ' );
5755 $ servers_name = array_column ($ servers , 'name ' );
5856 if (!in_array ($ bindServer , $ servers_name )) {
59- throw new \ Exception (sprintf ('The bind ApiServer name [%s] not found, defined in %s! ' , $ bindServer , $ className ));
57+ throw new Exception (sprintf ('The bind ApiServer name [%s] not found, defined in %s! ' , $ bindServer , $ className ));
6058 }
6159
6260 if ($ bindServer !== $ this ->server ) {
6361 return ;
6462 }
63+
6564 $ methodAnnotations = ApiAnnotation::methodMetadata ($ className , $ methodName );
6665 if (!$ controlerAnno || !$ methodAnnotations ) {
6766 return ;
6867 }
6968 $ params = [];
7069 $ responses = [];
71- /** @var \HPlus\Route\Annotation\ GetApi $mapping */
70+ /** @var GetApi $mapping */
7271 $ mapping = null ;
7372 $ consumes = null ;
7473 $ rules = [];
@@ -77,7 +76,7 @@ public function addPath($className, $methodName, $path)
7776 if ($ option instanceof RequestValidation) {
7877 $ rules = array_merge ($ rules , $ this ->getValidateRule ($ option ));
7978 if ($ option ->dateType == 'json ' ) {
80- $ param = new Body ();
79+ $ param = new Body ("" );
8180 $ param ->rules = $ this ->getValidateRule ($ option );
8281 $ param ->name = "body " ;
8382 $ param ->key = "body " ;
@@ -99,6 +98,7 @@ public function addPath($className, $methodName, $path)
9998 $ consumes = $ this ->getConsumes ($ option ->dateType );
10099 continue ;
101100 }
101+
102102 if ($ option instanceof Mapping) {
103103 $ mapping = $ option ;
104104 }
@@ -132,12 +132,13 @@ public function addPath($className, $methodName, $path)
132132 }
133133 $ path = str_replace ("/_self_path " , "" , $ path );
134134 $ path = $ this ->getPath ($ path );
135- $ method = strtolower ($ mapping ->methods [0 ]);
135+
136+ $ method = strtolower ($ mapping ->methods [0 ] ?? '' );
136137 $ this ->swagger ['paths ' ][$ path ][$ method ] = [
137138 'tags ' => [$ tag ],
138139 'summary ' => $ mapping ->summary ?? '' ,
139140 'description ' => $ mapping ->description ?? '' ,
140- 'operationId ' => implode ('' , array_map ('ucfirst ' , explode ('/ ' , $ path ))) . $ mapping ->methods [0 ],
141+ 'operationId ' => implode ('' , array_map ('ucfirst ' , explode ('/ ' , $ path ))) . ( $ mapping ->methods [0 ] ?? '' ) ,
141142 'parameters ' => $ this ->makeParameters ($ params , $ path , $ method ),
142143 'produces ' => [
143144 $ consumes
@@ -147,7 +148,7 @@ public function addPath($className, $methodName, $path)
147148 if ($ consumes !== null ) {
148149 $ this ->swagger ['paths ' ][$ path ][$ method ]['consumes ' ] = [$ consumes ];
149150 }
150- if (property_exists ($ mapping , 'security ' ) && $ mapping ->security && isset ($ this ->swagger ['securityDefinitions ' ])) {
151+ if ($ mapping && property_exists ($ mapping , 'security ' ) && $ mapping ->security && isset ($ this ->swagger ['securityDefinitions ' ])) {
151152 foreach ($ this ->swagger ['securityDefinitions ' ] as $ key => $ val ) {
152153 $ this ->swagger ['paths ' ][$ path ][$ method ]['security ' ][] = [$ key => $ val ['petstore_auth ' ] ?? []];
153154 }
@@ -168,7 +169,7 @@ private function getConsumes($type = 'json')
168169
169170 private function getValidateRule (RequestValidation $ validation )
170171 {
171- if (class_exists ($ validation ->validate )) {
172+ if ($ validation -> validate && class_exists ($ validation ->validate )) {
172173 $ rolesModel = ReflectionManager::reflectClass ($ validation ->validate )->getDefaultProperties ();
173174 $ rules = $ rolesModel ['scene ' ][$ validation ->scene ] ?? [];
174175 $ fields = $ rolesModel ['field ' ] ?? [];
@@ -254,7 +255,7 @@ private function rules2schema($name, $rules)
254255
255256 public function getTypeByRule ($ rule )
256257 {
257- if (empty ($ rule )) {
258+ if (empty ($ rule )) {
258259 return 'string ' ;
259260 }
260261 $ default = explode ('| ' , preg_replace ('/\[.*\]/ ' , '' , $ rule ));
@@ -294,7 +295,7 @@ public function makeParameters($params, $path, $method)
294295 $ parameters = [];
295296 /** @var Query $item */
296297 foreach ($ params as $ item ) {
297- if ($ item ->rule !== null && in_array ('array ' , explode ('| ' , $ item ->rule ))) {
298+ if ($ item ->rule && $ item -> rule !== null && in_array ('array ' , explode ('| ' , $ item ->rule ))) {
298299 $ item ->name .= '[] ' ;
299300 }
300301 $ parameters [$ item ->key ] = [
@@ -467,7 +468,7 @@ public function save()
467468 $ this ->swagger ['tags ' ] = array_values ($ this ->swagger ['tags ' ] ?? []);
468469 $ outputFile = $ this ->config ->get ('swagger.output_file ' );
469470 if (!$ outputFile ) {
470- throw new \ Exception ('/config/autoload/swagger.php need set output_file ' );
471+ throw new Exception ('/config/autoload/swagger.php need set output_file ' );
471472 }
472473 $ outputFile = str_replace ('{server} ' , $ this ->server , $ outputFile );
473474 file_put_contents ($ outputFile , json_encode ($ this ->swagger , JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES ));
0 commit comments