1
|
|
|
<?php |
2
|
|
|
namespace Nkey\Caribu\Mvc\Controller; |
3
|
|
|
|
4
|
|
|
use \Generics\Socket\InvalidUrlException; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* The request is encapsulated in this class |
8
|
|
|
* |
9
|
|
|
* @author Maik Greubel <[email protected]> |
10
|
|
|
* |
11
|
|
|
* This file is part of Caribu MVC package |
12
|
|
|
*/ |
13
|
|
|
class Request |
14
|
|
|
{ |
15
|
|
|
use \Nkey\Caribu\Mvc\Util\RequestParser; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Address of remote host |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
private $remoteHost; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Original request uri |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
private $origin = null; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Controller requested |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
private $controller = null; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Action requested |
40
|
|
|
* |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
private $action = null; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Parameters of the request |
47
|
|
|
* |
48
|
|
|
* @var array |
49
|
|
|
*/ |
50
|
|
|
private $params = array(); |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Prefix of the uri inside application context |
54
|
|
|
* |
55
|
|
|
* @var string |
56
|
|
|
*/ |
57
|
|
|
private $contextPrefix = null; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* An occured exception |
61
|
|
|
* |
62
|
|
|
* @var \Exception |
63
|
|
|
*/ |
64
|
|
|
private $exception; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Create a new instance of request |
68
|
|
|
* |
69
|
|
|
* @param string $defaultController |
70
|
|
|
* The name of default controller of nothing is provided |
71
|
|
|
* @param string $defaultAction |
72
|
|
|
* The name of default action if nothing is provided |
73
|
|
|
*/ |
74
|
27 |
|
private function __construct($defaultController, $defaultAction) |
75
|
|
|
{ |
76
|
27 |
|
$this->controller = $defaultController; |
77
|
27 |
|
$this->action = $defaultAction; |
78
|
27 |
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Parse an uri into its request parts |
82
|
|
|
* |
83
|
|
|
* @param string $uri |
84
|
|
|
* The uri to parse |
85
|
|
|
* |
86
|
|
|
* @param array $serverVars |
87
|
|
|
* The variables provided by sapi |
88
|
|
|
* |
89
|
|
|
* @param string $defaultController |
90
|
|
|
* The name of the default controller if nothing else is requested |
91
|
|
|
* |
92
|
|
|
* @param string $defaultAction |
93
|
|
|
* The name of the default action if nothing else is requested |
94
|
|
|
* |
95
|
|
|
* @return \Nkey\Caribu\Mvc\Controller\Request The new created request |
96
|
|
|
*/ |
97
|
27 |
|
public static function parse($uri, $serverVars = array(), $defaultController = 'Index', $defaultAction = 'index') |
98
|
|
|
{ |
99
|
27 |
|
$req = new self($defaultController, $defaultAction); |
100
|
27 |
|
$req->origin = $uri; |
101
|
|
|
|
102
|
27 |
|
self::parseRemoteHost($req, $serverVars); |
103
|
|
|
|
104
|
27 |
|
self::parseGetPostSessionCookie($req); |
105
|
|
|
|
106
|
|
|
// Save the request parameters for later usage and rewrite the uri |
107
|
27 |
|
$savedRequestParams = array(); |
108
|
27 |
|
if (strpos($uri, '?')) { |
109
|
2 |
|
parse_str(substr($uri, strpos($uri, '?') + 1), $savedRequestParams); |
110
|
2 |
|
$uri = substr($uri, 0, strpos($uri, '?')); |
111
|
|
|
} |
112
|
|
|
|
113
|
27 |
|
self::parseContextPrefix($req, $serverVars); |
114
|
|
|
|
115
|
27 |
|
$parts = self::parseUri($req, $uri, $defaultController, $defaultAction); |
116
|
|
|
|
117
|
|
|
// Walk over all parameters and put them into container |
118
|
27 |
|
$numParts = count($parts); |
119
|
27 |
|
for ($i = 0; $i < $numParts; $i = $i + 2) { |
120
|
2 |
|
$paramName = trim($parts[$i]); |
121
|
2 |
|
$paramValue = isset($parts[$i + 1]) ? trim($parts[$i + 1]) : ''; |
122
|
2 |
|
if ($paramName && $paramValue) { |
123
|
1 |
|
$req->params[$paramName] = $paramValue; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
27 |
|
$req->params = array_merge($req->params, $savedRequestParams); |
128
|
|
|
|
129
|
27 |
|
self::parseParameters($req, $serverVars); |
130
|
|
|
|
131
|
|
|
// Et'voila |
132
|
27 |
|
return $req; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Parse uri directly from request uri |
137
|
|
|
* |
138
|
|
|
* @param array $serverVars |
139
|
|
|
* The server variables provided by sapi |
140
|
|
|
* @param string $defaultController The |
141
|
|
|
* name of the default controller |
142
|
|
|
* @param string $defaultAction The |
143
|
|
|
* name of the default action |
144
|
|
|
* |
145
|
|
|
* @return \Nkey\Caribu\Mvc\Controller\Request |
146
|
|
|
* |
147
|
|
|
* @throws InvalidUrlException If no uri exists (e.g. sapi = cli) |
148
|
|
|
*/ |
149
|
7 |
|
public static function parseFromServerRequest($serverVars, $defaultController = 'Index', $defaultAction = 'index') |
150
|
|
|
{ |
151
|
7 |
|
if (! isset($serverVars['REQUEST_URI'])) { |
152
|
1 |
|
throw new InvalidUrlException("No such uri provided"); |
153
|
|
|
} |
154
|
6 |
|
return self::parse($serverVars['REQUEST_URI'], $serverVars, $defaultController, $defaultAction); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* The origin uri |
159
|
|
|
* |
160
|
|
|
* @return string The original request string |
161
|
|
|
*/ |
162
|
4 |
|
public function getOrigin() |
163
|
|
|
{ |
164
|
4 |
|
return $this->origin; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* The requested controller |
169
|
|
|
* |
170
|
|
|
* @return string The controller |
171
|
|
|
*/ |
172
|
24 |
|
public function getController() |
173
|
|
|
{ |
174
|
24 |
|
return $this->controller; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Set controller property |
179
|
|
|
* |
180
|
|
|
* @param string $controller |
181
|
|
|
*/ |
182
|
23 |
|
public function setController($controller) |
183
|
|
|
{ |
184
|
23 |
|
$this->controller = strval($controller); |
185
|
23 |
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* The requested action |
189
|
|
|
* |
190
|
|
|
* @return string The action |
191
|
|
|
*/ |
192
|
24 |
|
public function getAction() |
193
|
|
|
{ |
194
|
24 |
|
return $this->action; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Set action property |
199
|
|
|
* |
200
|
|
|
* @param string $action |
201
|
|
|
*/ |
202
|
21 |
|
public function setAction($action) |
203
|
|
|
{ |
204
|
21 |
|
$this->action = $action; |
205
|
21 |
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Retrieve the request parameters |
209
|
|
|
* |
210
|
|
|
* @return array The request parameters |
211
|
|
|
*/ |
212
|
6 |
|
public function getParams() |
213
|
|
|
{ |
214
|
6 |
|
return $this->params; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Retrieve the context prefix |
219
|
|
|
* |
220
|
|
|
* @return string The context prefix |
221
|
|
|
*/ |
222
|
27 |
|
public function getContextPrefix() |
223
|
|
|
{ |
224
|
27 |
|
return $this->contextPrefix; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Set context prefix property |
229
|
|
|
* |
230
|
|
|
* @param string $prefix |
231
|
|
|
*/ |
232
|
5 |
|
public function setContextPrefix($prefix) |
233
|
|
|
{ |
234
|
5 |
|
$this->contextPrefix = strval($prefix); |
235
|
5 |
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Retrieve the remote host |
239
|
|
|
* |
240
|
|
|
* @return string The remote host address |
241
|
|
|
*/ |
242
|
25 |
|
public function getRemoteHost() |
243
|
|
|
{ |
244
|
25 |
|
return $this->remoteHost; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Check whether a given parameter exists |
249
|
|
|
* |
250
|
|
|
* @param string $name |
251
|
|
|
* The name of the parameter |
252
|
|
|
* @return boolean true in case of it exists, false otherwise |
253
|
|
|
*/ |
254
|
1 |
|
public function hasParam($name) |
255
|
|
|
{ |
256
|
1 |
|
return isset($this->params[$name]); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Get value of particular parameter |
261
|
|
|
* |
262
|
|
|
* @param string $name |
263
|
|
|
* The name of parameters |
264
|
|
|
* @param string $typeOf |
265
|
|
|
* The type expected parameter value |
266
|
|
|
* @return mixed Depending on $typeOf the value as requested type and escaped |
267
|
|
|
*/ |
268
|
1 |
|
public function getParam($name, $typeOf = 'string') |
269
|
|
|
{ |
270
|
1 |
|
$result = $this->hasParam($name) ? $this->params[$name] : null; |
271
|
|
|
|
272
|
|
|
switch ($typeOf) { |
273
|
1 |
|
case 'bool': |
274
|
1 |
|
case 'boolean': |
275
|
|
|
$result = function_exists('boolval') ? boolval($result) : (bool) $result; |
276
|
|
|
break; |
277
|
|
|
|
278
|
1 |
|
case 'double': |
279
|
1 |
|
case 'float': |
280
|
|
|
$result = doubleval($result); |
281
|
|
|
break; |
282
|
|
|
|
283
|
1 |
|
case 'int': |
284
|
|
|
$result = intval($result); |
285
|
|
|
break; |
286
|
|
|
|
287
|
1 |
|
case 'string': |
288
|
|
|
default: |
289
|
1 |
|
$result = htmlentities(strval($result)); |
290
|
1 |
|
break; |
291
|
|
|
} |
292
|
|
|
|
293
|
1 |
|
return $result; |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* To override a given parameter |
298
|
|
|
* |
299
|
|
|
* @param string $name |
300
|
|
|
* The parameter name to override |
301
|
|
|
* @param string $value |
302
|
|
|
* The value to override |
303
|
|
|
* |
304
|
|
|
* @return Request the current request as fluent interface |
305
|
|
|
*/ |
306
|
7 |
|
public function setParam($name, $value) |
307
|
|
|
{ |
308
|
7 |
|
$this->params[$name] = $value; |
309
|
7 |
|
return $this; |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* Set the exception occured |
314
|
|
|
* |
315
|
|
|
* @param \Exception $ex |
316
|
|
|
* |
317
|
|
|
* @return Request the current request instance |
318
|
|
|
*/ |
319
|
1 |
|
public function setException(\Exception $ex) |
320
|
|
|
{ |
321
|
1 |
|
$this->exception = $ex; |
322
|
1 |
|
return $this; |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* Get the exception occured |
327
|
|
|
* |
328
|
|
|
* @return \Exception |
329
|
|
|
*/ |
330
|
1 |
|
public function getException() |
331
|
|
|
{ |
332
|
1 |
|
return $this->exception; |
333
|
|
|
} |
334
|
|
|
} |
335
|
|
|
|