5 use InvalidArgumentException;
30 private $attributes = [];
35 private $cookieParams = [];
45 private $queryParams = [];
50 private $serverParams;
55 private $uploadedFiles = [];
71 array $serverParams = []
73 $this->serverParams = $serverParams;
75 parent::__construct($method, $uri, $headers, $body, $version);
89 foreach ($files as $key => $value) {
91 $normalized[$key] = $value;
92 } elseif (is_array($value) && isset($value[
'tmp_name'])) {
93 $normalized[$key] = self::createUploadedFileFromSpec($value);
94 } elseif (is_array($value)) {
114 private static function createUploadedFileFromSpec(array $value)
116 if (is_array($value[
'tmp_name'])) {
117 return self::normalizeNestedFileSpec($value);
122 (
int) $value[
'size'],
123 (
int) $value[
'error'],
138 private static function normalizeNestedFileSpec(array $files = [])
140 $normalizedFiles = [];
142 foreach (array_keys($files[
'tmp_name']) as $key) {
144 'tmp_name' => $files[
'tmp_name'][$key],
145 'size' => $files[
'size'][$key],
146 'error' => $files[
'error'][$key],
147 'name' => $files[
'name'][$key],
148 'type' => $files[
'type'][$key],
150 $normalizedFiles[$key] = self::createUploadedFileFromSpec($spec);
153 return $normalizedFiles;
168 $method = isset($_SERVER[
'REQUEST_METHOD']) ? $_SERVER[
'REQUEST_METHOD'] :
'GET';
169 $headers = getallheaders();
171 $body =
new CachingStream(
new LazyOpenStream(
'php://input',
'r+'));
172 $protocol = isset($_SERVER[
'SERVER_PROTOCOL']) ? str_replace(
'HTTP/',
'', $_SERVER[
'SERVER_PROTOCOL']) :
'1.1';
174 $serverRequest =
new ServerRequest($method, $uri, $headers, $body, $protocol, $_SERVER);
176 return $serverRequest
177 ->withCookieParams($_COOKIE)
178 ->withQueryParams($_GET)
179 ->withParsedBody($_POST)
180 ->withUploadedFiles(self::normalizeFiles($_FILES));
183 private static function extractHostAndPortFromAuthority($authority)
185 $uri =
'http://'.$authority;
186 $parts = parse_url($uri);
187 if (
false === $parts) {
191 $host = isset($parts[
'host']) ? $parts[
'host'] :
null;
192 $port = isset($parts[
'port']) ? $parts[
'port'] :
null;
194 return [$host, $port];
206 $uri = $uri->withScheme(!empty($_SERVER[
'HTTPS']) && $_SERVER[
'HTTPS'] !==
'off' ?
'https' :
'http');
209 if (isset($_SERVER[
'HTTP_HOST'])) {
210 list($host, $port) = self::extractHostAndPortFromAuthority($_SERVER[
'HTTP_HOST']);
211 if ($host !==
null) {
212 $uri = $uri->withHost($host);
215 if ($port !==
null) {
217 $uri = $uri->withPort($port);
219 } elseif (isset($_SERVER[
'SERVER_NAME'])) {
220 $uri = $uri->withHost($_SERVER[
'SERVER_NAME']);
221 } elseif (isset($_SERVER[
'SERVER_ADDR'])) {
222 $uri = $uri->withHost($_SERVER[
'SERVER_ADDR']);
225 if (!$hasPort && isset($_SERVER[
'SERVER_PORT'])) {
226 $uri = $uri->withPort($_SERVER[
'SERVER_PORT']);
230 if (isset($_SERVER[
'REQUEST_URI'])) {
231 $requestUriParts = explode(
'?', $_SERVER[
'REQUEST_URI'], 2);
232 $uri = $uri->withPath($requestUriParts[0]);
233 if (isset($requestUriParts[1])) {
235 $uri = $uri->withQuery($requestUriParts[1]);
239 if (!$hasQuery && isset($_SERVER[
'QUERY_STRING'])) {
240 $uri = $uri->withQuery($_SERVER[
'QUERY_STRING']);
252 return $this->serverParams;
260 return $this->uploadedFiles;
269 $new->uploadedFiles = $uploadedFiles;
279 return $this->cookieParams;
288 $new->cookieParams = $cookies;
298 return $this->queryParams;
307 $new->queryParams = $query;
317 return $this->parsedBody;
326 $new->parsedBody = $data;
336 return $this->attributes;
342 public function getAttribute($attribute, $default =
null)
344 if (
false === array_key_exists($attribute, $this->attributes)) {
348 return $this->attributes[$attribute];
357 $new->attributes[$attribute] = $value;
367 if (
false === array_key_exists($attribute, $this->attributes)) {
372 unset($new->attributes[$attribute]);