|
8 | 8 |
|
9 | 9 | namespace chillerlan\HTTP\Psr17; |
10 | 10 |
|
11 | | -use Psr\Http\Message\{ |
12 | | - ServerRequestFactoryInterface, ServerRequestInterface, StreamInterface, UriInterface, UriFactoryInterface |
13 | | -}; |
14 | | -use chillerlan\HTTP\Psr7\{File, Stream}; |
| 11 | +use Psr\Http\Message\StreamInterface; |
| 12 | +use chillerlan\HTTP\Psr7\Stream; |
15 | 13 | use InvalidArgumentException; |
16 | 14 |
|
17 | | -use function explode, function_exists, getallheaders, is_scalar, method_exists, substr; |
| 15 | +use function is_scalar, method_exists; |
18 | 16 |
|
19 | 17 | const PSR17_INCLUDES = true; |
20 | 18 |
|
|
47 | 45 | 'wb' => true, |
48 | 46 | ]; |
49 | 47 |
|
50 | | -/** |
51 | | - * Return a ServerRequest populated with superglobals: |
52 | | - * $_GET |
53 | | - * $_POST |
54 | | - * $_COOKIE |
55 | | - * $_FILES |
56 | | - * $_SERVER |
57 | | - */ |
58 | | -function create_server_request_from_globals( |
59 | | - ServerRequestFactoryInterface $serverRequestFactory, |
60 | | - UriFactoryInterface $uriFactory |
61 | | -):ServerRequestInterface{ |
62 | | - |
63 | | - $serverRequest = $serverRequestFactory->createServerRequest( |
64 | | - $_SERVER['REQUEST_METHOD'] ?? 'GET', |
65 | | - create_uri_from_globals($uriFactory), |
66 | | - $_SERVER |
67 | | - ); |
68 | | - |
69 | | - if(function_exists('getallheaders')){ |
70 | | - foreach(getallheaders() ?: [] as $name => $value){ |
71 | | - $serverRequest = $serverRequest->withHeader($name, $value); |
72 | | - } |
73 | | - } |
74 | | - |
75 | | - return $serverRequest |
76 | | - ->withProtocolVersion(isset($_SERVER['SERVER_PROTOCOL']) ? substr($_SERVER['SERVER_PROTOCOL'], 5) : '1.1') |
77 | | - ->withCookieParams($_COOKIE) |
78 | | - ->withQueryParams($_GET) |
79 | | - ->withParsedBody($_POST) |
80 | | - ->withUploadedFiles(File::normalize($_FILES)) |
81 | | - ; |
82 | | -} |
83 | | - |
84 | | -/** |
85 | | - * Create a Uri populated with values from $_SERVER. |
86 | | - */ |
87 | | -function create_uri_from_globals(UriFactoryInterface $uriFactory):UriInterface{ |
88 | | - $hasPort = false; |
89 | | - $hasQuery = false; |
90 | | - |
91 | | - $uri = $uriFactory->createUri() |
92 | | - ->withScheme(!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' ? 'https' : 'http'); |
93 | | - |
94 | | - if(isset($_SERVER['HTTP_HOST'])){ |
95 | | - $hostHeaderParts = explode(':', $_SERVER['HTTP_HOST']); |
96 | | - $uri = $uri->withHost($hostHeaderParts[0]); |
97 | | - |
98 | | - if(isset($hostHeaderParts[1])){ |
99 | | - $hasPort = true; |
100 | | - $uri = $uri->withPort($hostHeaderParts[1]); |
101 | | - } |
102 | | - } |
103 | | - elseif(isset($_SERVER['SERVER_NAME'])){ |
104 | | - $uri = $uri->withHost($_SERVER['SERVER_NAME']); |
105 | | - } |
106 | | - elseif(isset($_SERVER['SERVER_ADDR'])){ |
107 | | - $uri = $uri->withHost($_SERVER['SERVER_ADDR']); |
108 | | - } |
109 | | - |
110 | | - if(!$hasPort && isset($_SERVER['SERVER_PORT'])){ |
111 | | - $uri = $uri->withPort($_SERVER['SERVER_PORT']); |
112 | | - } |
113 | | - |
114 | | - if(isset($_SERVER['REQUEST_URI'])){ |
115 | | - $requestUriParts = explode('?', $_SERVER['REQUEST_URI']); |
116 | | - $uri = $uri->withPath($requestUriParts[0]); |
117 | | - |
118 | | - if(isset($requestUriParts[1])){ |
119 | | - $hasQuery = true; |
120 | | - $uri = $uri->withQuery($requestUriParts[1]); |
121 | | - } |
122 | | - } |
123 | | - |
124 | | - if(!$hasQuery && isset($_SERVER['QUERY_STRING'])){ |
125 | | - $uri = $uri->withQuery($_SERVER['QUERY_STRING']); |
126 | | - } |
127 | | - |
128 | | - return $uri; |
129 | | -} |
130 | | - |
131 | 48 | /** |
132 | 49 | * Create a new writable stream from a string. |
133 | 50 | * |
|
0 commit comments