forked from byte-it/openapi-spec-generator
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathRoute.php
255 lines (207 loc) · 6.07 KB
/
Route.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<?php
namespace LaravelJsonApi\OpenApiSpec;
use Illuminate\Routing\Route as IlluminateRoute;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Str;
use LaravelJsonApi\Contracts\Schema\PolymorphicRelation;
use LaravelJsonApi\Contracts\Schema\Schema;
use LaravelJsonApi\Contracts\Server\Server;
use LaravelJsonApi\Eloquent\Fields\Relations\Relation;
class Route
{
protected Server $server;
protected Schema $schema;
protected IlluminateRoute $route;
protected string $resource;
/**
* @var string The controller class FQN
*/
protected string $controller;
/**
* @var string The method name on the controller
*/
protected string $method;
/**
* The last part of the route name. For the 'showRelated' method, the name
* is manually added.
*/
protected string $action;
/**
* @var string The route name without the prefix
*/
protected string $operationId;
protected string $uri;
protected ?string $relation = null;
/**
* Route constructor.
*/
public function __construct(Server $server, IlluminateRoute $route)
{
$this->server = $server;
$this->route = $route;
$segments = explode('.', $this->route->getName());
$segments = array_slice(
$segments,
array_search($this->server->name(), $segments) + 1
);
$this->operationId = collect($segments)->join('.');
$relation = null;
if (count($segments) === 2) {
[$resource, $action] = $segments;
} elseif (count($segments) === 3) {
[$resource, $relation, $action] = $segments;
} else {
throw new \LogicException('Unable to handle action structure '.$route->getName());
}
$this->resource = $resource;
$this->schema = $this->server->schemas()->schemaFor($resource);
if ($action !== null && $relation === null && $this->schema->isRelationship($action)) {
$this->relation = $action;
$this->action = 'showRelated';
} else {
$this->relation = $relation;
$this->action = $action;
}
$this->setUriForRoute();
[$controller, $method] = explode('@', $this->route->getActionName(), 2);
$this->controller = $controller;
$this->method = $method;
}
/**
* @return string The HTTP method
*/
public function method(): string
{
return collect($this->route->methods())
->filter(fn ($method) => $method !== 'HEAD')
->first();
}
public function schema(): Schema
{
return $this->schema;
}
public function route(): IlluminateRoute
{
return $this->route;
}
/**
* @return string[]
*/
public function controllerCallable(): array
{
return [$this->controller, $this->method];
}
public function id(): string
{
return $this->operationId;
}
public function uri(): string
{
return $this->uri;
}
public function relationName(): ?string
{
return $this->relation;
}
public function relation(): ?Relation
{
$relation = $this->relation ? $this->schema()
->relationship($this->relation) : null;
if ($relation !== null && ! ($relation instanceof Relation)) {
throw new \RuntimeException('Unexpected Type');
}
return $relation;
}
public function isRelation(): bool
{
return $this->relation !== null;
}
public function isPolymorphic(): bool
{
return $this->relation() instanceof PolymorphicRelation;
}
public function invers(): ?string
{
return $this->relation() !== null ? $this->relation()->inverse() : null;
}
public function inversSchema(): ?Schema
{
if ($this->isRelation()) {
if ($this->relation() instanceof PolymorphicRelation) {
throw new \LogicException('Method is not allowed for Polymorphic relationships');
}
return $this->server->schemas()
->schemaFor($this->relation() !== null ? $this->relation()->inverse() : null);
}
return null;
}
/**
* @return \LaravelJsonApi\Contracts\Schema\Schema[]
*/
public function inversSchemas(): array
{
$schemas = [];
if ($this->isRelation()) {
$relation = $this->relation();
if ($relation instanceof PolymorphicRelation) {
foreach ($relation->inverseTypes() as $type) {
$schemas[$type] = $this->server->schemas()
->schemaFor($type);
}
} else {
$schemas[$relation->inverse()] = $this->server->schemas()
->schemaFor($relation->inverse());
}
}
return $schemas;
}
public function inverseName(bool $singular = false): ?string
{
$relation = $this->relation() !== null ? $this->relation()->inverse() : null;
if ($singular) {
return Str::singular($relation);
}
return $relation;
}
/**
* @param false $singular
*/
public function name(bool $singular = false): string
{
if ($singular) {
return Str::singular($this->resource);
}
return $this->resource;
}
public function resource(): string
{
return $this->resource;
}
public function action(): string
{
return $this->action;
}
public static function belongsTo(
IlluminateRoute $route,
Server $server,
): bool {
return Str::contains(
$route->getName(),
$server->name(),
);
}
protected function setUriForRoute(): void
{
$domain = URL::to('/');
$serverBasePath = str_replace(
$domain,
'',
$this->server->url(),
);
$this->uri = str_replace(
$serverBasePath,
'',
'/'.$this->route->uri(),
);
}
}