1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ButterAMQP; |
4
|
|
|
|
5
|
|
|
class Url |
6
|
|
|
{ |
7
|
|
|
const DEFAULT_SCHEMA = 'amqp'; |
8
|
|
|
const DEFAULT_HOST = 'localhost'; |
9
|
|
|
const DEFAULT_PORT = 5672; |
10
|
|
|
const DEFAULT_SECURE_PORT = 5671; |
11
|
|
|
const DEFAULT_USER = 'guest'; |
12
|
|
|
const DEFAULT_VHOST = '/'; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
private $scheme; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
private $host; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var int |
26
|
|
|
*/ |
27
|
|
|
private $port; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
private $user; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
private $password; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
private $vhost; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var array |
46
|
|
|
*/ |
47
|
|
|
private $query = []; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param string $scheme |
51
|
|
|
* @param string $host |
52
|
|
|
* @param int $port |
53
|
|
|
* @param string $user |
54
|
|
|
* @param string $password |
55
|
|
|
* @param string $vhost |
56
|
|
|
* @param array $query |
57
|
|
|
*/ |
58
|
38 |
|
public function __construct( |
59
|
|
|
$scheme = null, |
60
|
|
|
$host = null, |
61
|
|
|
$port = null, |
62
|
|
|
$user = null, |
63
|
|
|
$password = null, |
64
|
|
|
$vhost = null, |
65
|
|
|
array $query = [] |
66
|
|
|
) { |
67
|
38 |
|
$this->scheme = $scheme; |
68
|
38 |
|
$this->host = $host; |
69
|
38 |
|
$this->port = $port; |
70
|
38 |
|
$this->user = $user; |
71
|
38 |
|
$this->password = $password; |
72
|
38 |
|
$this->vhost = $vhost; |
73
|
38 |
|
$this->query = $query; |
74
|
38 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param string $url |
78
|
|
|
* |
79
|
|
|
* @return Url |
80
|
|
|
* |
81
|
|
|
* @throws \Exception |
82
|
|
|
*/ |
83
|
35 |
|
public static function parse($url) |
84
|
|
|
{ |
85
|
35 |
|
if (($parts = parse_url($url)) === false) { |
86
|
1 |
|
throw new \InvalidArgumentException(sprintf('Invalid URL "%s"', $url)); |
87
|
|
|
} |
88
|
|
|
|
89
|
34 |
|
$parts = array_merge(self::getParseDefaults(), $parts); |
90
|
|
|
|
91
|
34 |
|
parse_str($parts['query'], $query); |
92
|
|
|
|
93
|
34 |
|
return new self( |
94
|
34 |
|
urldecode($parts['scheme']), |
95
|
34 |
|
urldecode($parts['host']), |
96
|
34 |
|
$parts['port'], |
97
|
34 |
|
urldecode($parts['user']), |
98
|
34 |
|
urldecode($parts['pass']), |
99
|
34 |
|
urldecode(substr($parts['path'], 1)), |
100
|
32 |
|
$query ?: [] |
101
|
34 |
|
); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return array |
106
|
|
|
*/ |
107
|
34 |
|
private static function getParseDefaults() |
108
|
|
|
{ |
109
|
|
|
return [ |
110
|
34 |
|
'scheme' => null, |
111
|
34 |
|
'host' => null, |
112
|
34 |
|
'port' => null, |
113
|
34 |
|
'user' => null, |
114
|
34 |
|
'pass' => null, |
115
|
34 |
|
'path' => null, |
116
|
34 |
|
'query' => '', |
117
|
34 |
|
]; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Import URL from an array. |
122
|
|
|
* |
123
|
|
|
* @param array $data |
124
|
|
|
* |
125
|
|
|
* @return Url |
126
|
|
|
*/ |
127
|
1 |
|
public static function import(array $data) |
128
|
|
|
{ |
129
|
1 |
|
$data = array_merge(self::getImportDefaults(), $data); |
130
|
|
|
|
131
|
1 |
|
return new self( |
132
|
1 |
|
$data['scheme'], |
133
|
1 |
|
$data['host'], |
134
|
1 |
|
(int) $data['port'], |
135
|
1 |
|
$data['user'], |
136
|
1 |
|
$data['password'], |
137
|
1 |
|
$data['vhost'], |
138
|
1 |
|
(array) $data['parameters'] |
139
|
1 |
|
); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @return array |
144
|
|
|
*/ |
145
|
1 |
|
private static function getImportDefaults() |
146
|
|
|
{ |
147
|
|
|
return [ |
148
|
1 |
|
'scheme' => null, |
149
|
1 |
|
'host' => null, |
150
|
1 |
|
'port' => null, |
151
|
1 |
|
'user' => null, |
152
|
1 |
|
'password' => null, |
153
|
1 |
|
'vhost' => null, |
154
|
1 |
|
'parameters' => [], |
155
|
1 |
|
]; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @return string |
160
|
|
|
*/ |
161
|
22 |
|
public function getScheme() |
162
|
|
|
{ |
163
|
22 |
|
return $this->scheme ?: self::DEFAULT_SCHEMA; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @return string |
168
|
|
|
*/ |
169
|
21 |
|
public function getHost() |
170
|
|
|
{ |
171
|
21 |
|
return $this->host ?: self::DEFAULT_HOST; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @return int |
176
|
|
|
*/ |
177
|
22 |
|
public function getPort() |
178
|
|
|
{ |
179
|
22 |
|
if (empty($this->port)) { |
180
|
19 |
|
return strcasecmp($this->getScheme(), 'amqps') == 0 ? self::DEFAULT_SECURE_PORT : self::DEFAULT_PORT; |
181
|
|
|
} |
182
|
|
|
|
183
|
4 |
|
return $this->port; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @return string |
188
|
|
|
*/ |
189
|
22 |
|
public function getUser() |
190
|
|
|
{ |
191
|
22 |
|
return $this->user === null ? self::DEFAULT_USER : $this->user; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @return string |
196
|
|
|
*/ |
197
|
22 |
|
public function getPassword() |
198
|
|
|
{ |
199
|
22 |
|
return $this->password; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @return string |
204
|
|
|
*/ |
205
|
21 |
|
public function getVhost() |
206
|
|
|
{ |
207
|
21 |
|
return $this->vhost ?: self::DEFAULT_VHOST; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @return array |
212
|
|
|
*/ |
213
|
21 |
|
public function getQuery() |
214
|
|
|
{ |
215
|
21 |
|
return $this->query; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @param string $key |
220
|
|
|
* @param mixed $default |
221
|
|
|
* |
222
|
|
|
* @return mixed |
223
|
|
|
*/ |
224
|
19 |
|
public function getQueryParameter($key, $default = null) |
225
|
|
|
{ |
226
|
19 |
|
return isset($this->query[$key]) ? $this->query[$key] : $default; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* @param bool $maskPassword |
231
|
|
|
* |
232
|
|
|
* @return string |
233
|
|
|
*/ |
234
|
1 |
|
public function compose($maskPassword = true) |
235
|
|
|
{ |
236
|
1 |
|
return sprintf( |
237
|
1 |
|
'%s://%s:%s@%s:%d/%s%s', |
238
|
1 |
|
urlencode($this->scheme), |
239
|
1 |
|
urlencode($this->user), |
240
|
1 |
|
$maskPassword ? '******' : urlencode($this->password), |
241
|
1 |
|
urlencode($this->host), |
242
|
1 |
|
$this->port, |
243
|
1 |
|
urlencode($this->vhost), |
244
|
1 |
|
$this->query ? '?'.http_build_query($this->query) : '' |
245
|
1 |
|
); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Export URL to an array. |
250
|
|
|
* |
251
|
|
|
* @return array |
252
|
|
|
*/ |
253
|
1 |
|
public function export() |
254
|
|
|
{ |
255
|
|
|
return [ |
256
|
1 |
|
'scheme' => $this->scheme, |
257
|
1 |
|
'host' => $this->host, |
258
|
1 |
|
'port' => $this->port, |
259
|
1 |
|
'user' => $this->user, |
260
|
1 |
|
'password' => $this->password, |
261
|
1 |
|
'vhost' => $this->vhost, |
262
|
1 |
|
'parameters' => $this->query, |
263
|
1 |
|
]; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* @return string |
268
|
|
|
*/ |
269
|
1 |
|
public function __toString() |
270
|
|
|
{ |
271
|
1 |
|
return $this->compose(); |
272
|
|
|
} |
273
|
|
|
} |
274
|
|
|
|