1
|
|
|
<?php |
2
|
|
|
namespace DevOp\Core\Http; |
3
|
|
|
|
4
|
|
|
use Psr\Http\Message\UriInterface; |
5
|
|
|
|
6
|
|
|
class Uri implements UriInterface |
7
|
|
|
{ |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @var array |
11
|
|
|
*/ |
12
|
|
|
private static $schemes = array( |
13
|
|
|
'http' => 80, |
14
|
|
|
'https' => 443 |
15
|
|
|
); |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var string|null |
19
|
|
|
*/ |
20
|
|
|
private $scheme; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string|null |
24
|
|
|
*/ |
25
|
|
|
private $userInfo; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string|null |
29
|
|
|
*/ |
30
|
|
|
private $host; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var int|null |
34
|
|
|
*/ |
35
|
|
|
private $port; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
private $path = ''; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var string|null |
44
|
|
|
*/ |
45
|
|
|
private $query; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var string|null |
49
|
|
|
*/ |
50
|
|
|
private $fragment; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return \DevOp\Core\Http\Uri |
54
|
|
|
*/ |
55
|
|
|
public static function createFromGlobals() |
56
|
|
|
{ |
57
|
|
|
|
58
|
|
|
$uri = new Uri(); |
59
|
|
|
|
60
|
|
|
$scheme = 'http'; |
61
|
|
|
if (isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on') { |
62
|
|
|
$scheme .= 's'; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$uri->withScheme($scheme); |
66
|
|
|
|
67
|
|
|
if (isset($_SERVER['REMOTE_PORT']) && (!isset(self::$schemes['http']) && !isset(self::$schemes['https']))) { |
68
|
|
|
$uri->withPort($_SERVER['REMOTE_PORT']); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
if (isset($_SERVER['SERVER_NAME'])) { |
72
|
|
|
$host = $_SERVER['SERVER_NAME']; |
73
|
|
|
} else if (isset($_SERVER['SERVER_ADDR'])) { |
74
|
|
|
$host = $_SERVER['SERVER_ADDR']; |
75
|
|
|
} else { |
76
|
|
|
$host = '127.0.0.1'; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$uri->withHost($host); |
80
|
|
|
|
81
|
|
|
if (isset($_SERVER['QUERY_STRING'])) { |
82
|
|
|
$uri->withQuery(ltrim($_SERVER['QUERY_STRING'], '?')); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $uri; |
86
|
|
|
} |
87
|
|
|
|
88
|
20 |
|
public function __construct($uri = '') |
89
|
|
|
{ |
90
|
|
|
|
91
|
20 |
|
if ($uri instanceof UriInterface) { |
92
|
|
|
return $uri; |
93
|
|
|
} |
94
|
|
|
|
95
|
20 |
|
if (!is_string($uri)) { |
96
|
|
|
throw new \InvalidArgumentException('Argument must be a string.'); |
97
|
|
|
} |
98
|
|
|
|
99
|
20 |
|
$components = parse_url($uri); |
100
|
20 |
|
if (isset($components['scheme'])) { |
101
|
14 |
|
$this->scheme = $components['scheme']; |
102
|
|
|
} |
103
|
20 |
|
if (isset($components['host'])) { |
104
|
14 |
|
$this->host = $components['host']; |
105
|
|
|
} |
106
|
20 |
|
if (isset($components['port']) && !in_array($components['port'], [self::$schemes['http'], self::$schemes['https']])) { |
107
|
14 |
|
$this->port = $components['port']; |
108
|
|
|
} |
109
|
20 |
|
if (isset($components['path'])) { |
110
|
20 |
|
$this->path = $components['path']; |
111
|
|
|
} |
112
|
20 |
|
if (isset($components['query'])) { |
113
|
14 |
|
$this->query = $components['query']; |
114
|
|
|
} |
115
|
20 |
|
if (isset($components['fragment'])) { |
116
|
14 |
|
$this->fragment = $components['fragment']; |
117
|
|
|
} |
118
|
20 |
|
if (isset($components['user'])) { |
119
|
14 |
|
$this->userInfo = $components['user']; |
120
|
|
|
} |
121
|
20 |
|
if (isset($components['pass'])) { |
122
|
14 |
|
$this->userInfo .= ':' . $components['pass']; |
123
|
|
|
} |
124
|
20 |
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return string |
128
|
|
|
*/ |
129
|
|
|
public function __toString() |
130
|
|
|
{ |
131
|
|
|
$uri = $this->scheme; |
132
|
|
|
|
133
|
|
|
if ($this->userInfo !== null) { |
134
|
|
|
$uri .= "//{$this->userInfo}"; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
if ($this->port !== null && !in_array($this->port, [self::$schemes['http'], self::$schemes['https']])) { |
138
|
|
|
$uri .= ":{$this->port}"; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$uri .= $this->path; |
142
|
|
|
|
143
|
|
|
if ($this->query !== null) { |
144
|
|
|
$uri .= "?{$this->query}"; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
if ($this->fragment !== null) { |
148
|
|
|
$uri .= "#{$this->fragment}"; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return $uri; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @return string |
156
|
|
|
*/ |
157
|
|
|
public function getAuthority() |
158
|
|
|
{ |
159
|
|
|
return ($this->userInfo ?: $this->userInfo) . $this->host . ($this->port ?: ":{$this->port}"); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @return string |
164
|
|
|
*/ |
165
|
2 |
|
public function getFragment() |
166
|
|
|
{ |
167
|
2 |
|
return $this->fragment; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @return string |
172
|
|
|
*/ |
173
|
4 |
|
public function getHost() |
174
|
|
|
{ |
175
|
4 |
|
return $this->host; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @return string |
180
|
|
|
*/ |
181
|
4 |
|
public function getPath() |
182
|
|
|
{ |
183
|
4 |
|
return $this->path; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @return null|int |
188
|
|
|
*/ |
189
|
4 |
|
public function getPort() |
190
|
|
|
{ |
191
|
4 |
|
return $this->port; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @return string |
196
|
|
|
*/ |
197
|
4 |
|
public function getQuery() |
198
|
|
|
{ |
199
|
4 |
|
return $this->query; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @return string |
204
|
|
|
*/ |
205
|
4 |
|
public function getScheme() |
206
|
|
|
{ |
207
|
4 |
|
return $this->scheme; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @return string |
212
|
|
|
*/ |
213
|
4 |
|
public function getUserInfo() |
214
|
|
|
{ |
215
|
4 |
|
return $this->userInfo; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @param string $fragment |
220
|
|
|
* @return \DevOp\Core\Http\Uri|$this |
221
|
|
|
*/ |
222
|
|
|
public function withFragment($fragment) |
223
|
|
|
{ |
224
|
|
|
if ($fragment === $this->fragment) { |
225
|
|
|
return $this; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
$clone = clone $this; |
229
|
|
|
$clone->fragment = $fragment; |
230
|
|
|
|
231
|
|
|
return $clone; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* @param string $host |
236
|
|
|
* @return \DevOp\Core\Http\Uri|$this |
237
|
|
|
*/ |
238
|
2 |
|
public function withHost($host) |
239
|
|
|
{ |
240
|
2 |
|
if ($host === $this->host) { |
241
|
|
|
return $this; |
242
|
|
|
} |
243
|
|
|
|
244
|
2 |
|
$clone = clone $this; |
245
|
2 |
|
$clone->host = $host; |
246
|
|
|
|
247
|
2 |
|
return $clone; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* @param string $path |
252
|
|
|
* @return \DevOp\Core\Http\Uri|$this |
253
|
|
|
*/ |
254
|
2 |
|
public function withPath($path) |
255
|
|
|
{ |
256
|
2 |
|
if ($path === $this->path) { |
257
|
|
|
return $this; |
258
|
|
|
} |
259
|
|
|
|
260
|
2 |
|
$clone = clone $this; |
261
|
2 |
|
$clone->path = $path; |
262
|
|
|
|
263
|
2 |
|
return $clone; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* @param int $port |
268
|
|
|
* @return \DevOp\Core\Http\Uri|$this |
269
|
|
|
*/ |
270
|
2 |
|
public function withPort($port) |
271
|
|
|
{ |
272
|
2 |
|
if ($port === $this->port) { |
273
|
|
|
return $this; |
274
|
|
|
} |
275
|
|
|
|
276
|
2 |
|
$clone = clone $this; |
277
|
2 |
|
$clone->port = $port; |
278
|
|
|
|
279
|
2 |
|
return $clone; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* @param string $query |
284
|
|
|
* @return \DevOp\Core\Http\Uri|$this |
285
|
|
|
*/ |
286
|
2 |
|
public function withQuery($query) |
287
|
|
|
{ |
288
|
2 |
|
if ($query === $this->query) { |
289
|
|
|
return $this; |
290
|
|
|
} |
291
|
|
|
|
292
|
2 |
|
$clone = clone $this; |
293
|
2 |
|
$clone->query = $query; |
294
|
|
|
|
295
|
2 |
|
return $clone; |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* @param string $scheme |
300
|
|
|
* @return \DevOp\Core\Http\Uri|$this |
301
|
|
|
*/ |
302
|
2 |
|
public function withScheme($scheme) |
303
|
|
|
{ |
304
|
2 |
|
if ($scheme === $this->scheme) { |
305
|
|
|
return $this; |
306
|
|
|
} |
307
|
|
|
|
308
|
2 |
|
$clone = clone $this; |
309
|
2 |
|
$clone->scheme = $scheme; |
310
|
|
|
|
311
|
2 |
|
return $clone; |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* @param string $user |
316
|
|
|
* @param string|null $password |
317
|
|
|
* @return \DevOp\Core\Http\Uri|$this |
318
|
|
|
*/ |
319
|
2 |
|
public function withUserInfo($user, $password = null) |
320
|
|
|
{ |
321
|
2 |
|
$userInfo = $user; |
322
|
2 |
|
if (null !== $password) { |
323
|
|
|
$userInfo .= ":{$password}"; |
324
|
|
|
} |
325
|
|
|
|
326
|
2 |
|
if ($userInfo === $this->userInfo) { |
327
|
|
|
return $this; |
328
|
|
|
} |
329
|
|
|
|
330
|
2 |
|
$clone = clone $this; |
331
|
2 |
|
$clone->userInfo = $userInfo; |
332
|
|
|
|
333
|
2 |
|
return $clone; |
334
|
|
|
} |
335
|
|
|
} |
336
|
|
|
|