Open Journal Systems  3.3.0
ServerRequest.php
1 <?php
2 
3 namespace GuzzleHttp\Psr7;
4 
5 use InvalidArgumentException;
10 
26 {
30  private $attributes = [];
31 
35  private $cookieParams = [];
36 
40  private $parsedBody;
41 
45  private $queryParams = [];
46 
50  private $serverParams;
51 
55  private $uploadedFiles = [];
56 
65  public function __construct(
66  $method,
67  $uri,
68  array $headers = [],
69  $body = null,
70  $version = '1.1',
71  array $serverParams = []
72  ) {
73  $this->serverParams = $serverParams;
74 
75  parent::__construct($method, $uri, $headers, $body, $version);
76  }
77 
85  public static function normalizeFiles(array $files)
86  {
87  $normalized = [];
88 
89  foreach ($files as $key => $value) {
90  if ($value instanceof UploadedFileInterface) {
91  $normalized[$key] = $value;
92  } elseif (is_array($value) && isset($value['tmp_name'])) {
93  $normalized[$key] = self::createUploadedFileFromSpec($value);
94  } elseif (is_array($value)) {
95  $normalized[$key] = self::normalizeFiles($value);
96  continue;
97  } else {
98  throw new InvalidArgumentException('Invalid value in files specification');
99  }
100  }
101 
102  return $normalized;
103  }
104 
114  private static function createUploadedFileFromSpec(array $value)
115  {
116  if (is_array($value['tmp_name'])) {
117  return self::normalizeNestedFileSpec($value);
118  }
119 
120  return new UploadedFile(
121  $value['tmp_name'],
122  (int) $value['size'],
123  (int) $value['error'],
124  $value['name'],
125  $value['type']
126  );
127  }
128 
138  private static function normalizeNestedFileSpec(array $files = [])
139  {
140  $normalizedFiles = [];
141 
142  foreach (array_keys($files['tmp_name']) as $key) {
143  $spec = [
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],
149  ];
150  $normalizedFiles[$key] = self::createUploadedFileFromSpec($spec);
151  }
152 
153  return $normalizedFiles;
154  }
155 
166  public static function fromGlobals()
167  {
168  $method = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : 'GET';
169  $headers = getallheaders();
170  $uri = self::getUriFromGlobals();
171  $body = new CachingStream(new LazyOpenStream('php://input', 'r+'));
172  $protocol = isset($_SERVER['SERVER_PROTOCOL']) ? str_replace('HTTP/', '', $_SERVER['SERVER_PROTOCOL']) : '1.1';
173 
174  $serverRequest = new ServerRequest($method, $uri, $headers, $body, $protocol, $_SERVER);
175 
176  return $serverRequest
177  ->withCookieParams($_COOKIE)
178  ->withQueryParams($_GET)
179  ->withParsedBody($_POST)
180  ->withUploadedFiles(self::normalizeFiles($_FILES));
181  }
182 
183  private static function extractHostAndPortFromAuthority($authority)
184  {
185  $uri = 'http://'.$authority;
186  $parts = parse_url($uri);
187  if (false === $parts) {
188  return [null, null];
189  }
190 
191  $host = isset($parts['host']) ? $parts['host'] : null;
192  $port = isset($parts['port']) ? $parts['port'] : null;
193 
194  return [$host, $port];
195  }
196 
202  public static function getUriFromGlobals()
203  {
204  $uri = new Uri('');
205 
206  $uri = $uri->withScheme(!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' ? 'https' : 'http');
207 
208  $hasPort = false;
209  if (isset($_SERVER['HTTP_HOST'])) {
210  list($host, $port) = self::extractHostAndPortFromAuthority($_SERVER['HTTP_HOST']);
211  if ($host !== null) {
212  $uri = $uri->withHost($host);
213  }
214 
215  if ($port !== null) {
216  $hasPort = true;
217  $uri = $uri->withPort($port);
218  }
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']);
223  }
224 
225  if (!$hasPort && isset($_SERVER['SERVER_PORT'])) {
226  $uri = $uri->withPort($_SERVER['SERVER_PORT']);
227  }
228 
229  $hasQuery = false;
230  if (isset($_SERVER['REQUEST_URI'])) {
231  $requestUriParts = explode('?', $_SERVER['REQUEST_URI'], 2);
232  $uri = $uri->withPath($requestUriParts[0]);
233  if (isset($requestUriParts[1])) {
234  $hasQuery = true;
235  $uri = $uri->withQuery($requestUriParts[1]);
236  }
237  }
238 
239  if (!$hasQuery && isset($_SERVER['QUERY_STRING'])) {
240  $uri = $uri->withQuery($_SERVER['QUERY_STRING']);
241  }
242 
243  return $uri;
244  }
245 
246 
250  public function getServerParams()
251  {
252  return $this->serverParams;
253  }
254 
258  public function getUploadedFiles()
259  {
260  return $this->uploadedFiles;
261  }
262 
266  public function withUploadedFiles(array $uploadedFiles)
267  {
268  $new = clone $this;
269  $new->uploadedFiles = $uploadedFiles;
270 
271  return $new;
272  }
273 
277  public function getCookieParams()
278  {
279  return $this->cookieParams;
280  }
281 
285  public function withCookieParams(array $cookies)
286  {
287  $new = clone $this;
288  $new->cookieParams = $cookies;
289 
290  return $new;
291  }
292 
296  public function getQueryParams()
297  {
298  return $this->queryParams;
299  }
300 
304  public function withQueryParams(array $query)
305  {
306  $new = clone $this;
307  $new->queryParams = $query;
308 
309  return $new;
310  }
311 
315  public function getParsedBody()
316  {
317  return $this->parsedBody;
318  }
319 
323  public function withParsedBody($data)
324  {
325  $new = clone $this;
326  $new->parsedBody = $data;
327 
328  return $new;
329  }
330 
334  public function getAttributes()
335  {
336  return $this->attributes;
337  }
338 
342  public function getAttribute($attribute, $default = null)
343  {
344  if (false === array_key_exists($attribute, $this->attributes)) {
345  return $default;
346  }
347 
348  return $this->attributes[$attribute];
349  }
350 
354  public function withAttribute($attribute, $value)
355  {
356  $new = clone $this;
357  $new->attributes[$attribute] = $value;
358 
359  return $new;
360  }
361 
365  public function withoutAttribute($attribute)
366  {
367  if (false === array_key_exists($attribute, $this->attributes)) {
368  return $this;
369  }
370 
371  $new = clone $this;
372  unset($new->attributes[$attribute]);
373 
374  return $new;
375  }
376 }
Psr\Http\Message\UploadedFileInterface
Definition: UploadedFileInterface.php:13
GuzzleHttp\Psr7\ServerRequest\getQueryParams
getQueryParams()
Definition: ServerRequest.php:314
GuzzleHttp\Psr7\ServerRequest\withUploadedFiles
withUploadedFiles(array $uploadedFiles)
Definition: ServerRequest.php:284
GuzzleHttp\Psr7\ServerRequest\__construct
__construct( $method, $uri, array $headers=[], $body=null, $version='1.1', array $serverParams=[])
Definition: ServerRequest.php:83
Psr\Http\Message\StreamInterface
Definition: vendor/psr/http-message/src/StreamInterface.php:12
GuzzleHttp\Psr7\ServerRequest\fromGlobals
static fromGlobals()
Definition: ServerRequest.php:184
GuzzleHttp\Psr7\ServerRequest\getServerParams
getServerParams()
Definition: ServerRequest.php:268
GuzzleHttp\Psr7\ServerRequest\getCookieParams
getCookieParams()
Definition: ServerRequest.php:295
GuzzleHttp\Psr7\CachingStream
Definition: CachingStream.php:10
Psr\Http\Message\ServerRequestInterface
Definition: ServerRequestInterface.php:43
GuzzleHttp\Psr7\ServerRequest\withAttribute
withAttribute($attribute, $value)
Definition: ServerRequest.php:372
GuzzleHttp\Psr7\Uri
Definition: Uri.php:13
GuzzleHttp\Psr7
Definition: AppendStream.php:2
Psr\Http\Message\UriInterface
Definition: UriInterface.php:24
GuzzleHttp\Psr7\ServerRequest\getAttributes
getAttributes()
Definition: ServerRequest.php:352
GuzzleHttp\Psr7\ServerRequest\normalizeFiles
static normalizeFiles(array $files)
Definition: ServerRequest.php:103
GuzzleHttp\Psr7\LazyOpenStream
Definition: LazyOpenStream.php:10
GuzzleHttp\Psr7\ServerRequest\withCookieParams
withCookieParams(array $cookies)
Definition: ServerRequest.php:303
GuzzleHttp\Psr7\Request
Definition: vendor/guzzlehttp/psr7/src/Request.php:12
GuzzleHttp\Psr7\UploadedFile
Definition: vendor/guzzlehttp/psr7/src/UploadedFile.php:9
GuzzleHttp\Psr7\ServerRequest
Definition: ServerRequest.php:25
GuzzleHttp\Psr7\ServerRequest\withoutAttribute
withoutAttribute($attribute)
Definition: ServerRequest.php:383
GuzzleHttp\Psr7\ServerRequest\getParsedBody
getParsedBody()
Definition: ServerRequest.php:333
GuzzleHttp\Psr7\ServerRequest\withQueryParams
withQueryParams(array $query)
Definition: ServerRequest.php:322
GuzzleHttp\Psr7\ServerRequest\withParsedBody
withParsedBody($data)
Definition: ServerRequest.php:341
GuzzleHttp\Psr7\ServerRequest\getAttribute
getAttribute($attribute, $default=null)
Definition: ServerRequest.php:360
GuzzleHttp\Psr7\ServerRequest\getUploadedFiles
getUploadedFiles()
Definition: ServerRequest.php:276
GuzzleHttp\Psr7\ServerRequest\getUriFromGlobals
static getUriFromGlobals()
Definition: ServerRequest.php:220
GuzzleHttp\Exception\InvalidArgumentException
Definition: vendor/guzzlehttp/guzzle/src/Exception/InvalidArgumentException.php:5