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
|
|
|
* Just triggering the super global. |
21
|
|
|
*/ |
22
|
18 |
|
public function __construct() |
|
|
|
|
23
|
|
|
{ |
24
|
18 |
|
$_SERVER; |
25
|
18 |
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Return URI with query string. |
29
|
|
|
* |
30
|
|
|
* @return string |
31
|
|
|
*/ |
32
|
5 |
|
public function uri() |
33
|
|
|
{ |
34
|
5 |
|
return urldecode($this->plainUri()); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Return URI without query string. |
39
|
|
|
* |
40
|
|
|
* @return string |
41
|
|
|
*/ |
42
|
5 |
|
public function plainUri() |
43
|
|
|
{ |
44
|
5 |
|
return explode('?', $this->server('REQUEST_URI'))[0]; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Return the http verb. |
49
|
|
|
* |
50
|
|
|
* @return string |
51
|
|
|
*/ |
52
|
5 |
|
public function method() |
53
|
|
|
{ |
54
|
5 |
|
return $this->server('REQUEST_METHOD'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Return whether the request URL is HTTPS URL or not. |
59
|
|
|
* Questionable reliability -> depends upon server config. |
60
|
|
|
* |
61
|
|
|
* @return string |
62
|
|
|
*/ |
63
|
1 |
|
public function isHttps() |
64
|
|
|
{ |
65
|
1 |
|
return $this->server('SERVER_PORT') == 443 |
66
|
1 |
|
|| strtoupper($this->server('HTTPS')) == 'ON'; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Return the hostname. |
71
|
|
|
* |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
1 |
|
public function host() |
75
|
|
|
{ |
76
|
1 |
|
return $this->server('HTTP_HOST'); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Return the time the request was initiated. |
81
|
|
|
* |
82
|
|
|
* @return float |
83
|
|
|
*/ |
84
|
1 |
|
public function requestTimeFloat() |
85
|
|
|
{ |
86
|
1 |
|
return $this->server('REQUEST_TIME_FLOAT'); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Return the elapsed time since the request was initiated. |
91
|
|
|
* |
92
|
|
|
* @return float |
93
|
|
|
*/ |
94
|
1 |
|
public function elapsedRequestTimeFloat() |
95
|
|
|
{ |
96
|
1 |
|
return microtime(1) - $this->server('REQUEST_TIME_FLOAT'); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Allow redirects when headers have already been sent |
101
|
|
|
* due to sessions or other output. |
102
|
|
|
* |
103
|
|
|
* @param string $url |
104
|
|
|
*/ |
105
|
1 |
|
public function redirect($url) |
106
|
|
|
{ |
107
|
1 |
|
echo "<script>location.replace('$url');</script>"; |
108
|
1 |
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Magic method to access super globals with optional default argument. |
112
|
|
|
* Calling without arguments e.g. $request->server() will simply |
113
|
|
|
* return the entire _SERVER global. |
114
|
|
|
* |
115
|
|
|
* @param string $name |
116
|
|
|
* @param array $args |
117
|
|
|
* @return mixed |
118
|
|
|
*/ |
119
|
16 |
|
public function __call($name, $args) |
|
|
|
|
120
|
|
|
{ |
121
|
16 |
|
$global = $GLOBALS['_' . strtoupper($name)]; |
122
|
16 |
|
$default = isset($args[1]) ? $args[1] : null; |
123
|
|
|
|
124
|
16 |
|
if (!count($args)) { |
125
|
1 |
|
return $global; |
126
|
15 |
|
} elseif (isset($global[$args[0]])) { |
127
|
13 |
|
return $global[$args[0]]; |
128
|
|
|
} else { |
129
|
2 |
|
return $default; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
|
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: