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