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