1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CarbonFramework; |
4
|
|
|
|
5
|
|
|
use CarbonFramework\Support\Arr; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* A server request representation |
9
|
|
|
*/ |
10
|
|
|
class Request { |
11
|
|
|
/** |
12
|
|
|
* GET parameters |
13
|
|
|
* |
14
|
|
|
* @var array |
15
|
|
|
*/ |
16
|
|
|
protected $get = []; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* POST parameters |
20
|
|
|
* |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
protected $post = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* COOKIE parameters |
27
|
|
|
* |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
protected $cookie = []; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* FILES parameters |
34
|
|
|
* |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
protected $files = []; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* SERVER parameters |
41
|
|
|
* |
42
|
|
|
* @var array |
43
|
|
|
*/ |
44
|
|
|
protected $server = []; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Headers |
48
|
|
|
* |
49
|
|
|
* @var array |
50
|
|
|
*/ |
51
|
|
|
protected $headers = []; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Create a new instance from php superglobals |
55
|
|
|
* |
56
|
|
|
* @return Request |
57
|
|
|
*/ |
58
|
1 |
|
public static function fromGlobals() { |
59
|
1 |
|
return new static( stripslashes_deep( $_GET ), stripslashes_deep( $_POST ), $_COOKIE, $_FILES, $_SERVER, getallheaders() ); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Constructor |
64
|
|
|
* |
65
|
|
|
* @param array $get |
66
|
|
|
* @param array $post |
67
|
|
|
* @param array $cookie |
68
|
|
|
* @param array $files |
69
|
|
|
* @param array $server |
70
|
|
|
* @param array $headers |
71
|
|
|
*/ |
72
|
1 |
|
public function __construct( $get, $post, $cookie, $files, $server, $headers ) { |
73
|
1 |
|
$this->get = $get; |
74
|
1 |
|
$this->post = $post; |
75
|
1 |
|
$this->cookie = $cookie; |
76
|
1 |
|
$this->files = $files; |
77
|
1 |
|
$this->server = $server; |
78
|
1 |
|
$this->headers = $headers; |
79
|
1 |
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Return the request method |
83
|
|
|
* |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
1 |
|
public function getMethod() { |
87
|
1 |
|
$method = (string) Arr::get( $this->server, 'REQUEST_METHOD', 'GET' ); |
88
|
|
|
|
89
|
1 |
|
$override = (string) Arr::get( $this->headers, 'X-HTTP-METHOD-OVERRIDE' ); |
90
|
1 |
|
if ( $method === 'POST' && $override ) { |
91
|
1 |
|
$method = $override; |
92
|
1 |
|
} |
93
|
|
|
|
94
|
1 |
|
return strtoupper( $method ); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Return the request url |
99
|
|
|
* |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
3 |
|
public function getUrl() { |
103
|
3 |
|
$https = Arr::get( $this->server, 'HTTPS' ); |
104
|
|
|
|
105
|
3 |
|
$protocol = $https ? 'https' : 'http'; |
106
|
3 |
|
$host = (string) Arr::get( $this->server, 'HTTP_HOST', '' ); |
107
|
3 |
|
$uri = (string) Arr::get( $this->server, 'REQUEST_URI', '' ); |
108
|
3 |
|
$uri = Url::addLeadingSlash( $uri ); |
109
|
|
|
|
110
|
3 |
|
return $protocol . '://' . $host . $uri; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Return a value from any of the request parameters |
115
|
|
|
* |
116
|
|
|
* @see \CarbonFramework\Support\Arr |
117
|
|
|
* @return mixed |
118
|
|
|
*/ |
119
|
3 |
|
protected function input() { |
120
|
3 |
|
$args = func_get_args(); |
121
|
3 |
|
$source = $this->{$args[0]}; |
122
|
|
|
|
123
|
3 |
|
if ( count( $args ) === 1 ) { |
124
|
|
|
return $source; |
125
|
|
|
} |
126
|
|
|
|
127
|
3 |
|
$args[0] = $source; |
128
|
3 |
|
return call_user_func_array( [Arr::class, 'get'], $args ); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Return a value from the GET parameters |
133
|
|
|
* |
134
|
|
|
* @see \CarbonFramework\Support\Arr |
135
|
|
|
* @return mixed |
136
|
|
|
*/ |
137
|
3 |
|
public function get() { |
138
|
3 |
|
return call_user_func_array( [$this, 'input'], array_merge( ['get'], func_get_args() ) ); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Return a value from the POST parameters |
143
|
|
|
* |
144
|
|
|
* @see \CarbonFramework\Support\Arr |
145
|
|
|
* @return mixed |
146
|
|
|
*/ |
147
|
|
|
public function post() { |
148
|
|
|
return call_user_func_array( [$this, 'input'], array_merge( ['post'], func_get_args() ) ); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Return a value from the COOKIE parameters |
153
|
|
|
* |
154
|
|
|
* @see \CarbonFramework\Support\Arr |
155
|
|
|
* @return mixed |
156
|
|
|
*/ |
157
|
|
|
public function cookie() { |
158
|
|
|
return call_user_func_array( [$this, 'input'], array_merge( ['cookie'], func_get_args() ) ); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Return a value from the FILES parameters |
163
|
|
|
* |
164
|
|
|
* @see \CarbonFramework\Support\Arr |
165
|
|
|
* @return mixed |
166
|
|
|
*/ |
167
|
|
|
public function files() { |
168
|
|
|
return call_user_func_array( [$this, 'input'], array_merge( ['files'], func_get_args() ) ); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Return a value from the SERVER parameters |
173
|
|
|
* |
174
|
|
|
* @see \CarbonFramework\Support\Arr |
175
|
|
|
* @return mixed |
176
|
|
|
*/ |
177
|
|
|
public function server() { |
178
|
|
|
return call_user_func_array( [$this, 'input'], array_merge( ['server'], func_get_args() ) ); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Return a value from the headers |
183
|
|
|
* |
184
|
|
|
* @see \CarbonFramework\Support\Arr |
185
|
|
|
* @return mixed |
186
|
|
|
*/ |
187
|
|
|
public function headers() { |
188
|
|
|
return call_user_func_array( [$this, 'input'], array_merge( ['headers'], func_get_args() ) ); |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|