1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Borobudur-Http package. |
4
|
|
|
* |
5
|
|
|
* (c) Hexacodelabs <http://hexacodelabs.com> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Borobudur\Http; |
12
|
|
|
|
13
|
|
|
use Borobudur\Http\Session; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @author Iqbal Maulana <[email protected]> |
17
|
|
|
* @created 8/5/15 |
18
|
|
|
*/ |
19
|
|
|
abstract class AbstractRequest |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Server and $_SERVER environment parameters. |
23
|
|
|
* |
24
|
|
|
* @var ServerBag |
25
|
|
|
*/ |
26
|
|
|
protected $server; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* $_POST parameters. |
30
|
|
|
* |
31
|
|
|
* @var ParameterBag |
32
|
|
|
*/ |
33
|
|
|
protected $request; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* $_FILES parameters. |
37
|
|
|
* |
38
|
|
|
* @var UploadFileBag |
39
|
|
|
*/ |
40
|
|
|
protected $files; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* $_GET parameters. |
44
|
|
|
* |
45
|
|
|
* @var ParameterBag |
46
|
|
|
*/ |
47
|
|
|
protected $query; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* $_COOKIE parameters. |
51
|
|
|
* |
52
|
|
|
* @var ParameterBag |
53
|
|
|
*/ |
54
|
|
|
protected $cookies; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Headers from $_SERVER. |
58
|
|
|
* |
59
|
|
|
* @var HeaderBag |
60
|
|
|
*/ |
61
|
|
|
protected $headers; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Request URI. |
65
|
|
|
* |
66
|
|
|
* @var Uri |
67
|
|
|
*/ |
68
|
|
|
protected $uri; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @var Url |
72
|
|
|
*/ |
73
|
|
|
protected $url; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @var SchemaHost |
77
|
|
|
*/ |
78
|
|
|
protected $schemaHost; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @var Session |
82
|
|
|
*/ |
83
|
|
|
protected $session; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @var string |
87
|
|
|
*/ |
88
|
|
|
protected $content; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* List of safe methods (read only). |
92
|
|
|
* |
93
|
|
|
* @var array |
94
|
|
|
*/ |
95
|
|
|
protected static $safeMethods = array( |
96
|
|
|
Request::HTTP_METHOD_GET, |
97
|
|
|
Request::HTTP_METHOD_HEAD, |
98
|
|
|
); |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Get server bag. |
102
|
|
|
* |
103
|
|
|
* @return ServerBag |
104
|
|
|
*/ |
105
|
|
|
public function getServerBag() |
106
|
|
|
{ |
107
|
|
|
return $this->server; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Get header bag. |
112
|
|
|
* |
113
|
|
|
* @return HeaderBag |
114
|
|
|
*/ |
115
|
|
|
public function getHeaderBag() |
116
|
|
|
{ |
117
|
|
|
return $this->headers; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Get request parameter bag. |
122
|
|
|
* |
123
|
|
|
* @return ParameterBag |
124
|
|
|
*/ |
125
|
|
|
public function getParameterBag() |
126
|
|
|
{ |
127
|
|
|
return $this->request; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Get query bag. |
132
|
|
|
* |
133
|
|
|
* @return ParameterBag |
134
|
|
|
*/ |
135
|
|
|
public function getQueryBag() |
136
|
|
|
{ |
137
|
|
|
return $this->query; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Get session. |
142
|
|
|
* |
143
|
|
|
* @return Session |
144
|
|
|
*/ |
145
|
|
|
public function getSession() |
146
|
|
|
{ |
147
|
|
|
return $this->session; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Get cookie bag. |
152
|
|
|
* |
153
|
|
|
* @return ParameterBag |
154
|
|
|
*/ |
155
|
|
|
public function getCookieBag() |
156
|
|
|
{ |
157
|
|
|
return $this->cookies; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Get file bag. |
162
|
|
|
* |
163
|
|
|
* @return UploadFileBag |
164
|
|
|
*/ |
165
|
|
|
public function getFileBag() |
166
|
|
|
{ |
167
|
|
|
return $this->files; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Get schema and host. |
172
|
|
|
* |
173
|
|
|
* @return SchemaHost |
174
|
|
|
*/ |
175
|
|
|
public function getSchemaHost() |
176
|
|
|
{ |
177
|
|
|
return $this->schemaHost; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Get uri component. |
182
|
|
|
* |
183
|
|
|
* @return Uri |
184
|
|
|
*/ |
185
|
|
|
public function getUri() |
186
|
|
|
{ |
187
|
|
|
return $this->uri; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Get url. |
192
|
|
|
* |
193
|
|
|
* @return Url |
194
|
|
|
*/ |
195
|
|
|
public function getUrl() |
196
|
|
|
{ |
197
|
|
|
return $this->url; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Get HTTP method. |
202
|
|
|
* |
203
|
|
|
* Return GET method as default. |
204
|
|
|
* |
205
|
|
|
* @return string |
206
|
|
|
*/ |
207
|
|
|
public function getMethod() |
208
|
|
|
{ |
209
|
|
|
return $this->server->get('REQUEST_METHOD', Request::HTTP_METHOD_GET); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Set HTTP method. |
214
|
|
|
* |
215
|
|
|
* @param string $method |
216
|
|
|
* |
217
|
|
|
* @return $this |
218
|
|
|
*/ |
219
|
|
|
public function setMethod($method) |
220
|
|
|
{ |
221
|
|
|
$this->server->set('REQUEST_METHOD', strtoupper($method)); |
222
|
|
|
|
223
|
|
|
return $this; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Check if request is Xml Http Request or not. |
228
|
|
|
* |
229
|
|
|
* @return bool |
230
|
|
|
*/ |
231
|
|
|
public function isXmlHttpRequest() |
232
|
|
|
{ |
233
|
|
|
return 'xmlhttprequest' === strtolower($this->server->get('HTTP_X_REQUESTED_WITH')); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Get request body content. |
238
|
|
|
* |
239
|
|
|
* @return resource|string |
240
|
|
|
*/ |
241
|
|
|
public function getContent() |
242
|
|
|
{ |
243
|
|
|
if (null === $this->content) { |
244
|
|
|
$this->content = file_get_contents('php://input'); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
return $this->content; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Set request content. |
252
|
|
|
* |
253
|
|
|
* @param string $content |
254
|
|
|
* |
255
|
|
|
* @return $this |
256
|
|
|
*/ |
257
|
|
|
public function setContent($content) |
258
|
|
|
{ |
259
|
|
|
$this->content = $content; |
260
|
|
|
|
261
|
|
|
return $this; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* Check if request method is of specified type. |
266
|
|
|
* |
267
|
|
|
* @param string $method |
268
|
|
|
* |
269
|
|
|
* @return bool |
270
|
|
|
*/ |
271
|
|
|
public function isMethod($method) |
272
|
|
|
{ |
273
|
|
|
return strtoupper($method) === $this->getMethod(); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* Check if request method is read only. |
278
|
|
|
* |
279
|
|
|
* @return bool |
280
|
|
|
*/ |
281
|
|
|
public function isReadOnly() |
282
|
|
|
{ |
283
|
|
|
return in_array($this->getMethod(), static::$safeMethods); |
284
|
|
|
} |
285
|
|
|
} |
286
|
|
|
|