|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* WebHemi. |
|
4
|
|
|
* |
|
5
|
|
|
* PHP version 5.6 |
|
6
|
|
|
* |
|
7
|
|
|
* @copyright 2012 - 2016 Gixx-web (http://www.gixx-web.com) |
|
8
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License (MIT) |
|
9
|
|
|
* |
|
10
|
|
|
* @link http://www.gixx-web.com |
|
11
|
|
|
*/ |
|
12
|
|
|
namespace WebHemi\Adapter\Http\GuzzleHttp; |
|
13
|
|
|
|
|
14
|
|
|
use GuzzleHttp\Psr7\LazyOpenStream; |
|
15
|
|
|
use GuzzleHttp\Psr7\Uri; |
|
16
|
|
|
use WebHemi\Adapter\Http\HttpAdapterInterface; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Class GuzzleHttpAdapter. |
|
20
|
|
|
*/ |
|
21
|
|
|
class GuzzleHttpAdapter implements HttpAdapterInterface |
|
22
|
|
|
{ |
|
23
|
|
|
/** @var ServerRequest */ |
|
24
|
|
|
private $request; |
|
25
|
|
|
/** @var Response */ |
|
26
|
|
|
private $response; |
|
27
|
|
|
|
|
28
|
|
|
/** @var array */ |
|
29
|
|
|
private $get; |
|
30
|
|
|
/** @var array */ |
|
31
|
|
|
private $post; |
|
32
|
|
|
/** @var array */ |
|
33
|
|
|
private $server; |
|
34
|
|
|
/** @var array */ |
|
35
|
|
|
private $cookie; |
|
36
|
|
|
/** @var array */ |
|
37
|
|
|
private $files; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* GuzzleHTTPAdapter constructor. |
|
41
|
|
|
* |
|
42
|
|
|
* @param array $get |
|
43
|
|
|
* @param array $post |
|
44
|
|
|
* @param array $server |
|
45
|
|
|
* @param array $cookie |
|
46
|
|
|
* @param array $files |
|
47
|
|
|
*/ |
|
48
|
8 |
|
public function __construct(array $get, array $post, array $server, array $cookie, array $files) |
|
49
|
|
|
{ |
|
50
|
8 |
|
$this->get = $get; |
|
51
|
8 |
|
$this->post = $post; |
|
52
|
8 |
|
$this->server = $server; |
|
53
|
8 |
|
$this->cookie = $cookie; |
|
54
|
8 |
|
$this->files = $files; |
|
55
|
|
|
|
|
56
|
8 |
|
$this->initialize(); |
|
57
|
8 |
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Initialize adapter: create the ServerRequest and Response instances. |
|
61
|
|
|
*/ |
|
62
|
8 |
|
private function initialize() |
|
63
|
|
|
{ |
|
64
|
8 |
|
$uri = new Uri(''); |
|
65
|
8 |
|
$uri = $uri->withScheme($this->getScheme()) |
|
66
|
8 |
|
->withHost($this->getHost()) |
|
67
|
8 |
|
->withPort($this->getServerData('SERVER_PORT', 80)) |
|
68
|
8 |
|
->withPath($this->getRequestUri()) |
|
69
|
8 |
|
->withQuery($this->getServerData('QUERY_STRING', '')); |
|
70
|
|
|
|
|
71
|
8 |
|
$serverRequest = new ServerRequest( |
|
72
|
8 |
|
$this->getServerData('REQUEST_METHOD', 'GET'), |
|
73
|
8 |
|
$uri, |
|
74
|
8 |
|
[], |
|
75
|
8 |
|
new LazyOpenStream('php://input', 'r+'), |
|
76
|
8 |
|
$this->getProtocol(), |
|
77
|
8 |
|
$this->server |
|
78
|
8 |
|
); |
|
79
|
8 |
|
$this->request = $serverRequest |
|
80
|
8 |
|
->withCookieParams($this->cookie) |
|
81
|
8 |
|
->withQueryParams($this->get) |
|
82
|
8 |
|
->withParsedBody($this->post); |
|
83
|
|
|
// Create a Response with HTTP 102 - Processing. |
|
84
|
8 |
|
$this->response = new Response(Response::STATUS_PROCESSING); |
|
85
|
8 |
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Gets the specific server data, or a default value if not present. |
|
89
|
|
|
* |
|
90
|
|
|
* @param string $keyName |
|
91
|
|
|
* @param mixed $defaultValue |
|
92
|
|
|
* |
|
93
|
|
|
* @return string |
|
94
|
|
|
*/ |
|
95
|
8 |
|
private function getServerData($keyName, $defaultValue = '') |
|
96
|
|
|
{ |
|
97
|
8 |
|
if (isset($this->server[$keyName])) { |
|
98
|
8 |
|
$defaultValue = $this->server[$keyName]; |
|
99
|
8 |
|
} |
|
100
|
|
|
|
|
101
|
8 |
|
return (string) $defaultValue; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Gets server scheme. |
|
106
|
|
|
* |
|
107
|
|
|
* @return string |
|
108
|
|
|
*/ |
|
109
|
8 |
|
private function getScheme() |
|
110
|
|
|
{ |
|
111
|
8 |
|
$scheme = 'http'; |
|
112
|
8 |
|
$https = $this->getServerData('HTTPS', 'off'); |
|
113
|
|
|
|
|
114
|
8 |
|
if ($https == 'on') { |
|
115
|
1 |
|
$scheme = 'https'; |
|
116
|
1 |
|
} |
|
117
|
|
|
|
|
118
|
8 |
|
return $scheme; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Gets the server host name. |
|
123
|
|
|
* |
|
124
|
|
|
* @return string |
|
125
|
|
|
*/ |
|
126
|
8 |
|
private function getHost() |
|
127
|
|
|
{ |
|
128
|
8 |
|
$host = $this->getServerData('HTTP_HOST'); |
|
129
|
8 |
|
$name = $this->getServerData('SERVER_NAME'); |
|
130
|
|
|
|
|
131
|
8 |
|
if (empty($host) && !empty($name)) { |
|
132
|
1 |
|
$host = $name; |
|
133
|
1 |
|
} |
|
134
|
|
|
|
|
135
|
8 |
|
return (string) preg_replace('/:[0-9]+$/', '', $host); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Gets the server request uri. |
|
140
|
|
|
* |
|
141
|
|
|
* @return string |
|
142
|
|
|
*/ |
|
143
|
8 |
|
private function getRequestUri() |
|
144
|
|
|
{ |
|
145
|
8 |
|
$requestUri = $this->getServerData('REQUEST_URI', '/'); |
|
146
|
|
|
|
|
147
|
8 |
|
return (string) current(explode('?', $requestUri)); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Gets the server protocol. |
|
152
|
|
|
* |
|
153
|
|
|
* @return string |
|
154
|
|
|
*/ |
|
155
|
8 |
|
private function getProtocol() |
|
156
|
|
|
{ |
|
157
|
8 |
|
$protocol = '1.1'; |
|
158
|
8 |
|
$serverProtocol = $this->getServerData('SERVER_PROTOCOL'); |
|
159
|
|
|
|
|
160
|
8 |
|
if (!empty($serverProtocol)) { |
|
161
|
1 |
|
$protocol = str_replace('HTTP/', '', $serverProtocol); |
|
162
|
1 |
|
} |
|
163
|
|
|
|
|
164
|
8 |
|
return (string) $protocol; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Returns the HTTP request. |
|
169
|
|
|
* |
|
170
|
|
|
* @return ServerRequest |
|
171
|
|
|
*/ |
|
172
|
5 |
|
public function getRequest() |
|
173
|
|
|
{ |
|
174
|
5 |
|
return $this->request; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Returns the response being sent. |
|
179
|
|
|
* |
|
180
|
|
|
* @return Response |
|
181
|
|
|
*/ |
|
182
|
5 |
|
public function getResponse() |
|
183
|
|
|
{ |
|
184
|
5 |
|
return $this->response; |
|
185
|
|
|
} |
|
186
|
|
|
} |
|
187
|
|
|
|