|
1
|
|
|
<?php |
|
2
|
|
|
namespace DevOp\Core\Http; |
|
3
|
|
|
|
|
4
|
|
|
use DevOp\Core\Http\Request; |
|
5
|
|
|
use Psr\Http\Message\UriInterface; |
|
6
|
|
|
use Psr\Http\Message\StreamInterface; |
|
7
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
8
|
|
|
|
|
9
|
|
|
class ServerRequest extends Request implements ServerRequestInterface |
|
10
|
|
|
{ |
|
11
|
|
|
|
|
12
|
|
|
use MessageTrait; |
|
13
|
|
|
use RequestTrait; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var array |
|
17
|
|
|
*/ |
|
18
|
|
|
private $attributes = []; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var array |
|
22
|
|
|
*/ |
|
23
|
|
|
private $cookieParams = []; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var mixed |
|
27
|
|
|
*/ |
|
28
|
|
|
private $parsedBody; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var array |
|
32
|
|
|
*/ |
|
33
|
|
|
private $queryParams = []; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var array |
|
37
|
|
|
*/ |
|
38
|
|
|
private $serverParams = []; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var array |
|
42
|
|
|
*/ |
|
43
|
|
|
private $uploadedFiles = []; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @return self |
|
47
|
|
|
*/ |
|
48
|
|
|
public static function createFromGlobals() |
|
49
|
|
|
{ |
|
50
|
|
|
|
|
51
|
|
|
$uri = Uri::createFromGlobals(); |
|
52
|
|
|
|
|
53
|
|
|
$method = 'GET'; |
|
54
|
|
|
if (isset($_SERVER['REQUEST_METHOD'])) { |
|
55
|
|
|
$method = $_SERVER['REQUEST_METHOD']; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
$headers = []; |
|
59
|
|
|
if (function_exists('getallheaders')) { |
|
60
|
|
|
$headers = getallheaders(); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$body = new Stream('php://memory'); |
|
64
|
|
|
|
|
65
|
|
|
$protocol = '1.1'; |
|
66
|
|
|
if (isset($_SERVER['SERVER_PROTOCOL'])) { |
|
67
|
|
|
$protocol = str_replace('HTTP/', '', $_SERVER['SERVER_PROTOCOL']); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$serverRequest = new ServerRequest($method, $uri, $headers, $body, $protocol, $_SERVER); |
|
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
return $serverRequest |
|
73
|
|
|
->withParsedBody($_POST) |
|
74
|
|
|
->withQueryParams($_GET) |
|
75
|
|
|
->withCookieParams($_COOKIE) |
|
76
|
|
|
->withUploadedFiles(UploadedFile::createFromGlobal()); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* |
|
81
|
|
|
* @param string $method |
|
82
|
|
|
* @param UriInterface|string $uri |
|
83
|
|
|
* @param array $headers |
|
84
|
|
|
* @param string|null|resource|StreamInterface $body |
|
85
|
|
|
* @param string|null $version |
|
86
|
|
|
* @param array $serverParams |
|
87
|
|
|
*/ |
|
88
|
2 |
|
public function __construct($method, $uri, array $headers = [], $body = 'php://memory', $version = '1.1', array $serverParams = []) |
|
89
|
|
|
{ |
|
90
|
2 |
|
parent::__construct($method, $uri, $headers, $body, $version); |
|
|
|
|
|
|
91
|
2 |
|
$this->serverParams = $serverParams; |
|
92
|
2 |
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @return string |
|
96
|
|
|
*/ |
|
97
|
|
|
public function getAttribute($name, $default = null) |
|
98
|
|
|
{ |
|
99
|
|
|
if (array_key_exists($name, $this->attributes)) { |
|
100
|
|
|
return $this->attributes[$name]; |
|
101
|
|
|
} |
|
102
|
|
|
return $default; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @return array |
|
107
|
|
|
*/ |
|
108
|
2 |
|
public function getAttributes() |
|
109
|
|
|
{ |
|
110
|
2 |
|
return $this->attributes; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @return array |
|
115
|
|
|
*/ |
|
116
|
|
|
public function getCookieParams() |
|
117
|
|
|
{ |
|
118
|
|
|
return $this->cookieParams; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @return mixed |
|
123
|
|
|
*/ |
|
124
|
|
|
public function getParsedBody() |
|
125
|
|
|
{ |
|
126
|
|
|
return $this->parsedBody; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @return array |
|
131
|
|
|
*/ |
|
132
|
|
|
public function getQueryParams() |
|
133
|
|
|
{ |
|
134
|
|
|
return $this->queryParams; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* @return array |
|
139
|
|
|
*/ |
|
140
|
|
|
public function getServerParams() |
|
141
|
|
|
{ |
|
142
|
|
|
return $this->serverParams; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @return array |
|
147
|
|
|
*/ |
|
148
|
|
|
public function getUploadedFiles() |
|
149
|
|
|
{ |
|
150
|
|
|
return $this->uploadedFiles; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* @param string $name |
|
155
|
|
|
* @param string $value |
|
156
|
|
|
* @return \DevOp\Core\Http\ServerRequest |
|
157
|
|
|
*/ |
|
158
|
|
|
public function withAttribute($name, $value) |
|
159
|
|
|
{ |
|
160
|
|
|
$clone = clone $this; |
|
161
|
|
|
$clone->attributes[$name] = $value; |
|
162
|
|
|
|
|
163
|
|
|
return $clone; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* @param array $cookies |
|
168
|
|
|
* @return \DevOp\Core\Http\ServerRequest |
|
169
|
|
|
*/ |
|
170
|
|
|
public function withCookieParams(array $cookies) |
|
171
|
|
|
{ |
|
172
|
|
|
$clone = clone $this; |
|
173
|
|
|
$clone->cookieParams = $cookies; |
|
174
|
|
|
|
|
175
|
|
|
return $clone; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* @param mixed $data |
|
180
|
|
|
* @return \DevOp\Core\Http\ServerRequest |
|
181
|
|
|
*/ |
|
182
|
|
|
public function withParsedBody($data) |
|
183
|
|
|
{ |
|
184
|
|
|
$clone = clone $this; |
|
185
|
|
|
$clone->parsedBody = $data; |
|
186
|
|
|
|
|
187
|
|
|
return $clone; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* @param array $query |
|
192
|
|
|
* @return \DevOp\Core\Http\ServerRequest |
|
193
|
|
|
*/ |
|
194
|
|
|
public function withQueryParams(array $query) |
|
195
|
|
|
{ |
|
196
|
|
|
$clone = clone $this; |
|
197
|
|
|
$clone->queryParams = $query; |
|
198
|
|
|
|
|
199
|
|
|
return $clone; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* @param array $uploadedFiles |
|
204
|
|
|
* @return \DevOp\Core\Http\ServerRequest |
|
205
|
|
|
*/ |
|
206
|
|
|
public function withUploadedFiles(array $uploadedFiles) |
|
207
|
|
|
{ |
|
208
|
|
|
$clone = clone $this; |
|
209
|
|
|
$clone->uploadedFiles = $uploadedFiles; |
|
210
|
|
|
|
|
211
|
|
|
return $clone; |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* @param string $name |
|
216
|
|
|
* @return \DevOp\Core\Http\ServerRequest|$this |
|
217
|
|
|
*/ |
|
218
|
|
|
public function withoutAttribute($name) |
|
219
|
|
|
{ |
|
220
|
|
|
if (!array_key_exists($name, $this->attributes)) { |
|
221
|
|
|
return $this; |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
$clone = clone $this; |
|
225
|
|
|
unset($clone->attributes[$name]); |
|
226
|
|
|
|
|
227
|
|
|
return $clone; |
|
228
|
|
|
} |
|
229
|
|
|
} |
|
230
|
|
|
|