forked from roadrunner-php/http
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGlobalState.php
More file actions
52 lines (40 loc) 路 1.37 KB
/
Copy pathGlobalState.php
File metadata and controls
52 lines (40 loc) 路 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
declare(strict_types=1);
namespace Spiral\RoadRunner\Http;
use function time;
use function microtime;
use function strtoupper;
use function str_replace;
use function implode;
final class GlobalState
{
/**
* Sets ip-address, request-time and other values.
*
* @return non-empty-array<array-key|string, mixed|string>
*/
public static function populateServer(Request $request): array
{
/** @var non-empty-array<array-key|string, mixed|string>|null $originalServer */
static $originalServer = null;
if ($originalServer == null) {
$originalServer = $_SERVER;
}
$newServer = $originalServer;
$newServer['REQUEST_URI'] = $request->uri;
$newServer['REQUEST_TIME'] = time();
$newServer['REQUEST_TIME_FLOAT'] = microtime(true);
$newServer['REMOTE_ADDR'] = $request->getRemoteAddr();
$newServer['REQUEST_METHOD'] = $request->method;
$newServer['HTTP_USER_AGENT'] = '';
foreach ($request->headers as $key => $value) {
$key = strtoupper(str_replace('-', '_', $key));
if ($key == 'CONTENT_TYPE' || $key == 'CONTENT_LENGTH') {
$newServer[$key] = implode(', ', $value);
continue;
}
$newServer['HTTP_' . $key] = implode(', ', $value);
}
return $newServer;
}
}