1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Limoncello\Core\Application; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Copyright 2015-2020 [email protected] |
7
|
|
|
* |
8
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
9
|
|
|
* you may not use this file except in compliance with the License. |
10
|
|
|
* You may obtain a copy of the License at |
11
|
|
|
* |
12
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
13
|
|
|
* |
14
|
|
|
* Unless required by applicable law or agreed to in writing, software |
15
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
16
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
17
|
|
|
* See the License for the specific language governing permissions and |
18
|
|
|
* limitations under the License. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
use Laminas\HttpHandlerRunner\Emitter\EmitterInterface; |
22
|
|
|
use Limoncello\Contracts\Core\SapiInterface; |
23
|
|
|
use Psr\Http\Message\ResponseInterface; |
24
|
|
|
use Psr\Http\Message\StreamInterface; |
25
|
|
|
use Psr\Http\Message\UriInterface; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @package Limoncello\Core |
29
|
|
|
*/ |
30
|
|
|
class Sapi implements SapiInterface |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* @var EmitterInterface |
34
|
|
|
*/ |
35
|
|
|
private $sapiEmitter; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
private $server; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var array |
44
|
|
|
*/ |
45
|
|
|
private $files; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var array |
49
|
|
|
*/ |
50
|
|
|
private $headers; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var UriInterface |
54
|
|
|
*/ |
55
|
|
|
private $uri; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var string |
59
|
|
|
*/ |
60
|
|
|
private $method; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var array |
64
|
|
|
*/ |
65
|
|
|
private $cookies; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @var array |
69
|
|
|
*/ |
70
|
|
|
private $queryParams; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @var array|object |
74
|
|
|
*/ |
75
|
|
|
private $parsedBody; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @var string|resource|StreamInterface |
79
|
|
|
*/ |
80
|
|
|
private $messageBody; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @var string |
84
|
|
|
*/ |
85
|
|
|
private $protocolVersion; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Sapi constructor. |
89
|
|
|
* |
90
|
|
|
* @param EmitterInterface $sapiEmitter |
91
|
|
|
* @param array|null $server |
92
|
|
|
* @param array|null $queryParams |
93
|
|
|
* @param array|object|null $parsedBody |
94
|
|
|
* @param array|null $cookies |
95
|
|
|
* @param array|null $files |
96
|
|
|
* @param string|resource|StreamInterface $messageBody |
97
|
|
|
* @param string $protocolVersion |
98
|
|
|
* |
99
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
100
|
|
|
* @SuppressWarnings(PHPMD.Superglobals) |
101
|
|
|
*/ |
102
|
9 |
|
public function __construct( |
103
|
|
|
EmitterInterface $sapiEmitter, |
104
|
|
|
array $server = null, |
105
|
|
|
array $queryParams = null, |
106
|
|
|
array $parsedBody = null, |
107
|
|
|
array $cookies = null, |
108
|
|
|
array $files = null, |
109
|
|
|
$messageBody = 'php://input', |
110
|
|
|
string $protocolVersion = '1.1' |
111
|
|
|
) { |
112
|
9 |
|
$this->sapiEmitter = $sapiEmitter; |
113
|
|
|
|
114
|
|
|
// Code below based on ServerRequestFactory::fromGlobals |
115
|
9 |
|
$this->server = \Zend\Diactoros\normalizeServer($server ?? $_SERVER); |
|
|
|
|
116
|
9 |
|
$this->files = \Zend\Diactoros\normalizeUploadedFiles($files ?? $_FILES); |
|
|
|
|
117
|
9 |
|
$this->headers = \Zend\Diactoros\marshalHeadersFromSapi($this->server); |
|
|
|
|
118
|
9 |
|
$this->uri = \Zend\Diactoros\marshalUriFromSapi($this->server, $this->headers); |
|
|
|
|
119
|
9 |
|
$this->method = \Zend\Diactoros\marshalMethodFromSapi($this->server); |
|
|
|
|
120
|
9 |
|
$this->cookies = $cookies ?? $_COOKIE; |
121
|
9 |
|
$this->queryParams = $queryParams ?? $_GET; |
122
|
9 |
|
$this->parsedBody = $parsedBody ?? $_POST; |
123
|
9 |
|
$this->messageBody = $messageBody; |
124
|
9 |
|
$this->protocolVersion = $protocolVersion; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @inheritdoc |
129
|
|
|
*/ |
130
|
7 |
|
public function getServer(): array |
131
|
|
|
{ |
132
|
7 |
|
return $this->server; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @inheritdoc |
137
|
|
|
*/ |
138
|
7 |
|
public function getFiles(): array |
139
|
|
|
{ |
140
|
7 |
|
return $this->files; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @inheritdoc |
145
|
|
|
*/ |
146
|
7 |
|
public function getHeaders(): array |
147
|
|
|
{ |
148
|
7 |
|
return $this->headers; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @inheritdoc |
153
|
|
|
*/ |
154
|
9 |
|
public function getUri(): UriInterface |
155
|
|
|
{ |
156
|
9 |
|
return $this->uri; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @inheritdoc |
161
|
|
|
*/ |
162
|
9 |
|
public function getMethod(): string |
163
|
|
|
{ |
164
|
9 |
|
return $this->method; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @inheritdoc |
169
|
|
|
*/ |
170
|
7 |
|
public function getCookies(): array |
171
|
|
|
{ |
172
|
7 |
|
return $this->cookies; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @inheritdoc |
177
|
|
|
*/ |
178
|
7 |
|
public function getQueryParams(): array |
179
|
|
|
{ |
180
|
7 |
|
return $this->queryParams; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @inheritdoc |
185
|
|
|
*/ |
186
|
7 |
|
public function getParsedBody() |
187
|
|
|
{ |
188
|
7 |
|
return $this->parsedBody; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @inheritdoc |
193
|
|
|
*/ |
194
|
7 |
|
public function getRequestBody() |
195
|
|
|
{ |
196
|
7 |
|
return $this->messageBody; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @inheritdoc |
201
|
|
|
*/ |
202
|
7 |
|
public function getProtocolVersion(): string |
203
|
|
|
{ |
204
|
7 |
|
return $this->protocolVersion; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @inheritdoc |
209
|
|
|
*/ |
210
|
1 |
|
public function handleResponse(ResponseInterface $response): void |
211
|
|
|
{ |
212
|
1 |
|
$this->sapiEmitter->emit($response); |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|
This function has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.