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