-
-
Notifications
You must be signed in to change notification settings - Fork 359
Expand file tree
/
Copy pathExtractedEndpointData.php
More file actions
160 lines (125 loc) · 4.43 KB
/
ExtractedEndpointData.php
File metadata and controls
160 lines (125 loc) · 4.43 KB
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
<?php
namespace Knuckles\Camel\Extraction;
use Illuminate\Routing\Route;
use Knuckles\Camel\BaseDTO;
use Knuckles\Scribe\Extracting\Shared\UrlParamsNormalizer;
use Knuckles\Scribe\Tools\Globals;
use Knuckles\Scribe\Tools\Utils as u;
use ReflectionClass;
class ExtractedEndpointData extends BaseDTO
{
/**
* @var array<string>
*/
public array $httpMethods;
public string $uri;
public Metadata $metadata;
/**
* @var array<string,string>
*/
public array $headers = [];
/**
* @var array<string,\Knuckles\Camel\Extraction\Parameter>
*/
public array $urlParameters = [];
/**
* @var array<string,mixed>
*/
public array $cleanUrlParameters = [];
/**
* @var array<string,\Knuckles\Camel\Extraction\Parameter>
*/
public array $queryParameters = [];
/**
* @var array<string,mixed>
*/
public array $cleanQueryParameters = [];
/**
* @var array<string,\Knuckles\Camel\Extraction\Parameter>
*/
public array $bodyParameters = [];
/**
* @var array<string,mixed>
*/
public array $cleanBodyParameters = [];
/**
* @var array<string,\Illuminate\Http\UploadedFile|array>
*/
public array $fileParameters = [];
public ResponseCollection $responses;
/**
* @var array<string,\Knuckles\Camel\Extraction\ResponseField>
*/
public array $responseFields = [];
public ExampleCollection $examples;
/**
* Authentication info for this endpoint. In the form [{where}, {name}, {sample}]
* Example: ["queryParameters", "api_key", "njiuyiw97865rfyvgfvb1"]
*/
public array $auth = [];
public ?ReflectionClass $controller;
public ?\ReflectionFunctionAbstract $method;
public ?Route $route;
public function __construct(array $parameters = [])
{
$parameters['metadata'] = $parameters['metadata'] ?? new Metadata([]);
$parameters['responses'] = $parameters['responses'] ?? new ResponseCollection([]);
$parameters['examples'] = $parameters['examples'] ?? new ExampleCollection([]);
parent::__construct($parameters);
$defaultNormalizer = fn() => UrlParamsNormalizer::normalizeParameterNamesInRouteUri($this->route, $this->method);
$this->uri = match (is_callable(Globals::$__normalizeEndpointUrlUsing)) {
true => call_user_func_array(Globals::$__normalizeEndpointUrlUsing,
[$this->route->uri, $this->route, $this->method, $this->controller, $defaultNormalizer]),
default => $defaultNormalizer(),
};
}
public static function fromRoute(Route $route, array $extras = []): self
{
$httpMethods = self::getMethods($route);
$uri = $route->uri();
[$controllerName, $methodName] = u::getRouteClassAndMethodNames($route);
$controller = new ReflectionClass($controllerName);
$method = u::getReflectedRouteMethod([$controllerName, $methodName]);
$data = compact('httpMethods', 'uri', 'controller', 'method', 'route');
$data = array_merge($data, $extras);
return new ExtractedEndpointData($data);
}
/**
* @param Route $route
*
* @return array<string>
*/
public static function getMethods(Route $route): array
{
$methods = $route->methods();
// Laravel adds an automatic "HEAD" endpoint for each GET request, so we'll strip that out,
// but not if there's only one method (means it was intentional)
if (count($methods) === 1) {
return $methods;
}
return array_diff($methods, ['HEAD']);
}
public function name()
{
return sprintf("[%s] {$this->route->uri}.", implode(',', $this->route->methods));
}
public function endpointId()
{
return $this->httpMethods[0] . str_replace(['/', '?', '{', '}', ':', '\\', '+', '|'], '-', $this->uri);
}
/**
* Prepare the endpoint data for serialising.
*/
public function forSerialisation()
{
$copy = $this->except(
// Get rid of all duplicate data
'cleanQueryParameters', 'cleanUrlParameters', 'fileParameters', 'cleanBodyParameters',
// and objects used only in extraction
'route', 'controller', 'method', 'auth',
);
// Remove these, since they're on the parent group object
$copy->metadata = $copy->metadata->except('groupName', 'groupDescription');
return $copy;
}
}