1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nono; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Simple request class which is passed as the first argument to |
7
|
|
|
* any callable / controller method associated with a route. |
8
|
|
|
* |
9
|
|
|
* @method string|mixed get(string $name = null, mixed $default = null) |
10
|
|
|
* @method string|mixed post(string $name = null, mixed $default = null) |
11
|
|
|
* @method string|mixed request(string $name = null, mixed $default = null) |
12
|
|
|
* @method string|mixed server(string $name = null, mixed $default = null) |
13
|
|
|
* @method string|mixed session(string $name = null, mixed $default = null) |
14
|
|
|
* @method string|mixed cookie(string $name = null, mixed $default = null) |
15
|
|
|
* @method array|mixed files(string $name = null, mixed $default = null) |
16
|
|
|
*/ |
17
|
|
|
class Request |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Return URI with query string. |
21
|
|
|
* |
22
|
|
|
* @return string |
23
|
|
|
*/ |
24
|
|
|
public function uri() |
25
|
|
|
{ |
26
|
|
|
return urldecode($this->plainUri()); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Return URI without query string. |
31
|
|
|
* |
32
|
|
|
* @return string |
33
|
|
|
*/ |
34
|
|
|
public function plainUri() |
35
|
|
|
{ |
36
|
|
|
return explode('?', $this->server('REQUEST_URI'))[0]; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return string |
41
|
|
|
*/ |
42
|
|
|
public function method() |
43
|
|
|
{ |
44
|
|
|
return $this->server('REQUEST_METHOD'); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @return string |
49
|
|
|
*/ |
50
|
|
|
public function isHttps() |
51
|
|
|
{ |
52
|
|
|
return $this->server('SERVER_PORT') == 443 |
53
|
|
|
|| strtoupper($this->server('HTTPS')) == 'ON'; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
|
|
public function host() |
60
|
|
|
{ |
61
|
|
|
return $this->server('HTTP_HOST'); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return float |
66
|
|
|
*/ |
67
|
|
|
public function requestTimeFloat() |
68
|
|
|
{ |
69
|
|
|
return $this->server('REQUEST_TIME_FLOAT'); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return float |
74
|
|
|
*/ |
75
|
|
|
public function elapsedRequestTimeFloat() |
76
|
|
|
{ |
77
|
|
|
return microtime(1) - $this->server('REQUEST_TIME_FLOAT'); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Magic method to access super globals with optional default argument. |
82
|
|
|
* Calling without arguments e.g. $request->server() will simply |
83
|
|
|
* return the entire _SERVER global. |
84
|
|
|
* |
85
|
|
|
* @param string $name |
86
|
|
|
* @param array $args |
87
|
|
|
* @return mixed |
88
|
|
|
*/ |
89
|
|
|
public function __call($name, $args) |
|
|
|
|
90
|
|
|
{ |
91
|
|
|
$global = $GLOBALS['_' . strtoupper($name)]; |
92
|
|
|
$default = isset($args[1]) ? $args[1] : null; |
93
|
|
|
|
94
|
|
|
if (!count($args)) { |
95
|
|
|
return $global; |
96
|
|
|
} elseif (isset($global[$args[0]])) { |
97
|
|
|
return $global[$args[0]]; |
98
|
|
|
} else { |
99
|
|
|
return $default; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: