-
Notifications
You must be signed in to change notification settings - Fork 941
/
Copy pathAttributeAnnotationFactory.php
159 lines (137 loc) · 6.78 KB
/
AttributeAnnotationFactory.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
<?php declare(strict_types=1);
/**
* @license Apache 2.0
*/
namespace OpenApi\Analysers;
use OpenApi\Annotations as OA;
use OpenApi\Context;
use OpenApi\Generator;
class AttributeAnnotationFactory implements AnnotationFactoryInterface
{
use GeneratorAwareTrait;
public function isSupported(): bool
{
return \PHP_VERSION_ID >= 80100;
}
public function build(\Reflector $reflector, Context $context): array
{
if (!$this->isSupported() || !method_exists($reflector, 'getAttributes')) {
return [];
}
if ($reflector instanceof \ReflectionProperty && method_exists($reflector, 'isPromoted') && $reflector->isPromoted()) {
// handled via __construct() parameter
return [];
}
// no proper way to inject
Generator::$context = $context;
/** @var OA\AbstractAnnotation[] $annotations */
$annotations = [];
try {
foreach ($reflector->getAttributes() as $attribute) {
if (class_exists($attribute->getName())) {
$instance = $attribute->newInstance();
if ($instance instanceof OA\AbstractAnnotation) {
$annotations[] = $instance;
} else {
if ($context->is('other') === false) {
$context->other = [];
}
$context->other[] = $instance;
}
} else {
$context->logger->debug(sprintf('Could not instantiate attribute "%s"; class not found.', $attribute->getName()));
}
}
if ($reflector instanceof \ReflectionMethod) {
// also look at parameter attributes
foreach ($reflector->getParameters() as $rp) {
foreach ([OA\Property::class, OA\Parameter::class, OA\RequestBody::class] as $attributeName) {
foreach ($rp->getAttributes($attributeName, \ReflectionAttribute::IS_INSTANCEOF) as $attribute) {
/** @var OA\Property|OA\Parameter|OA\RequestBody $instance */
$instance = $attribute->newInstance();
$type = (($rnt = $rp->getType()) && $rnt instanceof \ReflectionNamedType) ? $rnt->getName() : Generator::UNDEFINED;
$nullable = $rnt ? $rnt->allowsNull() : true;
if ($instance instanceof OA\RequestBody) {
$instance->required = !$nullable;
} elseif ($instance instanceof OA\Property) {
if (Generator::isDefault($instance->property)) {
$instance->property = $rp->getName();
}
if (Generator::isDefault($instance->type)) {
$instance->type = $type;
}
$instance->nullable = $nullable ?: Generator::UNDEFINED;
if ($rp->isPromoted()) {
// ensure each property has its own context
$instance->_context = new Context(['generated' => true, 'annotations' => [$instance]], $context);
// promoted parameter - docblock is available via class/property
if ($comment = $rp->getDeclaringClass()->getProperty($rp->getName())->getDocComment()) {
$instance->_context->comment = $comment;
}
}
} else {
if (!$instance->name || Generator::isDefault($instance->name)) {
$instance->name = $rp->getName();
}
$instance->required = !$nullable;
$context = new Context(['nested' => $this], $context);
$context->comment = null;
$instance->merge([new OA\Schema(['type' => $type, '_context' => $context])]);
}
$annotations[] = $instance;
}
}
}
if (($rrt = $reflector->getReturnType()) && $rrt instanceof \ReflectionNamedType) {
foreach ($annotations as $annotation) {
if ($annotation instanceof OA\Property && Generator::isDefault($annotation->type)) {
// pick up simple return types
$annotation->type = $rrt->getName();
}
}
}
}
} finally {
Generator::$context = null;
}
// merge backwards into parents...
$isParent = function (OA\AbstractAnnotation $annotation, OA\AbstractAnnotation $possibleParent): bool {
// regular annotation hierarchy
$explicitParent = null !== $possibleParent->matchNested($annotation) && !$annotation instanceof OA\Attachable;
$isParentAllowed = false;
// support Attachable subclasses
if ($isAttachable = $annotation instanceof OA\Attachable) {
if (!$isParentAllowed = (null === $annotation->allowedParents())) {
// check for allowed parents
foreach ($annotation->allowedParents() as $allowedParent) {
if ($possibleParent instanceof $allowedParent) {
$isParentAllowed = true;
break;
}
}
}
}
// Attachables can always be nested (unless explicitly restricted)
return ($isAttachable && $isParentAllowed)
|| ($annotation->getRoot() !== $possibleParent->getRoot() && $explicitParent);
};
$annotationsWithoutParent = [];
foreach ($annotations as $index => $annotation) {
$mergedIntoParent = false;
for ($ii = 0; $ii < count($annotations); ++$ii) {
if ($ii === $index) {
continue;
}
$possibleParent = $annotations[$ii];
if ($isParent($annotation, $possibleParent)) {
$mergedIntoParent = true; //
$possibleParent->merge([$annotation]);
}
}
if (!$mergedIntoParent) {
$annotationsWithoutParent[] = $annotation;
}
}
return $annotationsWithoutParent;
}
}