/
OpenApiPhpDescriber.php
239 lines (191 loc) · 8.53 KB
/
OpenApiPhpDescriber.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
<?php
/*
* This file is part of the NelmioApiDocBundle package.
*
* (c) Nelmio
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Nelmio\ApiDocBundle\Describer;
use Doctrine\Common\Annotations\Reader;
use Nelmio\ApiDocBundle\Annotation\Operation;
use Nelmio\ApiDocBundle\Annotation\Security;
use Nelmio\ApiDocBundle\OpenApiPhp\Util;
use Nelmio\ApiDocBundle\Util\ControllerReflector;
use Nelmio\ApiDocBundle\Util\SetsContextTrait;
use OpenApi\Analysers\AttributeAnnotationFactory;
use OpenApi\Annotations as OA;
use OpenApi\Generator;
use Psr\Log\LoggerInterface;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
// Help opcache.preload discover Swagger\Annotations\Swagger
class_exists(OA\OpenApi::class);
final class OpenApiPhpDescriber
{
use SetsContextTrait;
private RouteCollection $routeCollection;
private ControllerReflector $controllerReflector;
private ?Reader $annotationReader;
private LoggerInterface $logger;
public function __construct(RouteCollection $routeCollection, ControllerReflector $controllerReflector, ?Reader $annotationReader, LoggerInterface $logger, bool $overwrite = false)
{
if ($overwrite || func_num_args() > 4) {
trigger_deprecation('nelmio/api-doc-bundle', '4.25.2', 'The "$overwrite" argument of "%s" is unused and therefore deprecated.', __METHOD__);
}
$this->routeCollection = $routeCollection;
$this->controllerReflector = $controllerReflector;
$this->annotationReader = $annotationReader;
$this->logger = $logger;
}
public function describe(OA\OpenApi $api): void
{
$classAnnotations = [];
/** @var \ReflectionMethod $method */
foreach ($this->getMethodsToParse() as $method => [$path, $httpMethods, $routeName]) {
$declaringClass = $method->getDeclaringClass();
$path = Util::getPath($api, $path);
$context = Util::createContext(['nested' => $path], $path->_context);
$context->namespace = $declaringClass->getNamespaceName();
$context->class = $declaringClass->getShortName();
$context->method = $method->name;
$context->filename = $method->getFileName();
$this->setContext($context);
if (!array_key_exists($declaringClass->getName(), $classAnnotations)) {
$classAnnotations = [];
if (null !== $this->annotationReader) {
$classAnnotations = $this->annotationReader->getClassAnnotations($declaringClass);
}
$classAnnotations = array_filter($classAnnotations, function ($v) {
return $v instanceof OA\AbstractAnnotation;
});
$classAnnotations = array_merge($classAnnotations, $this->getAttributesAsAnnotation($declaringClass, $context));
$classAnnotations[$declaringClass->getName()] = $classAnnotations;
}
$annotations = [];
if (null !== $this->annotationReader) {
$annotations = array_filter($this->annotationReader->getMethodAnnotations($method), function ($v) {
return $v instanceof OA\AbstractAnnotation;
});
}
$annotations = array_merge($annotations, $this->getAttributesAsAnnotation($method, $context));
if (0 === count($annotations) && 0 === count($classAnnotations[$declaringClass->getName()])) {
continue;
}
$implicitAnnotations = [];
$mergeProperties = new \stdClass();
foreach (array_merge($annotations, $classAnnotations[$declaringClass->getName()]) as $annotation) {
if ($annotation instanceof Operation) {
foreach ($httpMethods as $httpMethod) {
$operation = Util::getOperation($path, $httpMethod);
$operation->mergeProperties($annotation);
}
continue;
}
if ($annotation instanceof OA\Operation) {
if (!in_array($annotation->method, $httpMethods, true)) {
continue;
}
if (Generator::UNDEFINED !== $annotation->path && $path->path !== $annotation->path) {
continue;
}
$operation = Util::getOperation($path, $annotation->method);
$operation->mergeProperties($annotation);
continue;
}
if ($annotation instanceof Security) {
$annotation->validate();
if (null === $annotation->name) {
$mergeProperties->security = [];
continue;
}
$mergeProperties->security[] = [$annotation->name => $annotation->scopes];
continue;
}
if ($annotation instanceof OA\Tag) {
$annotation->validate();
$mergeProperties->tags[] = $annotation->name;
continue;
}
if (
!$annotation instanceof OA\Response
&& !$annotation instanceof OA\RequestBody
&& !$annotation instanceof OA\Parameter
&& !$annotation instanceof OA\ExternalDocumentation
) {
throw new \LogicException(sprintf('Using the annotation "%s" as a root annotation in "%s::%s()" is not allowed.', get_class($annotation), $method->getDeclaringClass()->name, $method->name));
}
$implicitAnnotations[] = $annotation;
}
if ([] === $implicitAnnotations && [] === get_object_vars($mergeProperties)) {
continue;
}
foreach ($httpMethods as $httpMethod) {
$operation = Util::getOperation($path, $httpMethod);
$operation->merge($implicitAnnotations);
$operation->mergeProperties($mergeProperties);
if (Generator::UNDEFINED === $operation->operationId) {
$operation->operationId = $httpMethod.'_'.$routeName;
}
}
}
// Reset the Generator after the parsing
$this->setContext(null);
}
private function getMethodsToParse(): \Generator
{
foreach ($this->routeCollection->all() as $routeName => $route) {
if (!$route->hasDefault('_controller')) {
continue;
}
$controller = $route->getDefault('_controller');
$reflectedMethod = $this->controllerReflector->getReflectionMethod($controller);
if (null === $reflectedMethod) {
continue;
}
$path = $this->normalizePath($route->getPath());
$supportedHttpMethods = $this->getSupportedHttpMethods($route);
if ([] === $supportedHttpMethods) {
$this->logger->warning('None of the HTTP methods specified for path {path} are supported by swagger-ui, skipping this path', [
'path' => $path,
]);
continue;
}
yield $reflectedMethod => [$path, $supportedHttpMethods, $routeName];
}
}
/**
* @return string[]
*/
private function getSupportedHttpMethods(Route $route): array
{
$allMethods = Util::OPERATIONS;
$methods = array_map('strtolower', $route->getMethods());
// an empty array means that any method is allowed
if ([] === $methods) {
return $allMethods;
}
return array_intersect($methods, $allMethods);
}
private function normalizePath(string $path): string
{
if ('.{_format}' === substr($path, -10)) {
$path = substr($path, 0, -10);
}
return $path;
}
/**
* @param \ReflectionClass<object>|\ReflectionMethod $reflection
*
* @return OA\AbstractAnnotation[]
*/
private function getAttributesAsAnnotation($reflection, \OpenApi\Context $context): array
{
$attributesFactory = new AttributeAnnotationFactory();
$attributes = $attributesFactory->build($reflection, $context);
// The attributes factory removes the context after executing so we need to set it back...
$this->setContext($context);
return $attributes;
}
}