1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Common storage parts. |
5
|
|
|
* |
6
|
|
|
* @author Maksim Masiukevich <[email protected]> |
7
|
|
|
* @license MIT |
8
|
|
|
* @license https://opensource.org/licenses/MIT |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
declare(strict_types = 1); |
12
|
|
|
|
13
|
|
|
namespace ServiceBus\Storage\Common; |
14
|
|
|
|
15
|
|
|
use ServiceBus\Storage\Common\Exceptions\InvalidConfigurationOptions; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Adapter configuration for storage. |
19
|
|
|
* |
20
|
|
|
* @psalm-readonly |
21
|
|
|
*/ |
22
|
|
|
final class StorageConfiguration |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Original DSN. |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
public $originalDSN; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Scheme. |
33
|
|
|
* |
34
|
|
|
* @var string|null |
35
|
|
|
*/ |
36
|
|
|
public $scheme; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Database host. |
40
|
|
|
* |
41
|
|
|
* @var string|null |
42
|
|
|
*/ |
43
|
|
|
public $host = null; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Database port. |
47
|
|
|
* |
48
|
|
|
* @var int|null |
49
|
|
|
*/ |
50
|
|
|
public $port = null; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Database user. |
54
|
|
|
* |
55
|
|
|
* @var string|null |
56
|
|
|
*/ |
57
|
|
|
public $username; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Database user password. |
61
|
|
|
* |
62
|
|
|
* @var string|null |
63
|
|
|
*/ |
64
|
|
|
public $password = null; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Database name. |
68
|
|
|
* |
69
|
|
|
* @var string|null |
70
|
|
|
*/ |
71
|
|
|
public $databaseName = null; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Connection encoding. |
75
|
|
|
* |
76
|
|
|
* @var string|null |
77
|
|
|
*/ |
78
|
|
|
public $encoding; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* All query parameters. |
82
|
|
|
* |
83
|
|
|
* @var array |
84
|
|
|
*/ |
85
|
|
|
public $queryParameters = []; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param string $connectionDSN DSN examples: |
89
|
|
|
* - inMemory: sqlite:///:memory: |
90
|
|
|
* - AsyncPostgreSQL: pgsql://user:password@host:port/database |
91
|
|
|
* |
92
|
|
|
* @throws \ServiceBus\Storage\Common\Exceptions\InvalidConfigurationOptions |
93
|
|
|
*/ |
94
|
10 |
|
public function __construct(string $connectionDSN) |
95
|
|
|
{ |
96
|
10 |
|
$preparedDSN = \preg_replace('#^((?:pdo_)?sqlite3?):///#', '$1://localhost/', $connectionDSN); |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @psalm-var array{ |
100
|
|
|
* scheme:string|null, |
101
|
|
|
* host:string|null, |
102
|
|
|
* port:int|null, |
103
|
|
|
* user:string|null, |
104
|
|
|
* pass:string|null, |
105
|
|
|
* path:string|null |
106
|
|
|
* }|null|false $parsedDSN |
107
|
|
|
* |
108
|
|
|
* @var array|false|null $parsedDSN |
109
|
|
|
*/ |
110
|
10 |
|
$parsedDSN = \parse_url((string) $preparedDSN); |
111
|
|
|
|
112
|
|
|
// @codeCoverageIgnoreStart |
113
|
|
|
if (\is_array($parsedDSN) === false) |
|
|
|
|
114
|
|
|
{ |
115
|
|
|
throw new InvalidConfigurationOptions('Error while parsing connection DSN'); |
116
|
|
|
} |
117
|
|
|
// @codeCoverageIgnoreEnd |
118
|
|
|
|
119
|
10 |
|
$queryString = 'charset=UTF-8'; |
120
|
|
|
|
121
|
10 |
|
if (!empty($parsedDSN['query'])) |
122
|
|
|
{ |
123
|
2 |
|
$queryString = (string) $parsedDSN['query']; |
124
|
|
|
} |
125
|
|
|
|
126
|
10 |
|
\parse_str($queryString, $this->queryParameters); |
127
|
|
|
|
128
|
|
|
/** @var array{charset:string|null, max_connections:int|null, idle_timeout:int|null} $queryParameters */ |
129
|
10 |
|
$queryParameters = $this->queryParameters; |
130
|
|
|
|
131
|
10 |
|
$this->originalDSN = $connectionDSN; |
132
|
10 |
|
$this->port = $parsedDSN['port'] ?? null; |
133
|
10 |
|
$this->scheme = self::extract('scheme', $parsedDSN); |
134
|
10 |
|
$this->host = self::extract('host', $parsedDSN, 'localhost'); |
135
|
10 |
|
$this->username = self::extract('user', $parsedDSN); |
136
|
10 |
|
$this->password = self::extract('pass', $parsedDSN); |
137
|
10 |
|
$this->encoding = self::extract('charset', $queryParameters, 'UTF-8'); |
138
|
|
|
|
139
|
10 |
|
$databaseName = self::extract('path', $parsedDSN); |
140
|
|
|
|
141
|
10 |
|
if ($databaseName !== null) |
142
|
|
|
{ |
143
|
|
|
/** @psalm-suppress PossiblyInvalidArgument */ |
144
|
9 |
|
$databaseName = \ltrim($databaseName, '/'); |
145
|
|
|
} |
146
|
|
|
|
147
|
10 |
|
$this->databaseName = $databaseName; |
148
|
10 |
|
} |
149
|
|
|
|
150
|
10 |
|
private static function extract(string $key, array $collection, ?string $default = null): ?string |
151
|
|
|
{ |
152
|
10 |
|
if (\array_key_exists($key, $collection) === false) |
153
|
|
|
{ |
154
|
8 |
|
return $default; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** @var string|null $value */ |
158
|
10 |
|
$value = $collection[$key]; |
159
|
|
|
|
160
|
10 |
|
if (empty($value) === true) |
161
|
|
|
{ |
162
|
2 |
|
return $default; |
163
|
|
|
} |
164
|
|
|
|
165
|
10 |
|
return $value; |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|