|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace MySQLReplication\Config; |
|
5
|
|
|
|
|
6
|
|
|
use JsonSerializable; |
|
7
|
|
|
|
|
8
|
|
|
class Config implements JsonSerializable |
|
9
|
|
|
{ |
|
10
|
|
|
private static $user; |
|
11
|
|
|
private static $host; |
|
12
|
|
|
private static $port; |
|
13
|
|
|
private static $password; |
|
14
|
|
|
private static $charset; |
|
15
|
|
|
private static $gtid; |
|
16
|
|
|
private static $slaveId; |
|
17
|
|
|
private static $binLogFileName; |
|
18
|
|
|
private static $binLogPosition; |
|
19
|
|
|
private static $eventsOnly; |
|
20
|
|
|
private static $eventsIgnore; |
|
21
|
|
|
private static $tablesOnly; |
|
22
|
|
|
private static $databasesOnly; |
|
23
|
|
|
private static $mariaDbGtid; |
|
24
|
|
|
private static $tableCacheSize; |
|
25
|
|
|
private static $custom; |
|
26
|
|
|
private static $heartbeatPeriod; |
|
27
|
|
|
|
|
28
|
69 |
|
public function __construct( |
|
29
|
|
|
string $user, |
|
30
|
|
|
string $host, |
|
31
|
|
|
int $port, |
|
32
|
|
|
string $password, |
|
33
|
|
|
string $charset, |
|
34
|
|
|
string $gtid, |
|
35
|
|
|
string $mariaGtid, |
|
36
|
|
|
int $slaveId, |
|
37
|
|
|
string $binLogFileName, |
|
38
|
|
|
int $binLogPosition, |
|
39
|
|
|
array $eventsOnly, |
|
40
|
|
|
array $eventsIgnore, |
|
41
|
|
|
array $tablesOnly, |
|
42
|
|
|
array $databasesOnly, |
|
43
|
|
|
int $tableCacheSize, |
|
44
|
|
|
array $custom, |
|
45
|
|
|
int $heartbeatPeriod |
|
46
|
|
|
) { |
|
47
|
69 |
|
self::$user = $user; |
|
48
|
69 |
|
self::$host = $host; |
|
49
|
69 |
|
self::$port = $port; |
|
50
|
69 |
|
self::$password = $password; |
|
51
|
69 |
|
self::$charset = $charset; |
|
52
|
69 |
|
self::$gtid = $gtid; |
|
53
|
69 |
|
self::$slaveId = $slaveId; |
|
54
|
69 |
|
self::$binLogFileName = $binLogFileName; |
|
55
|
69 |
|
self::$binLogPosition = $binLogPosition; |
|
56
|
69 |
|
self::$eventsOnly = $eventsOnly; |
|
57
|
69 |
|
self::$eventsIgnore = $eventsIgnore; |
|
58
|
69 |
|
self::$tablesOnly = $tablesOnly; |
|
59
|
69 |
|
self::$databasesOnly = $databasesOnly; |
|
60
|
69 |
|
self::$mariaDbGtid = $mariaGtid; |
|
61
|
69 |
|
self::$tableCacheSize = $tableCacheSize; |
|
62
|
69 |
|
self::$custom = $custom; |
|
63
|
69 |
|
self::$heartbeatPeriod = $heartbeatPeriod; |
|
64
|
69 |
|
} |
|
65
|
|
|
|
|
66
|
8 |
|
public static function makeConfigFromArray(array $config): Config |
|
67
|
|
|
{ |
|
68
|
8 |
|
$configBuilder = new ConfigBuilder(); |
|
69
|
8 |
|
foreach ($config as $k => $v) { |
|
70
|
8 |
|
if ('user' === $k) { |
|
71
|
|
|
$configBuilder->withUser($v); |
|
72
|
|
|
} |
|
73
|
8 |
|
if ('ip' === $k || 'host' === $k) { |
|
74
|
1 |
|
$configBuilder->withHost($v); |
|
75
|
|
|
} |
|
76
|
8 |
|
if ('port' === $k) { |
|
77
|
1 |
|
$configBuilder->withPort($v); |
|
78
|
|
|
} |
|
79
|
8 |
|
if ('password' === $k) { |
|
80
|
|
|
$configBuilder->withPassword($v); |
|
81
|
|
|
} |
|
82
|
8 |
|
if ('charset' === $k) { |
|
83
|
|
|
$configBuilder->withCharset($v); |
|
84
|
|
|
} |
|
85
|
8 |
|
if ('gtid' === $k) { |
|
86
|
1 |
|
$configBuilder->withGtid($v); |
|
87
|
|
|
} |
|
88
|
8 |
|
if ('slaveId' === $k) { |
|
89
|
1 |
|
$configBuilder->withSlaveId($v); |
|
90
|
|
|
} |
|
91
|
8 |
|
if ('binLogFileName' === $k) { |
|
92
|
|
|
$configBuilder->withBinLogFileName($v); |
|
93
|
|
|
} |
|
94
|
8 |
|
if ('binLogPosition' === $k) { |
|
95
|
1 |
|
$configBuilder->withBinLogPosition($v); |
|
96
|
|
|
} |
|
97
|
8 |
|
if ('eventsOnly' === $k) { |
|
98
|
|
|
$configBuilder->withEventsOnly($v); |
|
99
|
|
|
} |
|
100
|
8 |
|
if ('eventsIgnore' === $k) { |
|
101
|
|
|
$configBuilder->withEventsIgnore($v); |
|
102
|
|
|
} |
|
103
|
8 |
|
if ('tablesOnly' === $k) { |
|
104
|
|
|
$configBuilder->withTablesOnly($v); |
|
105
|
|
|
} |
|
106
|
8 |
|
if ('databasesOnly' === $k) { |
|
107
|
|
|
$configBuilder->withDatabasesOnly($v); |
|
108
|
|
|
} |
|
109
|
8 |
|
if ('mariaDbGtid' === $k) { |
|
110
|
|
|
$configBuilder->withMariaDbGtid($v); |
|
111
|
|
|
} |
|
112
|
8 |
|
if ('tableCacheSize' === $k) { |
|
113
|
1 |
|
$configBuilder->withTableCacheSize($v); |
|
114
|
|
|
} |
|
115
|
8 |
|
if ('custom' === $k) { |
|
116
|
|
|
$configBuilder->withCustom($v); |
|
117
|
|
|
} |
|
118
|
8 |
|
if ('heartbeatPeriod' === $k) { |
|
119
|
8 |
|
$configBuilder->withHeartbeatPeriod($v); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
8 |
|
return $configBuilder->build(); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @throws ConfigException |
|
128
|
|
|
*/ |
|
129
|
65 |
|
public static function validate(): void |
|
130
|
|
|
{ |
|
131
|
65 |
|
if (!empty(self::$host)) { |
|
132
|
65 |
|
$ip = gethostbyname(self::$host); |
|
133
|
65 |
|
if (false === filter_var($ip, FILTER_VALIDATE_IP)) { |
|
134
|
1 |
|
throw new ConfigException(ConfigException::IP_ERROR_MESSAGE, ConfigException::IP_ERROR_CODE); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
64 |
|
if (!empty(self::$port) && false === filter_var( |
|
138
|
64 |
|
self::$port, FILTER_VALIDATE_INT, ['options' => ['min_range' => 0]] |
|
139
|
|
|
)) { |
|
140
|
1 |
|
throw new ConfigException(ConfigException::PORT_ERROR_MESSAGE, ConfigException::PORT_ERROR_CODE); |
|
141
|
|
|
} |
|
142
|
63 |
|
if (!empty(self::$gtid)) { |
|
143
|
2 |
|
foreach (explode(',', self::$gtid) as $gtid) { |
|
144
|
2 |
|
if (!(bool)preg_match( |
|
145
|
2 |
|
'/^([0-9a-fA-F]{8}(?:-[0-9a-fA-F]{4}){3}-[0-9a-fA-F]{12})((?::[0-9-]+)+)$/', $gtid, $matches |
|
146
|
|
|
)) { |
|
147
|
2 |
|
throw new ConfigException(ConfigException::GTID_ERROR_MESSAGE, ConfigException::GTID_ERROR_CODE); |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
62 |
|
if (!empty(self::$slaveId) && false === filter_var( |
|
152
|
62 |
|
self::$slaveId, FILTER_VALIDATE_INT, ['options' => ['min_range' => 0]] |
|
153
|
|
|
)) { |
|
154
|
1 |
|
throw new ConfigException(ConfigException::SLAVE_ID_ERROR_MESSAGE, ConfigException::SLAVE_ID_ERROR_CODE); |
|
155
|
|
|
} |
|
156
|
61 |
|
if (false === filter_var(self::$binLogPosition, FILTER_VALIDATE_INT, ['options' => ['min_range' => 0]])) { |
|
157
|
1 |
|
throw new ConfigException( |
|
158
|
1 |
|
ConfigException::BIN_LOG_FILE_POSITION_ERROR_MESSAGE, ConfigException::BIN_LOG_FILE_POSITION_ERROR_CODE |
|
159
|
|
|
); |
|
160
|
|
|
} |
|
161
|
60 |
|
if (false === filter_var(self::$tableCacheSize, FILTER_VALIDATE_INT, ['options' => ['min_range' => 0]])) { |
|
162
|
1 |
|
throw new ConfigException( |
|
163
|
1 |
|
ConfigException::TABLE_CACHE_SIZE_ERROR_MESSAGE, ConfigException::TABLE_CACHE_SIZE_ERROR_CODE |
|
164
|
|
|
); |
|
165
|
|
|
} |
|
166
|
59 |
|
if (0 !== self::$heartbeatPeriod && false === filter_var( |
|
167
|
59 |
|
self::$heartbeatPeriod, FILTER_VALIDATE_INT, ['options' => ['min_range' => 1, 'max_range' => 4294967]] |
|
168
|
|
|
)) { |
|
169
|
2 |
|
throw new ConfigException( |
|
170
|
2 |
|
ConfigException::HEARTBEAT_PERIOD_ERROR_MESSAGE, ConfigException::HEARTBEAT_PERIOD_ERROR_CODE |
|
171
|
|
|
); |
|
172
|
|
|
} |
|
173
|
57 |
|
} |
|
174
|
|
|
|
|
175
|
1 |
|
public static function getCustom(): array |
|
176
|
|
|
{ |
|
177
|
1 |
|
return self::$custom; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
57 |
|
public static function getUser(): string |
|
181
|
|
|
{ |
|
182
|
57 |
|
return self::$user; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
57 |
|
public static function getHost(): string |
|
186
|
|
|
{ |
|
187
|
57 |
|
return self::$host; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
57 |
|
public static function getPort(): int |
|
191
|
|
|
{ |
|
192
|
57 |
|
return self::$port; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
57 |
|
public static function getPassword(): string |
|
196
|
|
|
{ |
|
197
|
57 |
|
return self::$password; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
57 |
|
public static function getCharset(): string |
|
201
|
|
|
{ |
|
202
|
57 |
|
return self::$charset; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
57 |
|
public static function getGtid(): string |
|
206
|
|
|
{ |
|
207
|
57 |
|
return self::$gtid; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
57 |
|
public static function getMariaDbGtid(): string |
|
211
|
|
|
{ |
|
212
|
57 |
|
return self::$mariaDbGtid; |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
57 |
|
public static function getSlaveId(): int |
|
216
|
|
|
{ |
|
217
|
57 |
|
return self::$slaveId; |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
57 |
|
public static function getBinLogFileName(): string |
|
221
|
|
|
{ |
|
222
|
57 |
|
return self::$binLogFileName; |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
57 |
|
public static function getBinLogPosition(): int |
|
226
|
|
|
{ |
|
227
|
57 |
|
return self::$binLogPosition; |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
64 |
|
public static function getTableCacheSize(): int |
|
231
|
|
|
{ |
|
232
|
64 |
|
return self::$tableCacheSize; |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
55 |
|
public static function checkDataBasesOnly(string $database): bool |
|
236
|
|
|
{ |
|
237
|
55 |
|
return [] !== self::getDatabasesOnly() && !in_array($database, self::getDatabasesOnly(), true); |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
56 |
|
public static function getDatabasesOnly(): array |
|
241
|
|
|
{ |
|
242
|
56 |
|
return self::$databasesOnly; |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
55 |
|
public static function checkTablesOnly(string $table): bool |
|
246
|
|
|
{ |
|
247
|
55 |
|
return [] !== self::getTablesOnly() && !in_array($table, self::getTablesOnly(), true); |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
56 |
|
public static function getTablesOnly(): array |
|
251
|
|
|
{ |
|
252
|
56 |
|
return self::$tablesOnly; |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
57 |
|
public static function checkEvent(int $type): bool |
|
256
|
|
|
{ |
|
257
|
57 |
|
if ([] !== self::getEventsOnly() && !in_array($type, self::getEventsOnly(), true)) { |
|
258
|
5 |
|
return false; |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
57 |
|
if (in_array($type, self::getEventsIgnore(), true)) { |
|
262
|
1 |
|
return false; |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
57 |
|
return true; |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
58 |
|
public static function getEventsOnly(): array |
|
269
|
|
|
{ |
|
270
|
58 |
|
return self::$eventsOnly; |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
58 |
|
public static function getEventsIgnore(): array |
|
274
|
|
|
{ |
|
275
|
58 |
|
return self::$eventsIgnore; |
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
57 |
|
public static function getHeartbeatPeriod(): int |
|
279
|
|
|
{ |
|
280
|
57 |
|
return self::$heartbeatPeriod; |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
public function jsonSerialize() |
|
284
|
|
|
{ |
|
285
|
|
|
return get_class_vars(self::class); |
|
286
|
|
|
} |
|
287
|
|
|
} |