Skip to content

Commit 605e8e4

Browse files
author
Christian Bader
committed
Added support for multiple route definitions on a single method
1 parent e819961 commit 605e8e4

3 files changed

Lines changed: 75 additions & 55 deletions

File tree

src/Contract/Service/ControllerMethodParserInterface.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,8 @@
88

99
interface ControllerMethodParserInterface
1010
{
11-
public function parseMethod(\ReflectionMethod $method): MethodDoc;
11+
/**
12+
* @return MethodDoc[]
13+
*/
14+
public function parseMethod(\ReflectionMethod $method): array;
1215
}

src/Service/ControllerMethodParser.php

Lines changed: 67 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -48,90 +48,105 @@ public function __construct(
4848
) {
4949
}
5050

51-
public function parseMethod(\ReflectionMethod $method): MethodDoc
51+
/**
52+
* @return MethodDoc[]
53+
*/
54+
public function parseMethod(\ReflectionMethod $method): array
5255
{
53-
$routeDoc = $this->parseRoute($method);
56+
$routeDocs = $this->parseRoute($method);
5457
$requestDoc = $this->parseRequest($method);
5558
$responseDoc = $this->parseResponses($method);
5659

57-
$methodDoc = new MethodDoc();
58-
$methodDoc
59-
->setName($method->getName())
60-
->setResponsesDoc($responseDoc)
61-
->setRouteDoc($routeDoc)
62-
;
60+
$methodDocs = [];
61+
62+
foreach ($routeDocs as $idx => $routeDoc) {
63+
$methodDoc = new MethodDoc();
64+
$methodDoc
65+
->setName($method->getName() . '-' . $idx)
66+
->setResponsesDoc($responseDoc)
67+
->setRouteDoc($routeDoc)
68+
;
6369

64-
if ($requestDoc instanceof RequestDoc) {
65-
$methodDoc->setRequestDoc($requestDoc);
70+
if ($requestDoc instanceof RequestDoc) {
71+
$methodDoc->setRequestDoc($requestDoc);
72+
}
73+
74+
$methodDocs[] = $methodDoc;
6675
}
6776

68-
return $methodDoc;
77+
return $methodDocs;
6978
}
7079

71-
private function parseRoute(\ReflectionMethod $method): RouteDoc
80+
/**
81+
* @param RouteDoc[]
82+
*/
83+
private function parseRoute(\ReflectionMethod $method): array
7284
{
85+
$routeDocs = [];
86+
7387
$attributes = $method->getAttributes();
7488

7589
foreach ($attributes as $attribute) {
7690
if ($attribute->getName() === RouteAnnotation::class || $attribute->getName() === RouteAttribute::class) {
7791
$routeAttributeArguments = $attribute->getArguments();
7892

79-
break;
80-
}
81-
}
93+
if (!isset($routeAttributeArguments['path'], $routeAttributeArguments['methods'])) {
94+
throw new IncompleteRouteException(sprintf('Route in %s::%s not defined or missing attributes "path" and/or "methods".', $method->getDeclaringClass()->getName(), $method->getName()));
95+
}
8296

83-
if (!isset($routeAttributeArguments['path'], $routeAttributeArguments['methods'])) {
84-
throw new IncompleteRouteException(sprintf('Route in %s::%s not defined or missing attributes "path" and/or "methods".', $method->getDeclaringClass()->getName(), $method->getName()));
85-
}
97+
if (!isset($routeAttributeArguments['name'])) {
98+
throw new IncompleteRouteException(sprintf('Route in %s::%s does not have a "name" property.', $method->getDeclaringClass()->getName(), $method->getName()));
99+
}
86100

87-
if (!isset($routeAttributeArguments['name'])) {
88-
throw new IncompleteRouteException(sprintf('Route in %s::%s does not have a "name" property.', $method->getDeclaringClass()->getName(), $method->getName()));
89-
}
101+
$route = $this->router->getRouteCollection()->get($routeAttributeArguments['name']);
90102

91-
$route = $this->router->getRouteCollection()->get($routeAttributeArguments['name']);
103+
if (!$route instanceof \Symfony\Component\Routing\Route) {
104+
throw new IncompleteRouteException(sprintf('Route %s not found.', $routeAttributeArguments['name']));
105+
}
92106

93-
if (!$route instanceof \Symfony\Component\Routing\Route) {
94-
throw new IncompleteRouteException(sprintf('Route %s not found.', $routeAttributeArguments['name']));
95-
}
107+
$path = $route->getPath();
108+
preg_match_all('/{([^}]+)}/', $path, $routeParameters);
96109

97-
$path = $route->getPath();
98-
preg_match_all('/{([^}]+)}/', $path, $routeParameters);
110+
$parsedParameters = [];
99111

100-
$parsedParameters = [];
112+
foreach ($routeParameters[1] as $routeParameter) {
113+
$parameterDoc = new ParameterDoc();
114+
115+
$parameterDoc
116+
->setName($routeParameter)
117+
->setIn(ParameterDoc::IN_PATH)
118+
->setRequired(true)
119+
->setSchema([
120+
'type' => 'string',
121+
])
122+
;
101123

102-
foreach ($routeParameters[1] as $routeParameter) {
103-
$parameterDoc = new ParameterDoc();
124+
$parsedParameters[] = $parameterDoc;
125+
}
104126

105-
$parameterDoc
106-
->setName($routeParameter)
107-
->setIn(ParameterDoc::IN_PATH)
108-
->setRequired(true)
109-
->setSchema([
110-
'type' => 'string',
111-
])
112-
;
127+
$routeDoc = new RouteDoc();
113128

114-
$parsedParameters[] = $parameterDoc;
115-
}
129+
$methods = $routeAttributeArguments['methods'];
116130

117-
$routeDoc = new RouteDoc();
131+
if (is_array($methods)) {
132+
if (count($methods) > 1) {
133+
throw new UnsupportedRouteException(sprintf('Route %s has multiple methods. This is not yet supported.', $routeAttributeArguments['name']));
134+
}
135+
$methods = $methods[0];
136+
}
118137

119-
$methods = $routeAttributeArguments['methods'];
138+
$routeDoc
139+
->setPath($path)
140+
->setMethod(strtolower((string) $methods))
141+
->setParameters($parsedParameters)
142+
;
120143

121-
if (is_array($methods)) {
122-
if (count($methods) > 1) {
123-
throw new UnsupportedRouteException(sprintf('Route %s has multiple methods. This is not yet supported.', $routeAttributeArguments['name']));
144+
$routeDocs[] = $routeDoc;
124145
}
125-
$methods = $methods[0];
126146
}
127147

128-
$routeDoc
129-
->setPath($path)
130-
->setMethod(strtolower((string) $methods))
131-
->setParameters($parsedParameters)
132-
;
133148

134-
return $routeDoc;
149+
return $routeDocs;
135150
}
136151

137152
private function parseRequest(\ReflectionMethod $method): ?RequestDoc

src/Service/DocsGenerator.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,10 @@ private function generateControllerDoc(string $controllerClass): ControllerDoc
7676
$controllerDoc = new ControllerDoc();
7777

7878
foreach ($methods as $method) {
79-
$methodDoc = $this->controllerMethodParser->parseMethod($method);
80-
$controllerDoc->addMethodDoc($methodDoc);
79+
$methodDocs = $this->controllerMethodParser->parseMethod($method);
80+
foreach ($methodDocs as $methodDoc) {
81+
$controllerDoc->addMethodDoc($methodDoc);
82+
}
8183
}
8284

8385
return $controllerDoc;

0 commit comments

Comments
 (0)