|
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 const DEFAULT_TCP_PORT = '3301'; |
|
19
|
|
|
|
|
20
|
|
|
private $connectionUri; |
|
21
|
|
|
private $username; |
|
22
|
|
|
private $password; |
|
23
|
|
|
private $options; |
|
24
|
|
|
|
|
25
|
44 |
|
public static function parse(string $dsn) : self |
|
26
|
|
|
{ |
|
27
|
44 |
|
$isSocket = 0 === \strpos($dsn, 'unix://'); |
|
28
|
|
|
|
|
29
|
44 |
|
if ($isSocket && $pos = \strpos($dsn, '/', 7)) { |
|
30
|
12 |
|
$dsn = \substr_replace($dsn, '.', $pos, 0); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
44 |
|
if (false === $parts = \parse_url($dsn)) { |
|
34
|
4 |
|
throw new \InvalidArgumentException(\sprintf('Malformed DSN "%s".', $dsn)); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
40 |
|
$self = new self(); |
|
38
|
|
|
|
|
39
|
40 |
|
if ($isSocket) { |
|
40
|
12 |
|
if ('' === \trim($parts['path'], '/')) { |
|
41
|
2 |
|
throw new \InvalidArgumentException(\sprintf('Malformed DSN "%s".', $dsn)); |
|
42
|
|
|
} |
|
43
|
10 |
|
$self->connectionUri = 'unix://'.$parts['path']; |
|
44
|
|
|
} else { |
|
45
|
28 |
|
if (!isset($parts['scheme'], $parts['host'])) { |
|
46
|
8 |
|
throw new \InvalidArgumentException(\sprintf('Malformed DSN "%s".', $dsn)); |
|
47
|
|
|
} |
|
48
|
20 |
|
$self->connectionUri = $parts['scheme'].'://'.$parts['host'].':'.($parts['port'] ?? self::DEFAULT_TCP_PORT); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
30 |
|
if (isset($parts['user'])) { |
|
52
|
10 |
|
$self->username = $parts['user']; |
|
53
|
10 |
|
$self->password = $parts['pass'] ?? ''; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
30 |
|
if (isset($parts['query'])) { |
|
57
|
8 |
|
\parse_str($parts['query'], $self->options); |
|
58
|
|
|
} else { |
|
59
|
22 |
|
$self->options = []; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
30 |
|
return $self; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
30 |
|
public function getConnectionUri() : string |
|
66
|
|
|
{ |
|
67
|
30 |
|
return $this->connectionUri; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
10 |
|
public function getUsername() : ?string |
|
71
|
|
|
{ |
|
72
|
10 |
|
return $this->username; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
10 |
|
public function getPassword() : ?string |
|
76
|
|
|
{ |
|
77
|
10 |
|
return $this->password; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
4 |
|
public function getString(string $name, ?string $default = null) : ?string |
|
81
|
|
|
{ |
|
82
|
4 |
|
return $this->options[$name] ?? $default; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
4 |
|
public function getBool(string $name, ?bool $default = null) : ?bool |
|
86
|
|
|
{ |
|
87
|
4 |
|
if (!isset($this->options[$name])) { |
|
88
|
|
|
return $default; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
4 |
|
if (null === $value = \filter_var($this->options[$name], \FILTER_VALIDATE_BOOLEAN, \FILTER_NULL_ON_FAILURE)) { |
|
92
|
|
|
throw new \InvalidArgumentException($name); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
4 |
|
return $value; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
8 |
|
public function getInt(string $name, ?int $default = null) : ?int |
|
99
|
|
|
{ |
|
100
|
8 |
|
if (!isset($this->options[$name])) { |
|
101
|
|
|
return $default; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
8 |
|
if (null === $value = \filter_var($this->options[$name], \FILTER_VALIDATE_INT, \FILTER_NULL_ON_FAILURE)) { |
|
105
|
|
|
throw new \InvalidArgumentException($name); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
8 |
|
return $value; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|