|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Almendra\Http\Psr\Messages; |
|
4
|
|
|
|
|
5
|
|
|
use Almendra\Http\Collection; |
|
6
|
|
|
use Almendra\Http\Psr\Interfaces\EnvironmentInterface; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Represents the message environment. |
|
10
|
|
|
* |
|
11
|
|
|
* @package Almendra\Http |
|
12
|
|
|
*/ |
|
13
|
|
|
class Environment extends Collection implements EnvironmentInterface |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* Create mock environment |
|
17
|
|
|
* |
|
18
|
|
|
* @param array $userData Array of custom environment keys and values |
|
19
|
|
|
* @return array |
|
20
|
|
|
*/ |
|
21
|
|
|
public static function mock(array $userData = []) |
|
22
|
|
|
{ |
|
23
|
|
|
$data = array_merge([ |
|
24
|
|
|
'SERVER_PROTOCOL' => 'HTTP/1.1', |
|
25
|
|
|
'REQUEST_METHOD' => 'GET', |
|
26
|
|
|
'SCRIPT_NAME' => '', |
|
27
|
|
|
'REQUEST_URI' => '', |
|
28
|
|
|
'QUERY_STRING' => '', |
|
29
|
|
|
'SERVER_NAME' => 'localhost', |
|
30
|
|
|
'SERVER_PORT' => 8000, |
|
31
|
|
|
'HTTP_HOST' => 'localhost', |
|
32
|
|
|
'HTTP_ACCEPT' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', |
|
33
|
|
|
'HTTP_ACCEPT_LANGUAGE' => 'en-US,en;q=0.8', |
|
34
|
|
|
'HTTP_ACCEPT_CHARSET' => 'ISO-8859-1,utf-8;q=0.7,*;q=0.3', |
|
35
|
|
|
'HTTP_USER_AGENT' => 'Gate Framework', |
|
36
|
|
|
'REMOTE_ADDR' => '127.0.0.1', |
|
37
|
|
|
'REQUEST_TIME' => time(), |
|
38
|
|
|
'REQUEST_TIME_FLOAT' => microtime(true), |
|
39
|
|
|
], $userData); |
|
40
|
|
|
|
|
41
|
|
|
return $data; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Create environment from the global variables |
|
46
|
|
|
* |
|
47
|
|
|
* @param array $userData Array of custom environment keys and values |
|
48
|
|
|
* @return array |
|
49
|
|
|
*/ |
|
50
|
|
|
public static function init(array $userData = []) |
|
|
|
|
|
|
51
|
|
|
{ |
|
52
|
|
|
$data = array_merge([ |
|
53
|
|
|
'SERVER_PROTOCOL' => 'HTTP/1.1', |
|
54
|
|
|
'REQUEST_METHOD' => $_SERVER['REQUEST_METHOD'], |
|
55
|
|
|
'SCRIPT_NAME' => $_SERVER['SCRIPT_NAME'], |
|
56
|
|
|
'REQUEST_URI' => $_SERVER['REQUEST_URI'], |
|
57
|
|
|
// 'QUERY_STRING' => URIHelper::getQueryParams($_SERVER['REQUEST_URI']), |
|
|
|
|
|
|
58
|
|
|
'QUERY_STRING' => self::getServerValue('QUERY_STRING'), |
|
59
|
|
|
'SERVER_NAME' => $_SERVER['SERVER_NAME'], |
|
60
|
|
|
'SERVER_PORT' => $_SERVER['SERVER_PORT'], |
|
61
|
|
|
'HTTP_HOST' => $_SERVER['HTTP_HOST'], |
|
62
|
|
|
'HTTP_ACCEPT' => $_SERVER['HTTP_ACCEPT'], |
|
63
|
|
|
'HTTP_ACCEPT_LANGUAGE' => self::getServerValue('HTTP_ACCEPT_LANGUAGE', 'en'), |
|
64
|
|
|
'HTTP_ACCEPT_CHARSET' => self::getServerValue('HTTP_ACCEPT_CHARSET', 'ISO-8859-1,utf-8;q=0.7,*;q=0.3'), |
|
65
|
|
|
'HTTP_USER_AGENT' => $_SERVER['HTTP_USER_AGENT'], |
|
66
|
|
|
'REMOTE_ADDR' => self::getServerValue('REMOTE_ADDR'), |
|
67
|
|
|
'REQUEST_TIME' => time(), |
|
68
|
|
|
'REQUEST_TIME_FLOAT' => microtime(true), |
|
69
|
|
|
], $userData); |
|
70
|
|
|
|
|
71
|
|
|
return $data; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public static function getServerValue($value, $default = '') |
|
|
|
|
|
|
75
|
|
|
{ |
|
76
|
|
|
if (array_key_exists($value, $_SERVER)) { |
|
77
|
|
|
return $_SERVER[$value]; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
return $default; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: