-
-
Notifications
You must be signed in to change notification settings - Fork 166
/
Uri.php
514 lines (435 loc) · 9.84 KB
/
Uri.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
<?php
namespace Kirby\Http;
use Kirby\Cms\App;
use Kirby\Exception\InvalidArgumentException;
use SensitiveParameter;
use Throwable;
/**
* Uri builder class
*
* @package Kirby Http
* @author Bastian Allgeier <bastian@getkirby.com>
* @link https://getkirby.com
* @copyright Bastian Allgeier
* @license https://opensource.org/licenses/MIT
*/
class Uri
{
/**
* Cache for the current Uri object
*/
public static Uri|null $current = null;
/**
* The fragment after the hash
*/
protected string|false|null $fragment;
/**
* The host address
*/
protected string|null $host;
/**
* The optional password for basic authentication
*/
protected string|false|null $password;
/**
* The optional list of params
*/
protected Params $params;
/**
* The optional path
*/
protected Path $path;
/**
* The optional port number
*/
protected int|false|null $port;
/**
* All original properties
*/
protected array $props;
/**
* The optional query string without leading ?
*/
protected Query $query;
/**
* https or http
*/
protected string|null $scheme;
/**
* Supported schemes
*/
protected static array $schemes = ['http', 'https', 'ftp'];
protected bool $slash;
/**
* The optional username for basic authentication
*/
protected string|false|null $username = null;
/**
* Creates a new URI object
*
* @param array $inject Additional props to inject if a URL string is passed
*/
public function __construct(array|string $props = [], array $inject = [])
{
if (is_string($props) === true) {
$props = parse_url($props);
$props['username'] = $props['user'] ?? null;
$props['password'] = $props['pass'] ?? null;
$props = array_merge($props, $inject);
}
// parse the path and extract params
if (empty($props['path']) === false) {
$props = static::parsePath($props);
}
$this->props = $props;
$this->setFragment($props['fragment'] ?? null);
$this->setHost($props['host'] ?? null);
$this->setParams($props['params'] ?? null);
$this->setPassword($props['password'] ?? null);
$this->setPath($props['path'] ?? null);
$this->setPort($props['port'] ?? null);
$this->setQuery($props['query'] ?? null);
$this->setScheme($props['scheme'] ?? 'http');
$this->setSlash($props['slash'] ?? false);
$this->setUsername($props['username'] ?? null);
}
/**
* Magic caller to access all properties
*/
public function __call(string $property, array $arguments = [])
{
return $this->$property ?? null;
}
/**
* Make sure that cloning also clones
* the path and query objects
*/
public function __clone()
{
$this->path = clone $this->path;
$this->query = clone $this->query;
$this->params = clone $this->params;
}
/**
* Magic getter
*/
public function __get(string $property)
{
return $this->$property ?? null;
}
/**
* Magic setter
*/
public function __set(string $property, $value): void
{
if (method_exists($this, 'set' . $property) === true) {
$this->{'set' . $property}($value);
}
}
/**
* Converts the URL object to string
*/
public function __toString(): string
{
try {
return $this->toString();
} catch (Throwable) {
return '';
}
}
/**
* Returns the auth details (username:password)
*/
public function auth(): string|null
{
$auth = trim($this->username . ':' . $this->password);
return $auth !== ':' ? $auth : null;
}
/**
* Returns the base url (scheme + host)
* without trailing slash
*/
public function base(): string|null
{
if ($domain = $this->domain()) {
return $this->scheme ? $this->scheme . '://' . $domain : $domain;
}
return null;
}
/**
* Clones the Uri object and applies optional
* new props.
*/
public function clone(array $props = []): static
{
$clone = clone $this;
foreach ($props as $key => $value) {
$clone->__set($key, $value);
}
return $clone;
}
public static function current(array $props = []): static
{
if (static::$current !== null) {
return static::$current;
}
if ($app = App::instance(null, true)) {
$environment = $app->environment();
} else {
$environment = new Environment();
}
return new static($environment->requestUrl(), $props);
}
/**
* Returns the domain without scheme, path or query.
* Includes auth part when not empty.
* Includes port number when different from 80 or 443.
*/
public function domain(): string|null
{
if (empty($this->host) === true || $this->host === '/') {
return null;
}
$auth = $this->auth();
$domain = '';
if ($auth !== null) {
$domain .= $auth . '@';
}
$domain .= $this->host;
if (
$this->port !== null &&
in_array($this->port, [80, 443]) === false
) {
$domain .= ':' . $this->port;
}
return $domain;
}
public function hasFragment(): bool
{
return empty($this->fragment) === false;
}
public function hasPath(): bool
{
return $this->path()->isNotEmpty();
}
public function hasQuery(): bool
{
return $this->query()->isNotEmpty();
}
public function https(): bool
{
return $this->scheme() === 'https';
}
/**
* Tries to convert the internationalized host
* name to the human-readable UTF8 representation
*
* @return $this
*/
public function idn(): static
{
if (empty($this->host) === false) {
$this->setHost(Idn::decode($this->host));
}
return $this;
}
/**
* Creates an Uri object for the URL to the index.php
* or any other executed script.
*/
public static function index(array $props = []): static
{
if ($app = App::instance(null, true)) {
$url = $app->url('index');
} else {
$url = (new Environment())->baseUrl();
}
return new static($url, $props);
}
/**
* Checks if the host exists
*/
public function isAbsolute(): bool
{
return empty($this->host) === false;
}
/**
* @return $this
*/
public function setFragment(string|null $fragment = null): static
{
$this->fragment = $fragment ? ltrim($fragment, '#') : null;
return $this;
}
/**
* @return $this
*/
public function setHost(string|null $host = null): static
{
$this->host = $host;
return $this;
}
/**
* @return $this
*/
public function setParams(Params|string|array|false|null $params = null): static
{
// ensure that the special constructor value of `false`
// is never passed through as it's not supported by `Params`
if ($params === false) {
$params = [];
}
$this->params = $params instanceof Params ? $params : new Params($params);
return $this;
}
/**
* @return $this
*/
public function setPassword(
#[SensitiveParameter]
string|null $password = null
): static {
$this->password = $password;
return $this;
}
/**
* @return $this
*/
public function setPath(Path|string|array|null $path = null): static
{
$this->path = $path instanceof Path ? $path : new Path($path);
return $this;
}
/**
* @return $this
*/
public function setPort(int|null $port = null): static
{
if ($port === 0) {
$port = null;
}
if ($port !== null) {
if ($port < 1 || $port > 65535) {
throw new InvalidArgumentException('Invalid port format: ' . $port);
}
}
$this->port = $port;
return $this;
}
/**
* @return $this
*/
public function setQuery(Query|string|array|null $query = null): static
{
$this->query = $query instanceof Query ? $query : new Query($query);
return $this;
}
/**
* @return $this
*/
public function setScheme(string|null $scheme = null): static
{
if ($scheme !== null && in_array($scheme, static::$schemes) === false) {
throw new InvalidArgumentException('Invalid URL scheme: ' . $scheme);
}
$this->scheme = $scheme;
return $this;
}
/**
* Set if a trailing slash should be added to
* the path when the URI is being built
*
* @return $this
*/
public function setSlash(bool $slash = false): static
{
$this->slash = $slash;
return $this;
}
/**
* @return $this
*/
public function setUsername(string|null $username = null): static
{
$this->username = $username;
return $this;
}
/**
* Converts the Url object to an array
*/
public function toArray(): array
{
$array = [];
foreach ($this->props as $key => $value) {
$value = $this->$key;
if (is_object($value) === true) {
$value = $value->toArray();
}
$array[$key] = $value;
}
return $array;
}
public function toJson(...$arguments): string
{
return json_encode($this->toArray(), ...$arguments);
}
/**
* Returns the full URL as string
*/
public function toString(): string
{
$url = $this->base();
$slash = true;
if (empty($url) === true) {
$url = '/';
$slash = false;
}
$path = $this->path->toString($slash) . $this->params->toString(true);
if ($this->slash && $slash === true) {
$path .= '/';
}
$url .= $path;
$url .= $this->query->toString(true);
if (empty($this->fragment) === false) {
$url .= '#' . $this->fragment;
}
return $url;
}
/**
* Tries to convert a URL with an internationalized host
* name to the machine-readable Punycode representation
*
* @return $this
*/
public function unIdn(): static
{
if (empty($this->host) === false) {
$this->setHost(Idn::encode($this->host));
}
return $this;
}
/**
* Parses the path inside the props and extracts
* the params unless disabled
*
* @return array Modified props array
*/
protected static function parsePath(array $props): array
{
// extract params, the rest is the path;
// only do this if not explicitly disabled (set to `false`)
if (isset($props['params']) === false || $props['params'] !== false) {
$extract = Params::extract($props['path']);
$props['params'] ??= $extract['params'];
$props['path'] = $extract['path'];
$props['slash'] ??= $extract['slash'];
return $props;
}
// use the full path;
// automatically detect the trailing slash from it if possible
if (is_string($props['path']) === true) {
$props['slash'] = substr($props['path'], -1, 1) === '/';
}
return $props;
}
}