-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathGlobalState.php
More file actions
54 lines (42 loc) · 1.42 KB
/
GlobalState.php
File metadata and controls
54 lines (42 loc) · 1.42 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
53
54
<?php
declare(strict_types=1);
namespace Spiral\RoadRunner\Http;
final class GlobalState
{
/**
* @var array<array-key, mixed> Cached state of the $_SERVER superglobal.
*/
private static array $cachedServer = [];
/**
* Cache superglobal $_SERVER to avoid state leaks between requests.
*/
public static function cacheServerVars(): void
{
self::$cachedServer = $_SERVER;
}
/**
* Enrich cached $_SERVER with data from the request.
*
* @return non-empty-array<array-key, mixed> Cached $_SERVER data enriched with request data.
*/
public static function enrichServerVars(Request $request): array
{
$server = self::$cachedServer;
$server['REQUEST_URI'] = $request->uri;
$server['REQUEST_TIME'] = \time();
$server['REQUEST_TIME_FLOAT'] = \microtime(true);
$server['REMOTE_ADDR'] = $request->getRemoteAddr();
$server['REQUEST_METHOD'] = $request->method;
$server['HTTP_USER_AGENT'] = '';
foreach ($request->headers as $key => $value) {
$key = \strtoupper(\str_replace('-', '_', $key));
if ($key == 'CONTENT_TYPE' || $key == 'CONTENT_LENGTH') {
$server[$key] = \implode(', ', $value);
continue;
}
$server['HTTP_' . $key] = \implode(', ', $value);
}
return $server;
}
}
GlobalState::cacheServerVars();