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