|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Helldar\Support\Helpers; |
|
4
|
|
|
|
|
5
|
|
|
use Helldar\Support\Concerns\Makeable; |
|
6
|
|
|
use Helldar\Support\Facades\Helpers\Arr; |
|
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Based on code by Maksim (Ellrion) Platonov. |
|
10
|
|
|
* |
|
11
|
|
|
* @see https://gist.github.com/Ellrion/f51ba0d40ae1d62eeae44fd1adf7b704 |
|
12
|
|
|
*/ |
|
13
|
|
|
final class HttpBuilder |
|
14
|
|
|
{ |
|
15
|
|
|
use Makeable; |
|
16
|
|
|
|
|
17
|
|
|
protected $parsed = []; |
|
18
|
|
|
|
|
19
|
|
|
protected $components = [ |
|
20
|
|
|
PHP_URL_SCHEME => 'scheme', |
|
21
|
|
|
PHP_URL_HOST => 'host', |
|
22
|
|
|
PHP_URL_PORT => 'port', |
|
23
|
|
|
PHP_URL_USER => 'user', |
|
24
|
|
|
PHP_URL_PASS => 'pass', |
|
25
|
|
|
PHP_URL_QUERY => 'query', |
|
26
|
|
|
PHP_URL_PATH => 'path', |
|
27
|
|
|
PHP_URL_FRAGMENT => 'fragment', |
|
28
|
|
|
]; |
|
29
|
|
|
|
|
30
|
|
|
public function same(): self |
|
31
|
|
|
{ |
|
32
|
|
|
return $this; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function parse(string $url, int $component = -1): self |
|
36
|
|
|
{ |
|
37
|
|
|
$component = $this->componentIndex($component); |
|
38
|
|
|
$key = $this->componentKey($component); |
|
39
|
|
|
|
|
40
|
|
|
$component === -1 || empty($key) |
|
41
|
|
|
? $this->parsed = parse_url($url) |
|
42
|
|
|
: $this->parsed[$key] = parse_url($url, $component); |
|
43
|
|
|
|
|
44
|
|
|
return $this; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function getScheme(): ?string |
|
48
|
|
|
{ |
|
49
|
|
|
return $this->get('scheme'); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function getHost(): ?string |
|
53
|
|
|
{ |
|
54
|
|
|
return $this->get('host'); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function getPort(): ?string |
|
58
|
|
|
{ |
|
59
|
|
|
return $this->get('port'); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function getUser(): ?string |
|
63
|
|
|
{ |
|
64
|
|
|
return $this->get('user'); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function getPassword(): ?string |
|
68
|
|
|
{ |
|
69
|
|
|
return $this->get('pass'); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function getPath(): ?string |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->get('path'); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function getQuery(): ?string |
|
78
|
|
|
{ |
|
79
|
|
|
return $this->get('query'); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function getFragment(): ?string |
|
83
|
|
|
{ |
|
84
|
|
|
return $this->get('fragment'); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function compile(): string |
|
88
|
|
|
{ |
|
89
|
|
|
return implode('', array_filter($this->prepare())); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
protected function prepare(): array |
|
93
|
|
|
{ |
|
94
|
|
|
return [ |
|
95
|
|
|
$this->getScheme() ? $this->getScheme() . '://' : '', |
|
96
|
|
|
$this->getUser(), |
|
97
|
|
|
$this->getPassword() ? ':' . $this->getPassword() : '', |
|
98
|
|
|
$this->getUser() || $this->getPassword() ? '@' : '', |
|
99
|
|
|
$this->getHost(), |
|
100
|
|
|
$this->getPort() ? ':' . $this->getPort() : '', |
|
101
|
|
|
$this->getPath() ? '/' . ltrim($this->getPath(), '/') : '', |
|
102
|
|
|
$this->getQuery() ? '?' . $this->getQuery() : '', |
|
103
|
|
|
$this->getFragment() ? '#' . $this->getFragment() : '', |
|
104
|
|
|
]; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
protected function get(string $key): ?string |
|
108
|
|
|
{ |
|
109
|
|
|
return Arr::get($this->parsed, $key); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
protected function componentIndex(int $component = -1): int |
|
113
|
|
|
{ |
|
114
|
|
|
return Arr::getKey($this->components, $component, -1); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
protected function componentKey(int $component = -1): ?string |
|
118
|
|
|
{ |
|
119
|
|
|
return Arr::get($this->components, $component); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: