-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRequest.php
executable file
·207 lines (189 loc) · 5.76 KB
/
Request.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
namespace MaplePHP\Http;
use MaplePHP\Http\Interfaces\RequestInterface;
use MaplePHP\Http\Interfaces\UriInterface;
use MaplePHP\Http\Interfaces\HeadersInterface;
use MaplePHP\Http\Interfaces\StreamInterface;
class Request extends Message implements RequestInterface
{
private $method;
private $uri;
private $requestTarget;
protected $headers;
protected $body;
protected $cliKeywords;
protected $cliArgs;
public function __construct(
string $method,
UriInterface|string $uri,
HeadersInterface|array $headers = [],
StreamInterface|array|string|null $body = null
) {
$this->method = $method; // WHITELIST CASE SENSITIVE UPPERCASE
$this->uri = is_string($uri) ? new Uri($uri) : $uri;
$this->headers = is_array($headers) ? new Headers($headers) : $headers;
$this->body = $this->resolveRequestStream($body);
$this->setHostHeader();
}
/**
* Get the message request target (path+query)
* @return string
*/
public function getRequestTarget(): string
{
$this->requestTarget = $this->getUri()->getPath();
if ($query = $this->getUri()->getQuery()) {
$this->requestTarget .= '?' . $query;
}
return $this->requestTarget;
}
/**
* Return an instance with the specific set requestTarget
* @param mixed $requestTarget
* @return static
*/
public function withRequestTarget(mixed $requestTarget): RequestInterface
{
$inst = clone $this;
$inst->requestTarget = $requestTarget;
return $inst;
}
/**
* Get the message request method (always as upper case)
* @return string
*/
public function getMethod(): string
{
return $this->method;
}
/**
* Return an instance with the specific set Method
* @param string $method
* @return static
*/
public function withMethod(string $method): RequestInterface
{
$inst = clone $this;
$inst->method = strtoupper($method);
return $inst;
}
/**
* Get URI instance with set request message
* @return UriInterface
*/
public function getUri(): UriInterface
{
return $this->uri;
}
/**
* Return an instance with the with a new instance of UriInterface set
* @param UriInterface $uri Instance of UriInterface
* @param boolean $preserveHost Preserve the current request header Host
* @return static
*/
public function withUri(UriInterface $uri, $preserveHost = false): RequestInterface
{
$inst = clone $this;
if ($preserveHost) {
$uri = $uri->withHost($this->getHeaderLine("Host"));
}
$inst->uri = $uri;
return $inst;
}
/**
* Check if is request is SSL
* @return bool
*/
public function isSSL(): bool
{
$https = strtolower($this->env->get("HTTPS"));
return ($https === "on" || $https === "1" || $this->getPort() === 443);
}
/**
* Get Server request port
* @return int
*/
public function getPort(): int
{
$serverPort = $this->env->get("SERVER_PORT");
return (int)(($serverPort) ? $serverPort : $this->uri->getPort());
}
/**
* Set host header if missing or overwrite if custom is set.
* @return void
*/
final protected function setHostHeader(): void
{
if (!$this->headers->hasHeader('Host') || $this->uri->getHost() !== '') {
$this->headers->setHeader('Host', $this->uri->getHost());
}
}
/**
* This will resolve the Request Stream and make the call user-friendly
* @param StreamInterface|array|string|null $body
* @return StreamInterface
*/
private function resolveRequestStream(StreamInterface|array|string|null $body): StreamInterface
{
if ($body instanceof StreamInterface) {
$stream = $body;
} else {
if (is_array($body)) {
$body = http_build_query($body);
}
$stream = new Stream(Stream::TEMP);
if (!is_null($body)) {
$stream->write($body);
$stream->rewind();
}
}
return $stream;
}
/**
* Get Cli keyword
* @return string|null
*/
public function getCliKeyword(): ?string
{
if (is_null($this->cliKeywords)) {
$new = [];
$arg = $this->getUri()->getArgv();
foreach ($arg as $val) {
if (is_string($val)) {
if ((str_starts_with($val, "--")) || (str_starts_with($val, "-"))) {
break;
} else {
$new[] = $val;
}
}
}
array_shift($new);
$this->cliKeywords = implode("/", $new);
}
return $this->cliKeywords;
}
/**
* Get Cli arguments
* @return array
*/
public function getCliArgs(): array
{
if (is_null($this->cliArgs)) {
$args = $this->getUri()->getArgv();
$this->cliArgs = [];
foreach ($args as $arg) {
if (is_string($arg)) {
$arg = str_replace("&", "#", $arg);
if ((($pos1 = strpos($arg, "--")) === 0) || (str_starts_with($arg, "-"))) {
parse_str(substr($arg, ($pos1 !== false ? 2 : 1)), $result);
foreach ($result as &$val) {
$val = str_replace("#", "&", $val);
}
$this->cliArgs = array_merge($this->cliArgs, $result);
}
}
}
}
return $this->cliArgs;
}
}