|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
The MIT License (MIT) |
|
5
|
|
|
|
|
6
|
|
|
Copyright (c) 2015 Vectorface, Inc. |
|
7
|
|
|
|
|
8
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy |
|
9
|
|
|
of this software and associated documentation files (the "Software"), to deal |
|
10
|
|
|
in the Software without restriction, including without limitation the rights |
|
11
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
12
|
|
|
copies of the Software, and to permit persons to whom the Software is |
|
13
|
|
|
furnished to do so, subject to the following conditions: |
|
14
|
|
|
|
|
15
|
|
|
The above copyright notice and this permission notice shall be included in |
|
16
|
|
|
all copies or substantial portions of the Software. |
|
17
|
|
|
|
|
18
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
19
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
20
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
21
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
22
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
23
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
24
|
|
|
THE SOFTWARE. |
|
25
|
|
|
*/ |
|
26
|
|
|
|
|
27
|
|
|
namespace Vectorface\Whip\Request; |
|
28
|
|
|
|
|
29
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Provide IP address data from ta PSR-7 request. |
|
33
|
|
|
*/ |
|
34
|
|
|
class Psr7RequestAdapter implements RequestAdapter |
|
35
|
|
|
{ |
|
36
|
|
|
/** |
|
37
|
|
|
* The PSR-7 request that serves as the source of data. |
|
38
|
|
|
*/ |
|
39
|
|
|
private ServerRequestInterface $request; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* A formatted version of the HTTP headers: ["header" => "value", ...] |
|
43
|
|
|
* |
|
44
|
|
|
* @var string[] |
|
45
|
|
|
*/ |
|
46
|
|
|
private array $headers; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Create a new adapter for a superglobal $_SERVER-style array. |
|
50
|
|
|
* |
|
51
|
|
|
* @param ServerRequestInterface $request An array in a format like PHP's $_SERVER var. |
|
52
|
|
|
*/ |
|
53
|
1 |
|
public function __construct(ServerRequestInterface $request) |
|
54
|
|
|
{ |
|
55
|
1 |
|
$this->request = $request; |
|
56
|
1 |
|
} |
|
57
|
|
|
|
|
58
|
1 |
|
public function getRemoteAddr() : ?string |
|
59
|
|
|
{ |
|
60
|
1 |
|
$server = $this->request->getServerParams(); |
|
61
|
1 |
|
return $server['REMOTE_ADDR'] ?? null; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
1 |
|
public function getHeaders() : array |
|
65
|
|
|
{ |
|
66
|
1 |
|
if (!isset($this->headers)) { |
|
67
|
1 |
|
$this->headers = []; |
|
68
|
1 |
|
foreach ($this->request->getHeaders() as $header => $values) { |
|
69
|
1 |
|
$this->headers[strtolower($header)] = end($values); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
1 |
|
return $this->headers; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|