1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* It's free open-source software released under the MIT License. |
5
|
|
|
* |
6
|
|
|
* @author Anatoly Nekhay <[email protected]> |
7
|
|
|
* @copyright Copyright (c) 2018, Anatoly Nekhay |
8
|
|
|
* @license https://github.com/sunrise-php/http-message/blob/master/LICENSE |
9
|
|
|
* @link https://github.com/sunrise-php/http-message |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sunrise\Http\Message; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Import classes |
16
|
|
|
*/ |
17
|
|
|
use Psr\Http\Message\ServerRequestFactoryInterface; |
18
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
19
|
|
|
use Sunrise\Http\Message\Stream\PhpInputStream; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* ServerRequestFactory |
23
|
|
|
* |
24
|
|
|
* @link https://www.php-fig.org/psr/psr-17/ |
25
|
|
|
*/ |
26
|
|
|
class ServerRequestFactory implements ServerRequestFactoryInterface |
27
|
|
|
{ |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Creates a new request from superglobals variables |
31
|
|
|
* |
32
|
|
|
* @param array|null $serverParams |
33
|
|
|
* @param array|null $queryParams |
34
|
|
|
* @param array|null $cookieParams |
35
|
|
|
* @param array|null $uploadedFiles |
36
|
|
|
* @param array|null $parsedBody |
37
|
|
|
* |
38
|
|
|
* @return ServerRequestInterface |
39
|
|
|
* |
40
|
|
|
* @link http://php.net/manual/en/language.variables.superglobals.php |
41
|
|
|
* @link https://www.php-fig.org/psr/psr-15/meta/ |
42
|
|
|
*/ |
43
|
40 |
|
public static function fromGlobals( |
44
|
|
|
?array $serverParams = null, |
45
|
|
|
?array $queryParams = null, |
46
|
|
|
?array $cookieParams = null, |
47
|
|
|
?array $uploadedFiles = null, |
48
|
|
|
?array $parsedBody = null |
49
|
|
|
): ServerRequestInterface { |
50
|
40 |
|
$serverParams = $serverParams ?? $_SERVER; |
51
|
40 |
|
$queryParams = $queryParams ?? $_GET; |
52
|
40 |
|
$cookieParams = $cookieParams ?? $_COOKIE; |
53
|
40 |
|
$uploadedFiles = $uploadedFiles ?? $_FILES; |
54
|
40 |
|
$parsedBody = $parsedBody ?? $_POST; |
55
|
|
|
|
56
|
40 |
|
return new ServerRequest( |
57
|
40 |
|
server_request_protocol_version($serverParams), |
58
|
40 |
|
server_request_method($serverParams), |
59
|
40 |
|
server_request_uri($serverParams), |
60
|
40 |
|
server_request_headers($serverParams), |
61
|
40 |
|
new PhpInputStream(), |
62
|
40 |
|
$serverParams, |
63
|
40 |
|
$queryParams, |
64
|
40 |
|
$cookieParams, |
65
|
40 |
|
server_request_files($uploadedFiles), |
66
|
40 |
|
$parsedBody |
67
|
40 |
|
); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
*/ |
73
|
19 |
|
public function createServerRequest(string $method, $uri, array $serverParams = []): ServerRequestInterface |
74
|
|
|
{ |
75
|
19 |
|
return new ServerRequest( |
76
|
19 |
|
server_request_protocol_version($serverParams), |
77
|
19 |
|
$method, |
78
|
19 |
|
$uri, |
79
|
19 |
|
server_request_headers($serverParams), |
80
|
19 |
|
null, // body |
81
|
19 |
|
$serverParams |
82
|
19 |
|
); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|