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