1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mosaic\Http\Adapters\Psr7; |
4
|
|
|
|
5
|
|
|
use Mosaic\Common\Arr; |
6
|
|
|
use Mosaic\Http\Adapters\Psr7\Response as ResponseAdapter; |
7
|
|
|
use Mosaic\Http\Request as RequestContract; |
8
|
|
|
use Mosaic\Http\Response; |
|
|
|
|
9
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
10
|
|
|
use Psr\Http\Message\UriInterface; |
11
|
|
|
use Zend\Diactoros\Response as DiactorosResponse; |
12
|
|
|
|
13
|
|
|
class Request extends Message implements RequestContract, ServerRequestInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var ServerRequestInterface |
17
|
|
|
*/ |
18
|
|
|
protected $wrapped; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param ServerRequestInterface $wrapper |
22
|
|
|
*/ |
23
|
82 |
|
public function __construct(ServerRequestInterface $wrapper) |
24
|
|
|
{ |
25
|
82 |
|
parent::__construct($wrapper); |
26
|
82 |
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* {@inheritdoc} |
30
|
|
|
*/ |
31
|
4 |
|
public function header(string $key = null, $default = null) |
32
|
|
|
{ |
33
|
4 |
|
return Arr::unwrap($this->wrapped->getHeader($key), $default); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritdoc} |
38
|
|
|
*/ |
39
|
27 |
|
public function method() : string |
40
|
|
|
{ |
41
|
27 |
|
return $this->wrapped->getMethod(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
19 |
|
public function get(string $key, $default = null) |
48
|
|
|
{ |
49
|
19 |
|
return Arr::get($this->all(), $key, $default); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
26 |
|
public function all() |
56
|
|
|
{ |
57
|
26 |
|
if ($this->isMethod('GET')) { |
58
|
20 |
|
return $this->getQueryParams(); |
59
|
|
|
} |
60
|
|
|
|
61
|
6 |
|
return array_merge($this->getParsedBody(), $this->getQueryParams()); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritdoc} |
66
|
|
|
*/ |
67
|
5 |
|
public function only($keys) |
68
|
|
|
{ |
69
|
5 |
|
$params = []; |
70
|
|
|
|
71
|
5 |
|
foreach ((array) $keys as $key) { |
72
|
5 |
|
$params[$key] = $this->get($key); |
73
|
|
|
} |
74
|
|
|
|
75
|
5 |
|
return $params; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritdoc} |
80
|
|
|
*/ |
81
|
2 |
|
public function except($keys) |
82
|
|
|
{ |
83
|
2 |
|
$allKeys = array_keys($this->all()); |
84
|
|
|
|
85
|
2 |
|
return $this->only(array_diff($allKeys, (array) $keys)); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* {@inheritdoc} |
90
|
|
|
*/ |
91
|
4 |
|
public function exists($key) |
92
|
|
|
{ |
93
|
|
|
return array_reduce((array) $key, function ($carry, $item) { |
94
|
4 |
|
return $carry && array_key_exists($item, $this->all()); |
95
|
4 |
|
}, true); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* {@inheritdoc} |
100
|
|
|
*/ |
101
|
7 |
|
public function has($key) |
102
|
|
|
{ |
103
|
|
|
return array_reduce((array) $key, function ($carry, $item) { |
104
|
7 |
|
return $carry && !$this->isEmptyString($item); |
105
|
7 |
|
}, true); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* {@inheritdoc} |
110
|
|
|
*/ |
111
|
4 |
|
public function server(string $key = null, $default = null) |
112
|
|
|
{ |
113
|
4 |
|
if ($key === null) { |
114
|
1 |
|
return $this->wrapped->getServerParams(); |
115
|
|
|
} |
116
|
|
|
|
117
|
3 |
|
return Arr::get($this->wrapped->getServerParams(), $key, $default); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* {@inheritdoc} |
122
|
|
|
*/ |
123
|
9 |
|
public function segments() |
124
|
|
|
{ |
125
|
9 |
|
$segments = explode('/', trim($this->getUri()->getPath(), '/')); |
126
|
|
|
|
127
|
9 |
|
return array_values(array_filter($segments, function ($segment) { |
128
|
9 |
|
return trim($segment) !== ''; |
129
|
9 |
|
})); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* {@inheritdoc} |
134
|
|
|
*/ |
135
|
3 |
|
public function segment($index, $default = null) |
136
|
|
|
{ |
137
|
3 |
|
return Arr::get($this->segments(), $index, $default); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* {@inheritdoc} |
142
|
|
|
*/ |
143
|
3 |
|
public function file(string $key = null, $default = null) |
144
|
|
|
{ |
145
|
3 |
|
return Arr::get($this->getUploadedFiles(), $key, $default); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* {@inheritdoc} |
150
|
|
|
*/ |
151
|
1 |
|
public function hasFile(string $key) |
152
|
|
|
{ |
153
|
1 |
|
return array_key_exists($key, $this->wrapped->getUploadedFiles()); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* {@inheritdoc} |
158
|
|
|
*/ |
159
|
1 |
|
public function cookies() |
160
|
|
|
{ |
161
|
1 |
|
return $this->wrapped->getCookieParams(); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* {@inheritdoc} |
166
|
|
|
*/ |
167
|
2 |
|
public function cookie(string $key = null, $default = null) |
168
|
|
|
{ |
169
|
2 |
|
return Arr::get($this->getCookieParams(), $key, $default); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* {@inheritdoc} |
174
|
|
|
*/ |
175
|
1 |
|
public function getRequestTarget() |
176
|
|
|
{ |
177
|
1 |
|
return $this->wrapped->getRequestTarget(); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* {@inheritdoc} |
182
|
|
|
*/ |
183
|
1 |
|
public function withRequestTarget($requestTarget) |
184
|
|
|
{ |
185
|
1 |
|
return new static($this->wrapped->withRequestTarget($requestTarget)); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* {@inheritdoc} |
190
|
|
|
*/ |
191
|
1 |
|
public function getMethod() |
192
|
|
|
{ |
193
|
1 |
|
return $this->wrapped->getMethod(); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* {@inheritdoc} |
198
|
|
|
*/ |
199
|
1 |
|
public function withMethod($method) |
200
|
|
|
{ |
201
|
1 |
|
return new static($this->wrapped->withMethod($method)); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* {@inheritdoc} |
206
|
|
|
*/ |
207
|
9 |
|
public function getUri() |
208
|
|
|
{ |
209
|
9 |
|
return $this->wrapped->getUri(); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* {@inheritdoc} |
214
|
|
|
*/ |
215
|
1 |
|
public function withUri(UriInterface $uri, $preserveHost = false) |
216
|
|
|
{ |
217
|
1 |
|
return new static($this->wrapped->withUri($uri, $preserveHost)); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* {@inheritdoc} |
222
|
|
|
*/ |
223
|
1 |
|
public function getServerParams() |
224
|
|
|
{ |
225
|
1 |
|
return $this->wrapped->getServerParams(); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* {@inheritdoc} |
230
|
|
|
*/ |
231
|
3 |
|
public function getCookieParams() |
232
|
|
|
{ |
233
|
3 |
|
return $this->wrapped->getCookieParams(); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* {@inheritdoc} |
238
|
|
|
*/ |
239
|
1 |
|
public function withCookieParams(array $cookies) |
240
|
|
|
{ |
241
|
1 |
|
return new static($this->wrapped->withCookieParams($cookies)); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* {@inheritdoc} |
246
|
|
|
*/ |
247
|
26 |
|
public function getQueryParams() |
248
|
|
|
{ |
249
|
26 |
|
return $this->wrapped->getQueryParams(); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* {@inheritdoc} |
254
|
|
|
*/ |
255
|
1 |
|
public function withQueryParams(array $query) |
256
|
|
|
{ |
257
|
1 |
|
return new static($this->wrapped->withQueryParams($query)); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* {@inheritdoc} |
262
|
|
|
*/ |
263
|
3 |
|
public function getUploadedFiles() |
264
|
|
|
{ |
265
|
3 |
|
return $this->wrapped->getUploadedFiles(); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* {@inheritdoc} |
270
|
|
|
*/ |
271
|
1 |
|
public function withUploadedFiles(array $uploadedFiles) |
272
|
|
|
{ |
273
|
1 |
|
return new static($this->wrapped->withUploadedFiles($uploadedFiles)); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* {@inheritdoc} |
278
|
|
|
*/ |
279
|
6 |
|
public function getParsedBody() |
280
|
|
|
{ |
281
|
6 |
|
return $this->wrapped->getParsedBody(); |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* {@inheritdoc} |
286
|
|
|
*/ |
287
|
1 |
|
public function withParsedBody($data) |
288
|
|
|
{ |
289
|
1 |
|
return new static($this->wrapped->withParsedBody($data)); |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* {@inheritdoc} |
294
|
|
|
*/ |
295
|
1 |
|
public function getAttributes() |
296
|
|
|
{ |
297
|
1 |
|
return $this->wrapped->getAttributes(); |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* {@inheritdoc} |
302
|
|
|
*/ |
303
|
1 |
|
public function getAttribute($name, $default = null) |
304
|
|
|
{ |
305
|
1 |
|
return $this->wrapped->getAttribute($name, $default); |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* {@inheritdoc} |
310
|
|
|
*/ |
311
|
1 |
|
public function withAttribute($name, $value) |
312
|
|
|
{ |
313
|
1 |
|
return new static($this->wrapped->withAttribute($name, $value)); |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* {@inheritdoc} |
318
|
|
|
*/ |
319
|
1 |
|
public function withoutAttribute($name) |
320
|
|
|
{ |
321
|
1 |
|
return new static($this->wrapped->withoutAttribute($name)); |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* @param string $method |
326
|
|
|
* |
327
|
|
|
* @return bool |
328
|
|
|
*/ |
329
|
26 |
|
private function isMethod(string $method) |
330
|
|
|
{ |
331
|
26 |
|
return strcasecmp($method, $this->method()) === 0; |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
/** |
335
|
|
|
* @param string $key |
336
|
|
|
* |
337
|
|
|
* @return bool |
338
|
|
|
*/ |
339
|
7 |
|
private function isEmptyString(string $key) |
340
|
|
|
{ |
341
|
7 |
|
$item = $this->get($key); |
342
|
|
|
|
343
|
7 |
|
if (is_array($item)) { |
344
|
2 |
|
return empty($item); |
345
|
|
|
} |
346
|
|
|
|
347
|
7 |
|
return trim($item) === ''; |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
/** |
351
|
|
|
* @return string |
352
|
|
|
*/ |
353
|
1 |
|
public function uri() : string |
354
|
|
|
{ |
355
|
1 |
|
return (string) $this->wrapped->getUri(); |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
/** |
359
|
|
|
* @return string |
360
|
|
|
*/ |
361
|
1 |
|
public function path() : string |
362
|
|
|
{ |
363
|
1 |
|
return (string) $this->wrapped->getUri()->getPath(); |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
/** |
367
|
|
|
* @return ServerRequestInterface |
368
|
|
|
*/ |
369
|
1 |
|
public function toPsr7() : ServerRequestInterface |
370
|
|
|
{ |
371
|
1 |
|
return $this; |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
/** |
375
|
|
|
* @return Response |
376
|
|
|
*/ |
377
|
1 |
|
public function prepareResponse() : Response |
378
|
|
|
{ |
379
|
1 |
|
return new ResponseAdapter( |
380
|
1 |
|
new DiactorosResponse() |
381
|
|
|
); |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
/** |
385
|
|
|
* @return ServerRequestInterface |
386
|
|
|
*/ |
387
|
11 |
|
protected function getWrapped() |
388
|
|
|
{ |
389
|
11 |
|
return $this->wrapped; |
390
|
|
|
} |
391
|
|
|
} |
392
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: