|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace BEAR\Resource; |
|
6
|
|
|
|
|
7
|
|
|
use BEAR\Resource\Annotation\Embed; |
|
8
|
|
|
use BEAR\Resource\Annotation\JsonSchema; |
|
9
|
|
|
use BEAR\Resource\Annotation\Link; |
|
10
|
|
|
use Doctrine\Common\Annotations\Reader; |
|
11
|
|
|
use Ray\Di\Di\Named; |
|
12
|
|
|
use Ray\WebContextParam\Annotation\AbstractWebContextParam; |
|
13
|
|
|
use Ray\WebContextParam\Annotation\CookieParam; |
|
14
|
|
|
use Ray\WebContextParam\Annotation\EnvParam; |
|
15
|
|
|
use Ray\WebContextParam\Annotation\FilesParam; |
|
16
|
|
|
use Ray\WebContextParam\Annotation\FormParam; |
|
17
|
|
|
use Ray\WebContextParam\Annotation\QueryParam; |
|
18
|
|
|
use Ray\WebContextParam\Annotation\ServerParam; |
|
19
|
|
|
use ReflectionMethod; |
|
20
|
|
|
|
|
21
|
|
|
use function assert; |
|
22
|
|
|
use function class_exists; |
|
23
|
|
|
use function file_exists; |
|
24
|
|
|
use function file_get_contents; |
|
25
|
|
|
use function json_decode; |
|
26
|
|
|
|
|
27
|
|
|
use const JSON_THROW_ON_ERROR; |
|
28
|
|
|
|
|
29
|
|
|
final class OptionsMethods |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* Constants for annotation name and "in" name |
|
33
|
|
|
*/ |
|
34
|
|
|
private const WEB_CONTEXT_NAME = [ |
|
35
|
|
|
CookieParam::class => 'cookie', |
|
36
|
|
|
EnvParam::class => 'env', |
|
37
|
|
|
FormParam::class => 'formData', |
|
38
|
|
|
QueryParam::class => 'query', |
|
39
|
|
|
ServerParam::class => 'server', |
|
40
|
|
|
FilesParam::class => 'files', |
|
41
|
|
|
]; |
|
42
|
|
|
|
|
43
|
|
|
public function __construct( |
|
44
|
|
|
private Reader $reader, |
|
45
|
|
|
#[Named('json_schema_dir')] private string $schemaDir = '', |
|
46
|
|
|
) { |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* return array{summary?: string, description?: string, request: array, links: array, embed: array} |
|
51
|
|
|
* |
|
52
|
|
|
* @return array<int|string, array<mixed>|string> |
|
53
|
|
|
*/ |
|
54
|
|
|
public function __invoke(ResourceObject $ro, string $requestMethod): array |
|
55
|
|
|
{ |
|
56
|
|
|
$method = new ReflectionMethod($ro::class, 'on' . $requestMethod); |
|
57
|
|
|
$ins = $this->getInMap($method); |
|
58
|
|
|
[$doc, $paramDoc] = (new OptionsMethodDocBolck())($method); |
|
59
|
|
|
$methodOption = $doc; |
|
60
|
|
|
$paramMetas = (new OptionsMethodRequest($this->reader))($method, $paramDoc, $ins); |
|
61
|
|
|
$schema = $this->getJsonSchema($method); |
|
62
|
|
|
$request = $paramMetas ? ['request' => $paramMetas] : []; |
|
63
|
|
|
$methodOption += $request; |
|
64
|
|
|
if (! empty($schema)) { |
|
65
|
|
|
$methodOption += ['schema' => $schema]; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$extras = $this->getMethodExtras($method); |
|
69
|
|
|
if (! empty($extras)) { |
|
70
|
|
|
$methodOption += $extras; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
return $methodOption; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @return (Embed|Link)[][] |
|
78
|
|
|
* @psalm-return array{links?: non-empty-list<Link>, embed?: non-empty-list<Embed>} |
|
79
|
|
|
* @phpstan-return (Embed|Link)[][] |
|
80
|
|
|
*/ |
|
81
|
|
|
private function getMethodExtras(ReflectionMethod $method): array |
|
82
|
|
|
{ |
|
83
|
|
|
$extras = []; |
|
84
|
|
|
$annotations = $this->reader->getMethodAnnotations($method); |
|
85
|
|
|
foreach ($annotations as $annotation) { |
|
86
|
|
|
if ($annotation instanceof Link) { |
|
87
|
|
|
$extras['links'][] = $annotation; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
if (! ($annotation instanceof Embed)) { |
|
91
|
|
|
continue; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$extras['embed'][] = $annotation; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
return $extras; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** @return array<string, string> */ |
|
101
|
|
|
private function getInMap(ReflectionMethod $method): array |
|
102
|
|
|
{ |
|
103
|
|
|
$ins = []; |
|
104
|
|
|
$annotations = $this->reader->getMethodAnnotations($method); |
|
105
|
|
|
$ins = $this->getInsFromMethodAnnotations($annotations, $ins); |
|
106
|
|
|
if ($ins) { |
|
107
|
|
|
return $ins; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** @var array<string, string> $insParam */ |
|
111
|
|
|
$insParam = $this->getInsFromParameterAttributes($method, $ins); |
|
112
|
|
|
|
|
113
|
|
|
return $insParam; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** @return array<string, mixed> */ |
|
117
|
|
|
private function getJsonSchema(ReflectionMethod $method): array |
|
118
|
|
|
{ |
|
119
|
|
|
$schema = $this->reader->getMethodAnnotation($method, JsonSchema::class); |
|
120
|
|
|
if (! $schema instanceof JsonSchema) { |
|
121
|
|
|
return []; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
$schemaFile = $this->schemaDir . '/' . $schema->schema; |
|
125
|
|
|
if (! file_exists($schemaFile)) { |
|
126
|
|
|
return []; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
return (array) json_decode((string) file_get_contents($schemaFile), null, 512, JSON_THROW_ON_ERROR); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @param array<object> $annotations |
|
134
|
|
|
* @param array<string, string> $ins |
|
135
|
|
|
* |
|
136
|
|
|
* @return array<string, string> |
|
137
|
|
|
*/ |
|
138
|
|
|
public function getInsFromMethodAnnotations(array $annotations, array $ins): array |
|
139
|
|
|
{ |
|
140
|
|
|
foreach ($annotations as $annotation) { |
|
141
|
|
|
if (! ($annotation instanceof AbstractWebContextParam)) { |
|
142
|
|
|
continue; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
$class = $annotation::class; |
|
146
|
|
|
assert(class_exists($class)); |
|
147
|
|
|
$ins[$annotation->param] = self::WEB_CONTEXT_NAME[$class]; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
return $ins; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* @param array<string, string> $ins |
|
155
|
|
|
* |
|
156
|
|
|
* @return array<string, string> |
|
157
|
|
|
*/ |
|
158
|
|
|
public function getInsFromParameterAttributes(ReflectionMethod $method, array $ins): array|null |
|
159
|
|
|
{ |
|
160
|
|
|
$parameters = $method->getParameters(); |
|
161
|
|
|
foreach ($parameters as $parameter) { |
|
162
|
|
|
$attributes = $parameter->getAttributes(); |
|
163
|
|
|
foreach ($attributes as $attribute) { |
|
164
|
|
|
$instance = $attribute->newInstance(); |
|
165
|
|
|
if (! ($instance instanceof AbstractWebContextParam)) { |
|
166
|
|
|
continue; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
$class = $instance::class; |
|
170
|
|
|
assert(class_exists($class)); |
|
171
|
|
|
$ins[$parameter->name] = self::WEB_CONTEXT_NAME[$class]; |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
return $ins; |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
|