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