1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Component\Http; |
4
|
|
|
|
5
|
|
|
use App\Component\Http\Interfaces\IRequest; |
6
|
|
|
use App\Component\Http\Interfaces\IHeaders; |
7
|
|
|
use App\Component\Http\Session; |
8
|
|
|
|
9
|
|
|
class Request extends Session implements IRequest |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
protected $server; |
13
|
|
|
protected $method; |
14
|
|
|
protected $contentType; |
15
|
|
|
protected $isCli; |
16
|
|
|
protected $params; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* headers list |
20
|
|
|
* |
21
|
|
|
* @var Headers |
22
|
|
|
*/ |
23
|
|
|
protected $headerManager; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* instanciate |
27
|
|
|
*/ |
28
|
41 |
|
public function __construct() |
29
|
|
|
{ |
30
|
41 |
|
$sapiName = php_sapi_name(); |
31
|
41 |
|
$this->setIsCli( |
32
|
41 |
|
$sapiName == self::_CLI |
33
|
41 |
|
|| $sapiName == self::_CLID |
34
|
|
|
); |
35
|
41 |
|
$this->headerManager = new Headers(); |
36
|
41 |
|
$this->server = $_SERVER; |
37
|
41 |
|
$this->method = $this->getMethod(); |
38
|
41 |
|
$this->setContentType(self::APPLICATION_JSON); |
39
|
41 |
|
$this->setParams(); |
40
|
41 |
|
parent::__construct(); |
41
|
41 |
|
$this->setHeaders(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* returns header manager |
46
|
|
|
* |
47
|
|
|
* @return Headers |
48
|
|
|
*/ |
49
|
1 |
|
public function getHeaderManager(): Headers |
50
|
|
|
{ |
51
|
1 |
|
return $this->headerManager; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* returns http method |
56
|
|
|
* |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
41 |
|
public function getMethod(): string |
60
|
|
|
{ |
61
|
41 |
|
return ($this->isCli) |
62
|
41 |
|
? self::METHOD_TRACE |
63
|
41 |
|
: $this->getServer(self::REQUEST_METHOD); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* returns http param for a given key |
68
|
|
|
* |
69
|
|
|
* @return array |
70
|
|
|
*/ |
71
|
3 |
|
public function getParams(): array |
72
|
|
|
{ |
73
|
3 |
|
return $this->params; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* returns http param for a given key |
78
|
|
|
* |
79
|
|
|
* @return array |
80
|
|
|
*/ |
81
|
1 |
|
public function getParam(string $key): string |
82
|
|
|
{ |
83
|
1 |
|
return isset($this->params[$key]) |
84
|
1 |
|
? $this->params[$key] |
85
|
1 |
|
: ''; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* returns active route |
90
|
|
|
* |
91
|
|
|
* @return string |
92
|
|
|
*/ |
93
|
1 |
|
public function getRoute(): string |
94
|
|
|
{ |
95
|
1 |
|
return $this->getServer(self::SCRIPT_URL); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* return php script filename |
100
|
|
|
* |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
3 |
|
public function getFilename(): string |
104
|
|
|
{ |
105
|
3 |
|
return $this->getServer(self::SCRIPT_FILENAME); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* return request uri |
110
|
|
|
* |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
1 |
|
public function getUri(): string |
114
|
|
|
{ |
115
|
1 |
|
return ($this->isCli()) |
116
|
1 |
|
? $this->getArgs() |
117
|
1 |
|
: $this->getServer(self::REQUEST_URI); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* return request host |
122
|
|
|
* |
123
|
|
|
* @return string |
124
|
|
|
*/ |
125
|
1 |
|
public function getHost(): string |
126
|
|
|
{ |
127
|
1 |
|
return $this->getServer(self::HTTP_HOST); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* return request client ip |
132
|
|
|
* |
133
|
|
|
* @return string |
134
|
|
|
*/ |
135
|
1 |
|
public function getIp(): string |
136
|
|
|
{ |
137
|
1 |
|
return $this->getServer(self::REMOTE_ADDR); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* return request content type |
142
|
|
|
* |
143
|
|
|
* @return string |
144
|
|
|
*/ |
145
|
1 |
|
public function getContentType(): string |
146
|
|
|
{ |
147
|
1 |
|
return $this->contentType; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* return request accept encoding |
152
|
|
|
* |
153
|
|
|
* @return string |
154
|
|
|
*/ |
155
|
1 |
|
public function getAcceptEncoding(): string |
156
|
|
|
{ |
157
|
1 |
|
$headers = $this->getHeaders(); |
158
|
1 |
|
return isset($headers[IHeaders::ACCEPT_ENCODING]) |
159
|
|
|
? $headers[IHeaders::ACCEPT_ENCODING] |
160
|
1 |
|
: ''; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* return request headers |
165
|
|
|
* |
166
|
|
|
* @return array |
167
|
|
|
*/ |
168
|
1 |
|
public function getHeaders(): array |
169
|
|
|
{ |
170
|
1 |
|
return $this->headerManager->get(); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* return request headers |
175
|
|
|
* |
176
|
|
|
* @return Request |
177
|
|
|
*/ |
178
|
1 |
|
protected function setHeaders(): Request |
179
|
|
|
{ |
180
|
1 |
|
$this->headerManager->addMany( |
181
|
1 |
|
$this->isCli ? [] : getallheaders() |
|
|
|
|
182
|
|
|
); |
183
|
1 |
|
return $this; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* build uri from cli args |
188
|
|
|
* |
189
|
|
|
* @return string |
190
|
|
|
*/ |
191
|
1 |
|
protected function getArgs(): string |
192
|
|
|
{ |
193
|
1 |
|
return isset($this->server[self::_ARGV][1]) |
194
|
1 |
|
? $this->server[self::_ARGV][1] |
195
|
1 |
|
: ''; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* return server value for a given key |
200
|
|
|
* |
201
|
|
|
* @param string $key |
202
|
|
|
* @return string |
203
|
|
|
*/ |
204
|
1 |
|
protected function getServer(string $key): string |
205
|
|
|
{ |
206
|
1 |
|
return (isset($this->server[$key])) |
207
|
1 |
|
? $this->server[$key] |
208
|
1 |
|
: ''; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* isJsonAppContentType |
213
|
|
|
* |
214
|
|
|
* @return bool |
215
|
|
|
*/ |
216
|
1 |
|
protected function isJsonContentType(): bool |
217
|
|
|
{ |
218
|
1 |
|
return strpos( |
219
|
1 |
|
strtolower($this->contentType), |
220
|
1 |
|
self::APPLICATION_JSON |
221
|
1 |
|
) !== false; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* getInput |
226
|
|
|
* |
227
|
|
|
* @return array |
228
|
|
|
*/ |
229
|
2 |
|
protected function getInput(): array |
230
|
|
|
{ |
231
|
2 |
|
$input = []; |
232
|
2 |
|
$inputContent = file_get_contents('php://input'); |
233
|
2 |
|
if ($this->isJsonContentType()) { |
234
|
2 |
|
$input = json_decode($inputContent, true); |
235
|
2 |
|
if (json_last_error() !== 0) { |
236
|
2 |
|
$input = []; |
237
|
|
|
} |
238
|
|
|
} else { |
239
|
1 |
|
parse_str($inputContent, $input); |
240
|
|
|
} |
241
|
2 |
|
return $input; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* set method |
246
|
|
|
* essentially for testing purposes |
247
|
|
|
* |
248
|
|
|
* @param string $method |
249
|
|
|
* @return Request |
250
|
|
|
*/ |
251
|
2 |
|
protected function setMethod(string $method): Request |
252
|
|
|
{ |
253
|
2 |
|
$this->method = $method; |
254
|
2 |
|
return $this; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* set true if we are running from cli |
259
|
|
|
* essentially for testing purposes |
260
|
|
|
* |
261
|
|
|
* @param boolean $isCli |
262
|
|
|
* @return Request |
263
|
|
|
*/ |
264
|
1 |
|
protected function setIsCli(bool $isCli): Request |
265
|
|
|
{ |
266
|
1 |
|
$this->isCli = $isCli; |
267
|
1 |
|
if (false === $this->isCli()) { |
268
|
1 |
|
$this->startSession($this->getFilename()); |
269
|
|
|
} |
270
|
1 |
|
return $this; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* return true id sapi mode |
275
|
|
|
* essentially for testing purposes |
276
|
|
|
* |
277
|
|
|
* @return boolean |
278
|
|
|
*/ |
279
|
41 |
|
public function isCli(): bool |
280
|
|
|
{ |
281
|
41 |
|
return $this->isCli; |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* set content type |
286
|
|
|
* |
287
|
|
|
* @param string $contentType |
288
|
|
|
* @return Request |
289
|
|
|
*/ |
290
|
2 |
|
protected function setContentType(string $contentType = ''): Request |
291
|
|
|
{ |
292
|
2 |
|
$this->contentType = (empty($contentType)) |
293
|
1 |
|
? $this->getServer(IHeaders::CONTENT_TYPE) |
294
|
2 |
|
: $contentType; |
295
|
2 |
|
return $this; |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* get params in cli mode |
300
|
|
|
* |
301
|
|
|
* @return array |
302
|
|
|
*/ |
303
|
3 |
|
protected function getCliParams(): array |
304
|
|
|
{ |
305
|
3 |
|
$params = $this->getInput(); |
306
|
3 |
|
if ($this->isCli) { |
307
|
3 |
|
$queryString = parse_url($this->getArgs(), PHP_URL_QUERY); |
308
|
3 |
|
parse_str($queryString, $queryParams); |
309
|
3 |
|
$params = array_merge($params, $queryParams); |
310
|
|
|
} |
311
|
3 |
|
return $params; |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* set an entry in params for key value |
316
|
|
|
* |
317
|
|
|
* @param string $key |
318
|
|
|
* @param string $value |
319
|
|
|
* @return Request |
320
|
|
|
*/ |
321
|
1 |
|
protected function setParam(string $key, string $value): Request |
322
|
|
|
{ |
323
|
1 |
|
$this->params[$key] = $value; |
324
|
1 |
|
return $this; |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
/** |
328
|
|
|
* set http params |
329
|
|
|
* |
330
|
|
|
* @param array $params |
331
|
|
|
* @return Request |
332
|
|
|
*/ |
333
|
1 |
|
protected function setParams(array $params = []): Request |
334
|
|
|
{ |
335
|
1 |
|
if (!empty($params)) { |
336
|
1 |
|
$this->params = $params; |
337
|
1 |
|
return $this; |
338
|
|
|
} |
339
|
1 |
|
if ($this->method === self::METHOD_GET) { |
340
|
1 |
|
$this->params = $_GET; |
341
|
1 |
|
} elseif ($this->method === self::METHOD_POST) { |
342
|
1 |
|
$this->params = ($this->isJsonContentType()) |
343
|
1 |
|
? $this->getInput() |
344
|
1 |
|
: $_POST; |
345
|
1 |
|
} elseif ($this->method === self::METHOD_TRACE) { |
346
|
1 |
|
$this->params = $this->getCliParams(); |
347
|
|
|
} else { |
348
|
1 |
|
$this->params = $this->getInput(); |
349
|
|
|
} |
350
|
1 |
|
return $this; |
351
|
|
|
} |
352
|
|
|
} |
353
|
|
|
|