Skip to content

Commit 2bc2198

Browse files
author
jiangjianyong
committed
添加QueryParam从FormRequest子类获取的支持
1 parent c48cf5e commit 2bc2198

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

src/Tools/Generator.php

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function processRoute(Route $route, array $rulesToApply = [])
6161
$routeGroup = $this->getRouteGroup($controller, $method);
6262
$docBlock = $this->parseDocBlock($method);
6363
$bodyParameters = $this->getBodyParameters($method, $docBlock['tags']);
64-
$queryParameters = $this->getQueryParametersFromDocBlock($docBlock['tags']);
64+
$queryParameters = $this->getQueryParameters($method, $docBlock['tags']);
6565
$content = ResponseResolver::getResponse($route, $docBlock['tags'], [
6666
'rules' => $rulesToApply,
6767
'body' => $bodyParameters,
@@ -76,9 +76,9 @@ public function processRoute(Route $route, array $rulesToApply = [])
7676
'methods' => $this->getMethods($route),
7777
'uri' => $this->getUri($route),
7878
'boundUri' => Utils::getFullUrl($route, $rulesToApply['bindings'] ?? []),
79+
'queryParameters' => $queryParameters,
7980
'bodyParameters' => $bodyParameters,
8081
'cleanBodyParameters' => $this->cleanParams($bodyParameters),
81-
'queryParameters' => $queryParameters,
8282
'authenticated' => $this->getAuthStatusFromDocBlock($docBlock['tags']),
8383
'response' => $content,
8484
'showresponse' => ! empty($content),
@@ -157,6 +157,42 @@ protected function getBodyParametersFromDocBlock(array $tags)
157157
return $parameters;
158158
}
159159

160+
/**
161+
* @param ReflectionMethod $method
162+
* @param array $tags
163+
* @return array
164+
*/
165+
protected function getQueryParameters(ReflectionMethod $method, array $tags)
166+
{
167+
foreach ($method->getParameters() as $param) {
168+
$paramType = $param->getType();
169+
if ($paramType === null) {
170+
continue;
171+
}
172+
173+
$parameterClassName = version_compare(phpversion(), '7.1.0', '<')
174+
? $paramType->__toString()
175+
: $paramType->getName();
176+
177+
try {
178+
$parameterClass = new ReflectionClass($parameterClassName);
179+
} catch (\ReflectionException $e) {
180+
continue;
181+
}
182+
183+
if (class_exists('\Illuminate\Foundation\Http\FormRequest') && $parameterClass->isSubclassOf(\Illuminate\Foundation\Http\FormRequest::class)) {
184+
$formRequestDocBlock = new DocBlock($parameterClass->getDocComment());
185+
$queryParametersFromDocBlock = $this->getQueryParametersFromDocBlock($formRequestDocBlock->getTags());
186+
187+
if (count($queryParametersFromDocBlock)) {
188+
return $queryParametersFromDocBlock;
189+
}
190+
}
191+
}
192+
193+
return $this->getQueryParametersFromDocBlock($tags);
194+
}
195+
160196
/**
161197
* @param array $tags
162198
*

0 commit comments

Comments
 (0)