1
|
|
|
<?php |
2
|
|
|
namespace DevOp\Core\Http; |
3
|
|
|
|
4
|
|
|
use Psr\Http\Message\UriInterface; |
5
|
|
|
use Psr\Http\Message\StreamInterface; |
6
|
|
|
use Psr\Http\Message\RequestInterface; |
7
|
|
|
|
8
|
|
|
class Request implements RequestInterface |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
use MessageTrait; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
private $method; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
private $requestTarget; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var UriInterface |
25
|
|
|
*/ |
26
|
|
|
private $uri; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* |
30
|
|
|
* @param string $method |
31
|
|
|
* @param UriInterface $uri |
32
|
|
|
* @param array $headers |
33
|
|
|
* @param StreamInterface|null $body |
34
|
|
|
* @param string $protocolVersion |
35
|
|
|
*/ |
36
|
82 |
|
public function __construct($method, UriInterface $uri, array $headers = [], $body = null, $protocolVersion = '1.1') |
37
|
|
|
{ |
38
|
82 |
|
$this->method = $method; |
39
|
82 |
|
$this->uri = $uri; |
40
|
82 |
|
$this->body = $body; |
41
|
|
|
|
42
|
82 |
|
foreach ($headers AS $header => $value) { |
43
|
14 |
|
$this->headersName[strtolower($header)] = $header; |
44
|
14 |
|
$this->headers[$header] = !is_array($value) ? [$value] : $value; |
45
|
41 |
|
} |
46
|
|
|
|
47
|
82 |
|
if (!$this->hasHeader('Host')) { |
48
|
82 |
|
if (!$uri = $this->uri->getHost()) { |
49
|
22 |
|
$uri = 'localhost'; |
50
|
11 |
|
} |
51
|
82 |
|
$this->headersName['host'] = 'Host'; |
52
|
82 |
|
$this->headers['Host'] = [$uri]; |
53
|
41 |
|
} |
54
|
|
|
|
55
|
82 |
|
$this->protocolVersion = $protocolVersion; |
56
|
82 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
54 |
|
public function getMethod() |
62
|
|
|
{ |
63
|
54 |
|
return $this->method; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param string $method |
68
|
|
|
* @return self |
69
|
|
|
*/ |
70
|
2 |
|
public function withMethod($method) |
71
|
|
|
{ |
72
|
2 |
|
if ($method === $this->method) { |
73
|
|
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
2 |
|
$clone = clone $this; |
77
|
2 |
|
$clone->method = $method; |
78
|
|
|
|
79
|
2 |
|
return $clone; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return string |
84
|
|
|
*/ |
85
|
|
|
public function getRequestTarget() |
86
|
|
|
{ |
87
|
|
|
|
88
|
|
|
if ($this->requestTarget) { |
89
|
|
|
return $this->requestTarget; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$target = $this->uri->getPath(); |
93
|
|
|
|
94
|
|
|
if ($target === '') { |
95
|
|
|
$target .= '/'; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
if ($this->uri->getQuery()) { |
99
|
|
|
$target .= '?' . $this->uri->getQuery(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $target; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param string $requestTarget |
107
|
|
|
* @return self |
108
|
|
|
*/ |
109
|
|
|
public function withRequestTarget($requestTarget) |
110
|
|
|
{ |
111
|
|
|
if ($requestTarget === $this->requestTarget) { |
112
|
|
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$clone = clone $this; |
116
|
|
|
$clone->requestTarget = $requestTarget; |
117
|
|
|
|
118
|
|
|
return $clone; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @return UriInterface |
123
|
|
|
*/ |
124
|
50 |
|
public function getUri() |
125
|
|
|
{ |
126
|
50 |
|
return $this->uri; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param UriInterface $uri |
131
|
|
|
* @param boolean $preserveHost |
132
|
|
|
* @return self |
133
|
|
|
*/ |
134
|
|
|
public function withUri(UriInterface $uri, $preserveHost = false) |
135
|
|
|
{ |
136
|
|
|
if ($uri === $this->uri) { |
137
|
|
|
return $this; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
$clone = clone $this; |
141
|
|
|
$clone->uri = $uri; |
142
|
|
|
|
143
|
|
|
if ($preserveHost) { |
144
|
|
|
if (!$clone->hasHeader('host')) { |
145
|
|
|
$clone->headers['host'] = 'Host'; |
146
|
|
|
$clone->headers['Host'] = [$uri->getHost()]; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return $clone; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|