|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the Shieldon package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Terry L. <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
declare(strict_types=1); |
|
12
|
|
|
|
|
13
|
|
|
namespace Shieldon\Psr17\Utils; |
|
14
|
|
|
|
|
15
|
|
|
use function microtime; |
|
16
|
|
|
use function php_sapi_name; |
|
17
|
|
|
use function str_replace; |
|
18
|
|
|
use function strtolower; |
|
19
|
|
|
use function substr; |
|
20
|
|
|
use function time; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Data Helper |
|
24
|
|
|
*/ |
|
25
|
|
|
class SuperGlobal |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* Extract data from global variables. |
|
29
|
|
|
* |
|
30
|
|
|
* @return array |
|
31
|
|
|
*/ |
|
32
|
8 |
|
public static function extract(): array |
|
33
|
|
|
{ |
|
34
|
8 |
|
if (php_sapi_name() === 'cli') { |
|
35
|
8 |
|
self::mockCliEnvironment(); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
// Here we add the HTTP prefix by ourselves... |
|
39
|
8 |
|
$headerParamsWithoutHttpPrefix = [ |
|
40
|
8 |
|
'CONTENT_TYPE', |
|
41
|
8 |
|
'CONTENT_LENGTH', |
|
42
|
8 |
|
]; |
|
43
|
|
|
|
|
44
|
8 |
|
foreach ($headerParamsWithoutHttpPrefix as $value) { |
|
45
|
8 |
|
if (isset($_SERVER[$value])) { |
|
46
|
8 |
|
$_SERVER['HTTP_' . $value] = $_SERVER[$value]; |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
8 |
|
$headerParams = []; |
|
51
|
8 |
|
$serverParams = $_SERVER ?? []; |
|
52
|
8 |
|
$cookieParams = $_COOKIE ?? []; |
|
53
|
8 |
|
$filesParams = $_FILES ?? []; |
|
54
|
8 |
|
$postParams = $_POST ?? []; |
|
55
|
8 |
|
$getParams = $_GET ?? []; |
|
56
|
|
|
|
|
57
|
8 |
|
foreach ($serverParams as $name => $value) { |
|
58
|
8 |
|
if (substr($name, 0, 5) == 'HTTP_') { |
|
59
|
8 |
|
$key = strtolower(str_replace('_', '-', substr($name, 5))); |
|
60
|
8 |
|
$headerParams[$key] = $value; |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
8 |
|
return [ |
|
65
|
8 |
|
'header' => $headerParams, |
|
66
|
8 |
|
'server' => $serverParams, |
|
67
|
8 |
|
'cookie' => $cookieParams, |
|
68
|
8 |
|
'files' => $filesParams, |
|
69
|
8 |
|
'post' => $postParams, |
|
70
|
8 |
|
'get' => $getParams, |
|
71
|
8 |
|
]; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
// @codeCoverageIgnoreStart |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Mock data for unit testing purpose ONLY. |
|
78
|
|
|
* |
|
79
|
|
|
* @param array $server Overwrite the mock data. |
|
80
|
|
|
* |
|
81
|
|
|
* @return void |
|
82
|
|
|
*/ |
|
83
|
|
|
public static function mockCliEnvironment(array $server = []): void |
|
84
|
|
|
{ |
|
85
|
|
|
$_POST = $_POST ?? []; |
|
86
|
|
|
$_COOKIE = $_COOKIE ?? []; |
|
87
|
|
|
$_GET = $_GET ?? []; |
|
88
|
|
|
$_FILES = $_FILES ?? []; |
|
89
|
|
|
$_SERVER = $_SERVER ?? []; |
|
90
|
|
|
|
|
91
|
|
|
$defaultServerParams = [ |
|
92
|
|
|
'HTTP_ACCEPT' => 'text/html,application/xhtml+xml,application/xml;q=0.9', |
|
93
|
|
|
'HTTP_ACCEPT_CHARSET' => 'ISO-8859-1,utf-8;q=0.7,*;q=0.3', |
|
94
|
|
|
'HTTP_ACCEPT_LANGUAGE' => 'en-US,en;q=0.9,zh-TW;q=0.8,zh;q=0.7', |
|
95
|
|
|
'HTTP_USER_AGENT' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)', |
|
96
|
|
|
'HTTP_HOST' => '127.0.0.1', |
|
97
|
|
|
'QUERY_STRING' => '', |
|
98
|
|
|
'REMOTE_ADDR' => '127.0.0.1', |
|
99
|
|
|
'REQUEST_METHOD' => 'GET', |
|
100
|
|
|
'REQUEST_SCHEME' => 'http', |
|
101
|
|
|
'REQUEST_TIME' => time(), |
|
102
|
|
|
'REQUEST_TIME_FLOAT' => microtime(true), |
|
103
|
|
|
'REQUEST_URI' => '', |
|
104
|
|
|
'SCRIPT_NAME' => '', |
|
105
|
|
|
'SERVER_NAME' => 'localhost', |
|
106
|
|
|
'SERVER_PORT' => 80, |
|
107
|
|
|
'SERVER_PROTOCOL' => 'HTTP/1.1', |
|
108
|
|
|
'CONTENT_TYPE' => 'text/html; charset=UTF-8', |
|
109
|
|
|
]; |
|
110
|
|
|
|
|
111
|
|
|
if (defined('NO_MOCK_ENV')) { |
|
112
|
|
|
$defaultServerParams = []; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
$_SERVER = array_merge($defaultServerParams, $_SERVER, $server); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
// @codeCoverageIgnoreEnd |
|
119
|
|
|
} |
|
120
|
|
|
|