1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Tarantool Client package. |
7
|
|
|
* |
8
|
|
|
* (c) Eugene Leonovich <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Tarantool\Client; |
15
|
|
|
|
16
|
|
|
final class Dsn |
17
|
|
|
{ |
18
|
|
|
private $host; |
19
|
|
|
private $port; |
20
|
|
|
private $path; |
21
|
|
|
private $connectionUri; |
22
|
|
|
private $username; |
23
|
|
|
private $password; |
24
|
|
|
private $isTcp = false; |
25
|
|
|
private $options; |
26
|
|
|
|
27
|
128 |
|
private function __construct(string $connectionUri) |
28
|
|
|
{ |
29
|
128 |
|
$this->connectionUri = $connectionUri; |
30
|
128 |
|
} |
31
|
|
|
|
32
|
162 |
|
public static function parse(string $dsn) : self |
33
|
|
|
{ |
34
|
162 |
|
if (0 === \strpos($dsn, 'unix://') && isset($dsn[7])) { |
35
|
60 |
|
return self::parseUds($dsn); |
36
|
|
|
} |
37
|
|
|
|
38
|
102 |
|
if (false === $parsed = \parse_url($dsn)) { |
39
|
6 |
|
self::throwParseError($dsn); |
40
|
|
|
} |
41
|
96 |
|
if (!isset($parsed['scheme'], $parsed['host']) || 'tcp' !== $parsed['scheme']) { |
42
|
22 |
|
self::throwParseError($dsn); |
43
|
|
|
} |
44
|
74 |
|
if (isset($parsed['path']) && '/' !== $parsed['path']) { |
45
|
2 |
|
self::throwParseError($dsn); |
46
|
|
|
} |
47
|
|
|
|
48
|
72 |
|
$self = new self('tcp://'.$parsed['host'].':'.($parsed['port'] ?? '3301')); |
49
|
72 |
|
$self->host = $parsed['host']; |
50
|
72 |
|
$self->port = $parsed['port'] ?? 3301; |
51
|
72 |
|
$self->isTcp = true; |
52
|
|
|
|
53
|
72 |
|
if (isset($parsed['user'])) { |
54
|
6 |
|
$self->username = \rawurldecode($parsed['user']); |
55
|
6 |
|
$self->password = isset($parsed['pass']) ? \rawurldecode($parsed['pass']) : ''; |
56
|
|
|
} |
57
|
|
|
|
58
|
72 |
|
if (isset($parsed['query'])) { |
59
|
48 |
|
\parse_str($parsed['query'], $self->options); |
60
|
|
|
} |
61
|
|
|
|
62
|
72 |
|
return $self; |
63
|
|
|
} |
64
|
|
|
|
65
|
60 |
|
private static function parseUds(string $dsn) : self |
66
|
|
|
{ |
67
|
60 |
|
$parts = \explode('@', \substr($dsn, 7), 2); |
68
|
60 |
|
if (isset($parts[1])) { |
69
|
8 |
|
$parsed = \parse_url($parts[1]); |
70
|
8 |
|
$authority = \explode(':', $parts[0]); |
71
|
|
|
} else { |
72
|
52 |
|
$parsed = \parse_url($parts[0]); |
73
|
|
|
} |
74
|
|
|
|
75
|
60 |
|
if (false === $parsed) { |
76
|
2 |
|
self::throwParseError($dsn); |
77
|
|
|
} |
78
|
58 |
|
if (isset($parsed['host'])) { |
79
|
2 |
|
self::throwParseError($dsn); |
80
|
|
|
} |
81
|
|
|
|
82
|
56 |
|
$self = new self('unix://'.$parsed['path']); |
83
|
56 |
|
$self->path = \rawurldecode($parsed['path']); |
84
|
|
|
|
85
|
56 |
|
if (isset($authority)) { |
86
|
8 |
|
$self->username = \rawurldecode($authority[0]); |
87
|
8 |
|
$self->password = isset($authority[1]) ? \rawurldecode($authority[1]) : ''; |
88
|
|
|
} |
89
|
|
|
|
90
|
56 |
|
if (isset($parsed['query'])) { |
91
|
42 |
|
\parse_str($parsed['query'], $self->options); |
92
|
|
|
} |
93
|
|
|
|
94
|
56 |
|
return $self; |
95
|
|
|
} |
96
|
|
|
|
97
|
34 |
|
public function getConnectionUri() : string |
98
|
|
|
{ |
99
|
34 |
|
return $this->connectionUri; |
100
|
|
|
} |
101
|
|
|
|
102
|
34 |
|
public function getHost() : ?string |
103
|
|
|
{ |
104
|
34 |
|
return $this->host; |
105
|
|
|
} |
106
|
|
|
|
107
|
34 |
|
public function getPort() : ?int |
108
|
|
|
{ |
109
|
34 |
|
return $this->port; |
110
|
|
|
} |
111
|
|
|
|
112
|
34 |
|
public function getPath() : ?string |
113
|
|
|
{ |
114
|
34 |
|
return $this->path; |
115
|
|
|
} |
116
|
|
|
|
117
|
14 |
|
public function getUsername() : ?string |
118
|
|
|
{ |
119
|
14 |
|
return $this->username; |
120
|
|
|
} |
121
|
|
|
|
122
|
14 |
|
public function getPassword() : ?string |
123
|
|
|
{ |
124
|
14 |
|
return $this->password; |
125
|
|
|
} |
126
|
|
|
|
127
|
34 |
|
public function isTcp() : bool |
128
|
|
|
{ |
129
|
34 |
|
return $this->isTcp; |
130
|
|
|
} |
131
|
|
|
|
132
|
22 |
|
public function getString(string $name, ?string $default = null) : ?string |
133
|
|
|
{ |
134
|
22 |
|
return $this->options[$name] ?? $default; |
135
|
|
|
} |
136
|
|
|
|
137
|
42 |
|
public function getBool(string $name, ?bool $default = null) : ?bool |
138
|
|
|
{ |
139
|
42 |
|
if (!isset($this->options[$name])) { |
140
|
6 |
|
return $default; |
141
|
|
|
} |
142
|
|
|
|
143
|
36 |
|
if (null === $value = \filter_var($this->options[$name], \FILTER_VALIDATE_BOOLEAN, \FILTER_NULL_ON_FAILURE)) { |
144
|
8 |
|
throw new \TypeError(\sprintf('DSN option "%s" must be of the type bool.', $name)); |
145
|
|
|
} |
146
|
|
|
|
147
|
28 |
|
return $value; |
148
|
|
|
} |
149
|
|
|
|
150
|
30 |
|
public function getInt(string $name, ?int $default = null) : ?int |
151
|
|
|
{ |
152
|
30 |
|
if (!isset($this->options[$name])) { |
153
|
6 |
|
return $default; |
154
|
|
|
} |
155
|
|
|
|
156
|
24 |
|
if (null === $value = \filter_var($this->options[$name], \FILTER_VALIDATE_INT, \FILTER_NULL_ON_FAILURE)) { |
157
|
16 |
|
throw new \TypeError(\sprintf('DSN option "%s" must be of the type int.', $name)); |
158
|
|
|
} |
159
|
|
|
|
160
|
8 |
|
return $value; |
161
|
|
|
} |
162
|
|
|
|
163
|
34 |
|
private static function throwParseError(string $dsn) : void |
164
|
|
|
{ |
165
|
34 |
|
throw new \InvalidArgumentException(\sprintf('Unable to parse DSN "%s".', $dsn)); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|