1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Napp\Core\Api\Router; |
4
|
|
|
|
5
|
|
|
use Illuminate\Foundation\Application; |
6
|
|
|
use Illuminate\Http\Request; |
7
|
|
|
use Illuminate\Routing\Router as LaravelRouter; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class Router. |
11
|
|
|
*/ |
12
|
|
|
class Router |
13
|
|
|
{ |
14
|
|
|
private $app; |
15
|
|
|
|
16
|
|
|
private $router; |
17
|
|
|
|
18
|
|
|
private $request; |
19
|
|
|
|
20
|
|
|
private $disableMiddleware = false; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param \Illuminate\Foundation\Application $app |
24
|
|
|
* @param \Illuminate\Http\Request $request, |
25
|
|
|
* @param \Illuminate\Routing\Router $router |
26
|
|
|
*/ |
27
|
|
|
public function __construct(Application $app, Request $request, LaravelRouter $router) |
28
|
|
|
{ |
29
|
|
|
$this->app = $app; |
30
|
|
|
$this->request = $request; |
31
|
|
|
$this->router = $router; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param string $uri |
36
|
|
|
* @param array $data |
37
|
|
|
* @param array $headers |
38
|
|
|
* @param string $content |
39
|
|
|
* |
40
|
|
|
* @return \Illuminate\Http\Response |
41
|
|
|
*/ |
42
|
|
|
public function get() |
43
|
|
|
{ |
44
|
|
|
return $this->quickCall('GET', func_get_args()); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param string $uri |
49
|
|
|
* @param array $data |
50
|
|
|
* @param array $headers |
51
|
|
|
* @param string $content |
52
|
|
|
* |
53
|
|
|
* @return \Illuminate\Http\Response |
54
|
|
|
*/ |
55
|
|
|
public function post() |
56
|
|
|
{ |
57
|
|
|
return $this->quickCall('POST', func_get_args()); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param string $uri |
62
|
|
|
* @param array $data |
63
|
|
|
* @param array $headers |
64
|
|
|
* @param string $content |
65
|
|
|
* |
66
|
|
|
* @return \Illuminate\Http\Response |
67
|
|
|
*/ |
68
|
|
|
public function put() |
69
|
|
|
{ |
70
|
|
|
return $this->quickCall('PUT', func_get_args()); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param string $uri |
75
|
|
|
* @param array $data |
76
|
|
|
* @param array $headers |
77
|
|
|
* @param string $content |
78
|
|
|
* |
79
|
|
|
* @return \Illuminate\Http\Response |
80
|
|
|
*/ |
81
|
|
|
public function delete() |
82
|
|
|
{ |
83
|
|
|
return $this->quickCall('DELETE', func_get_args()); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param array $requests An array of requests |
88
|
|
|
* |
89
|
|
|
* @return array |
90
|
|
|
*/ |
91
|
|
|
public function batchRequest(array $requests) |
92
|
|
|
{ |
93
|
|
|
foreach ($requests as $i => $request) { |
94
|
|
|
$requests[$i] = call_user_func_array([$this, 'singleRequest'], $request); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $requests; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param string $method |
102
|
|
|
* @param array $args |
103
|
|
|
* |
104
|
|
|
* @return \Illuminate\Http\Response |
105
|
|
|
*/ |
106
|
|
|
public function quickCall($method, array $args) |
107
|
|
|
{ |
108
|
|
|
array_unshift($args, $method); |
109
|
|
|
|
110
|
|
|
return call_user_func_array([$this, 'singleRequest'], $args); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param string $method |
115
|
|
|
* @param string $uri |
116
|
|
|
* @param array $data |
117
|
|
|
* @param array $headers |
118
|
|
|
* @param string $content |
119
|
|
|
* |
120
|
|
|
* @throws \Exception |
121
|
|
|
* |
122
|
|
|
* @return \Illuminate\Http\Response |
123
|
|
|
*/ |
124
|
|
|
public function singleRequest($method, $uri, array $data = [], array $headers = [], $content = null) |
125
|
|
|
{ |
126
|
|
|
// Save the current request so we can reset the router back to it |
127
|
|
|
// after we've completed our internal request. |
128
|
|
|
$currentRequest = $this->request->instance()->duplicate(); |
129
|
|
|
|
130
|
|
|
$headers = $this->overrideHeaders($currentRequest->server->getHeaders(), $headers); |
131
|
|
|
|
132
|
|
|
if ($this->disableMiddleware) { |
133
|
|
|
$this->app->instance('middleware.disable', true); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
$response = $this->request($method, $uri, $data, $headers, $content); |
137
|
|
|
|
138
|
|
|
if ($this->disableMiddleware) { |
139
|
|
|
$this->app->instance('middleware.disable', false); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
// Once the request has completed we reset the currentRequest of the router |
143
|
|
|
// to match the original request. |
144
|
|
|
$this->app->instance('request', $currentRequest); |
145
|
|
|
|
146
|
|
|
return $response; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param array $default |
151
|
|
|
* @param array $headers |
152
|
|
|
* |
153
|
|
|
* @return array |
154
|
|
|
*/ |
155
|
|
|
private function overrideHeaders(array $default, array $headers) |
156
|
|
|
{ |
157
|
|
|
$headers = $this->transformHeadersToUppercaseUnderscoreType($headers); |
158
|
|
|
|
159
|
|
|
return array_merge($default, $headers); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function enableMiddleware() |
163
|
|
|
{ |
164
|
|
|
$this->disableMiddleware = false; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function disableMiddleware() |
168
|
|
|
{ |
169
|
|
|
$this->disableMiddleware = true; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @param string $method |
174
|
|
|
* @param string $uri |
175
|
|
|
* @param array $data |
176
|
|
|
* @param array $headers |
177
|
|
|
* @param string $content |
178
|
|
|
* |
179
|
|
|
* @throws \Exception |
180
|
|
|
* |
181
|
|
|
* @return \Illuminate\Http\Response |
182
|
|
|
*/ |
183
|
|
|
private function request($method, $uri, array $data = [], array $headers = [], $content = null) |
184
|
|
|
{ |
185
|
|
|
// Create a new request object for the internal request |
186
|
|
|
$request = $this->createRequest($method, $uri, $data, $headers, $content); |
187
|
|
|
|
188
|
|
|
// Handle the request in the kernel and prepare a response |
189
|
|
|
$response = $this->router->prepareResponse($request, $this->app->handle($request)); |
190
|
|
|
|
191
|
|
|
return $response; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @param string $method |
196
|
|
|
* @param string $uri |
197
|
|
|
* @param array $data |
198
|
|
|
* @param array $headers |
199
|
|
|
* @param string $content |
200
|
|
|
* |
201
|
|
|
* @return \Illuminate\Http\Request |
202
|
|
|
*/ |
203
|
|
|
private function createRequest($method, $uri, array $data = [], array $headers = [], $content = null) |
204
|
|
|
{ |
205
|
|
|
$server = $this->transformHeadersToServerVariables($headers); |
206
|
|
|
|
207
|
|
|
return $this->request->create($uri, $method, $data, [], [], $server, $content); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @param $headers |
212
|
|
|
* |
213
|
|
|
* @return array |
214
|
|
|
*/ |
215
|
|
|
private function transformHeadersToUppercaseUnderscoreType($headers) |
216
|
|
|
{ |
217
|
|
|
$transformed = []; |
218
|
|
|
|
219
|
|
|
foreach ($headers as $headerType => $headerValue) { |
220
|
|
|
$headerType = strtoupper(str_replace('-', '_', $headerType)); |
221
|
|
|
|
222
|
|
|
$transformed[$headerType] = $headerValue; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
return $transformed; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* https://github.com/symfony/symfony/issues/5074. |
230
|
|
|
* |
231
|
|
|
* @param array $headers |
232
|
|
|
* |
233
|
|
|
* @return array |
234
|
|
|
*/ |
235
|
|
|
private function transformHeadersToServerVariables($headers) |
236
|
|
|
{ |
237
|
|
|
$server = []; |
238
|
|
|
|
239
|
|
|
foreach ($headers as $headerType => $headerValue) { |
240
|
|
|
$headerType = 'HTTP_' . $headerType; |
241
|
|
|
|
242
|
|
|
$server[$headerType] = $headerValue; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
return $server; |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
|