|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace MySQLReplication\Config; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class Config |
|
7
|
|
|
* @package MySQLReplication\Config |
|
8
|
|
|
*/ |
|
9
|
|
|
class Config implements \JsonSerializable |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var string |
|
13
|
|
|
*/ |
|
14
|
|
|
private static $user; |
|
15
|
|
|
/** |
|
16
|
|
|
* @var string |
|
17
|
|
|
*/ |
|
18
|
|
|
private static $host; |
|
19
|
|
|
/** |
|
20
|
|
|
* @var int |
|
21
|
|
|
*/ |
|
22
|
|
|
private static $port; |
|
23
|
|
|
/** |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
private static $password; |
|
27
|
|
|
/** |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
private static $charset; |
|
31
|
|
|
/** |
|
32
|
|
|
* @var string |
|
33
|
|
|
*/ |
|
34
|
|
|
private static $gtid; |
|
35
|
|
|
/** |
|
36
|
|
|
* @var int |
|
37
|
|
|
*/ |
|
38
|
|
|
private static $slaveId; |
|
39
|
|
|
/** |
|
40
|
|
|
* @var string |
|
41
|
|
|
*/ |
|
42
|
|
|
private static $binLogFileName; |
|
43
|
|
|
/** |
|
44
|
|
|
* @var int |
|
45
|
|
|
*/ |
|
46
|
|
|
private static $binLogPosition; |
|
47
|
|
|
/** |
|
48
|
|
|
* @var array |
|
49
|
|
|
*/ |
|
50
|
|
|
private static $eventsOnly; |
|
51
|
|
|
/** |
|
52
|
|
|
* @var array |
|
53
|
|
|
*/ |
|
54
|
|
|
private static $eventsIgnore; |
|
55
|
|
|
/** |
|
56
|
|
|
* @var array |
|
57
|
|
|
*/ |
|
58
|
|
|
private static $tablesOnly; |
|
59
|
|
|
/** |
|
60
|
|
|
* @var array |
|
61
|
|
|
*/ |
|
62
|
|
|
private static $databasesOnly; |
|
63
|
|
|
/** |
|
64
|
|
|
* @var string |
|
65
|
|
|
*/ |
|
66
|
|
|
private static $mariaDbGtid; |
|
67
|
|
|
/** |
|
68
|
|
|
* @var int |
|
69
|
|
|
*/ |
|
70
|
|
|
private static $tableCacheSize; |
|
71
|
|
|
/** |
|
72
|
|
|
* @var array |
|
73
|
|
|
*/ |
|
74
|
|
|
private static $custom; |
|
75
|
|
|
/** |
|
76
|
|
|
* @var int |
|
77
|
|
|
*/ |
|
78
|
|
|
private static $heartbeatPeriod; |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Config constructor. |
|
82
|
|
|
* @param string $user |
|
83
|
|
|
* @param string $host |
|
84
|
|
|
* @param int $port |
|
85
|
|
|
* @param string $password |
|
86
|
|
|
* @param string $charset |
|
87
|
|
|
* @param string $gtid |
|
88
|
|
|
* @param string $mariaGtid |
|
89
|
|
|
* @param int $slaveId |
|
90
|
|
|
* @param string $binLogFileName |
|
91
|
|
|
* @param $binLogPosition |
|
92
|
|
|
* @param array $eventsOnly |
|
93
|
|
|
* @param array $eventsIgnore |
|
94
|
|
|
* @param array $tablesOnly |
|
95
|
|
|
* @param array $databasesOnly |
|
96
|
|
|
* @param int $tableCacheSize |
|
97
|
|
|
* @param array $custom |
|
98
|
|
|
* @param int $heartbeatPeriod |
|
99
|
|
|
*/ |
|
100
|
74 |
|
public function __construct( |
|
101
|
|
|
$user, |
|
102
|
|
|
$host, |
|
103
|
|
|
$port, |
|
104
|
|
|
$password, |
|
105
|
|
|
$charset, |
|
106
|
|
|
$gtid, |
|
107
|
|
|
$mariaGtid, |
|
108
|
|
|
$slaveId, |
|
109
|
|
|
$binLogFileName, |
|
110
|
|
|
$binLogPosition, |
|
111
|
|
|
array $eventsOnly, |
|
112
|
|
|
array $eventsIgnore, |
|
113
|
|
|
array $tablesOnly, |
|
114
|
|
|
array $databasesOnly, |
|
115
|
|
|
$tableCacheSize, |
|
116
|
|
|
array $custom, |
|
117
|
|
|
$heartbeatPeriod |
|
118
|
|
|
) { |
|
119
|
74 |
|
self::$user = $user; |
|
120
|
74 |
|
self::$host = $host; |
|
121
|
74 |
|
self::$port = $port; |
|
122
|
74 |
|
self::$password = $password; |
|
123
|
74 |
|
self::$charset = $charset; |
|
124
|
74 |
|
self::$gtid = $gtid; |
|
125
|
74 |
|
self::$slaveId = $slaveId; |
|
126
|
74 |
|
self::$binLogFileName = $binLogFileName; |
|
127
|
74 |
|
self::$binLogPosition = $binLogPosition; |
|
128
|
74 |
|
self::$eventsOnly = $eventsOnly; |
|
129
|
74 |
|
self::$eventsIgnore = $eventsIgnore; |
|
130
|
74 |
|
self::$tablesOnly = $tablesOnly; |
|
131
|
74 |
|
self::$databasesOnly = $databasesOnly; |
|
132
|
74 |
|
self::$mariaDbGtid = $mariaGtid; |
|
133
|
74 |
|
self::$tableCacheSize = $tableCacheSize; |
|
134
|
74 |
|
self::$custom = $custom; |
|
135
|
74 |
|
self::$heartbeatPeriod = $heartbeatPeriod; |
|
136
|
74 |
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @throws ConfigException |
|
140
|
|
|
*/ |
|
141
|
69 |
|
public static function validate() |
|
142
|
|
|
{ |
|
143
|
69 |
|
if (!empty(self::$user) && !is_string(self::$user)) { |
|
|
|
|
|
|
144
|
1 |
|
throw new ConfigException(ConfigException::USER_ERROR_MESSAGE, ConfigException::USER_ERROR_CODE); |
|
145
|
|
|
} |
|
146
|
68 |
|
if (!empty(self::$host)) { |
|
147
|
68 |
|
$ip = gethostbyname(self::$host); |
|
148
|
68 |
|
if (false === filter_var($ip, FILTER_VALIDATE_IP)) { |
|
149
|
1 |
|
throw new ConfigException(ConfigException::IP_ERROR_MESSAGE, ConfigException::IP_ERROR_CODE); |
|
150
|
|
|
} |
|
151
|
67 |
|
} |
|
152
|
67 |
|
if (!empty(self::$port) && false === filter_var( |
|
153
|
67 |
|
self::$port, FILTER_VALIDATE_INT, ['options' => ['min_range' => 0]] |
|
154
|
67 |
|
) |
|
155
|
67 |
|
) { |
|
156
|
1 |
|
throw new ConfigException(ConfigException::PORT_ERROR_MESSAGE, ConfigException::PORT_ERROR_CODE); |
|
157
|
|
|
} |
|
158
|
66 |
|
if (!empty(self::$password) && !is_string(self::$password) && !is_numeric(self::$password)) { |
|
|
|
|
|
|
159
|
1 |
|
throw new ConfigException(ConfigException::PASSWORD_ERROR_MESSAGE, ConfigException::PASSWORD_ERROR_CODE); |
|
160
|
|
|
} |
|
161
|
65 |
|
if (!empty(self::$charset) && !is_string(self::$charset)) { |
|
|
|
|
|
|
162
|
1 |
|
throw new ConfigException(ConfigException::CHARSET_ERROR_MESSAGE, ConfigException::CHARSET_ERROR_CODE); |
|
163
|
|
|
} |
|
164
|
64 |
|
if (!empty(self::$gtid) && !is_string(self::$gtid)) { |
|
|
|
|
|
|
165
|
1 |
|
foreach (explode(',', self::$gtid) as $gtid) { |
|
166
|
1 |
|
if (!(bool)preg_match( |
|
167
|
1 |
|
'/^([0-9a-fA-F]{8}(?:-[0-9a-fA-F]{4}){3}-[0-9a-fA-F]{12})((?::[0-9-]+)+)$/', $gtid, $matches |
|
168
|
1 |
|
) |
|
169
|
1 |
|
) { |
|
170
|
1 |
|
throw new ConfigException(ConfigException::GTID_ERROR_MESSAGE, ConfigException::GTID_ERROR_CODE); |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
63 |
|
if (!empty(self::$slaveId) && false === filter_var(self::$slaveId, FILTER_VALIDATE_INT, ['options' => ['min_range' => 0]]) |
|
175
|
63 |
|
) { |
|
176
|
1 |
|
throw new ConfigException(ConfigException::SLAVE_ID_ERROR_MESSAGE, ConfigException::SLAVE_ID_ERROR_CODE); |
|
177
|
|
|
} |
|
178
|
62 |
|
if (!empty(self::$binLogFileName) && !is_string(self::$binLogFileName)) { |
|
|
|
|
|
|
179
|
1 |
|
throw new ConfigException( |
|
180
|
1 |
|
ConfigException::BIN_LOG_FILE_NAME_ERROR_MESSAGE, ConfigException::BIN_LOG_FILE_NAME_ERROR_CODE |
|
181
|
1 |
|
); |
|
182
|
|
|
} |
|
183
|
61 |
|
if (false === filter_var(self::$binLogPosition, FILTER_VALIDATE_INT, ['options' => ['min_range' => 0]])) { |
|
184
|
1 |
|
throw new ConfigException( |
|
185
|
1 |
|
ConfigException::BIN_LOG_FILE_POSITION_ERROR_MESSAGE, ConfigException::BIN_LOG_FILE_POSITION_ERROR_CODE |
|
186
|
1 |
|
); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
60 |
|
if (!empty(self::$mariaDbGtid) && !is_string(self::$mariaDbGtid)) { |
|
|
|
|
|
|
190
|
1 |
|
throw new ConfigException( |
|
191
|
1 |
|
ConfigException::MARIADBGTID_ERROR_MESSAGE, ConfigException::MARIADBGTID_ERROR_CODE |
|
192
|
1 |
|
); |
|
193
|
|
|
} |
|
194
|
59 |
|
if (false === filter_var(self::$tableCacheSize, FILTER_VALIDATE_INT, ['options' => ['min_range' => 0]])) { |
|
195
|
1 |
|
throw new ConfigException( |
|
196
|
1 |
|
ConfigException::TABLE_CACHE_SIZE_ERROR_MESSAGE, ConfigException::TABLE_CACHE_SIZE_ERROR_CODE |
|
197
|
1 |
|
); |
|
198
|
|
|
} |
|
199
|
58 |
|
if (0 !== self::$heartbeatPeriod && false === filter_var( |
|
200
|
4 |
|
self::$heartbeatPeriod, FILTER_VALIDATE_INT, ['options' => ['min_range' => 1, 'max_range' => 4294967]] |
|
201
|
4 |
|
) |
|
202
|
58 |
|
) { |
|
203
|
3 |
|
throw new ConfigException( |
|
204
|
3 |
|
ConfigException::HEARTBEAT_PERIOD_ERROR_MESSAGE, ConfigException::HEARTBEAT_PERIOD_ERROR_CODE |
|
205
|
3 |
|
); |
|
206
|
|
|
} |
|
207
|
55 |
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* @return array |
|
211
|
|
|
*/ |
|
212
|
1 |
|
public static function getCustom() |
|
213
|
|
|
{ |
|
214
|
1 |
|
return self::$custom; |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* @return string |
|
219
|
|
|
*/ |
|
220
|
55 |
|
public static function getUser() |
|
221
|
|
|
{ |
|
222
|
55 |
|
return self::$user; |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* @return string |
|
227
|
|
|
*/ |
|
228
|
55 |
|
public static function getHost() |
|
229
|
|
|
{ |
|
230
|
55 |
|
return self::$host; |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
/** |
|
234
|
|
|
* @return int |
|
235
|
|
|
*/ |
|
236
|
55 |
|
public static function getPort() |
|
237
|
|
|
{ |
|
238
|
55 |
|
return self::$port; |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
/** |
|
242
|
|
|
* @return string |
|
243
|
|
|
*/ |
|
244
|
55 |
|
public static function getPassword() |
|
245
|
|
|
{ |
|
246
|
55 |
|
return self::$password; |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
/** |
|
250
|
|
|
* @return string |
|
251
|
|
|
*/ |
|
252
|
55 |
|
public static function getCharset() |
|
253
|
|
|
{ |
|
254
|
55 |
|
return self::$charset; |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
/** |
|
258
|
|
|
* @return string |
|
259
|
|
|
*/ |
|
260
|
55 |
|
public static function getGtid() |
|
261
|
|
|
{ |
|
262
|
55 |
|
return self::$gtid; |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
/** |
|
266
|
|
|
* @return string |
|
267
|
|
|
*/ |
|
268
|
55 |
|
public static function getMariaDbGtid() |
|
269
|
|
|
{ |
|
270
|
55 |
|
return self::$mariaDbGtid; |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
|
|
/** |
|
274
|
|
|
* @return int |
|
275
|
|
|
*/ |
|
276
|
55 |
|
public static function getSlaveId() |
|
277
|
|
|
{ |
|
278
|
55 |
|
return self::$slaveId; |
|
279
|
|
|
} |
|
280
|
|
|
|
|
281
|
|
|
/** |
|
282
|
|
|
* @return string |
|
283
|
|
|
*/ |
|
284
|
55 |
|
public static function getBinLogFileName() |
|
285
|
|
|
{ |
|
286
|
55 |
|
return self::$binLogFileName; |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
/** |
|
290
|
|
|
* @return int |
|
291
|
|
|
*/ |
|
292
|
55 |
|
public static function getBinLogPosition() |
|
293
|
|
|
{ |
|
294
|
55 |
|
return self::$binLogPosition; |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
|
|
/** |
|
298
|
|
|
* @return array |
|
299
|
|
|
*/ |
|
300
|
56 |
|
public static function getEventsOnly() |
|
301
|
|
|
{ |
|
302
|
56 |
|
return self::$eventsOnly; |
|
303
|
|
|
} |
|
304
|
|
|
|
|
305
|
|
|
/** |
|
306
|
|
|
* @return array |
|
307
|
|
|
*/ |
|
308
|
56 |
|
public static function getEventsIgnore() |
|
309
|
|
|
{ |
|
310
|
56 |
|
return self::$eventsIgnore; |
|
311
|
|
|
} |
|
312
|
|
|
|
|
313
|
|
|
/** |
|
314
|
|
|
* @return array |
|
315
|
|
|
*/ |
|
316
|
54 |
|
public static function getTablesOnly() |
|
317
|
|
|
{ |
|
318
|
54 |
|
return self::$tablesOnly; |
|
319
|
|
|
} |
|
320
|
|
|
|
|
321
|
|
|
/** |
|
322
|
|
|
* @return array |
|
323
|
|
|
*/ |
|
324
|
54 |
|
public static function getDatabasesOnly() |
|
325
|
|
|
{ |
|
326
|
54 |
|
return self::$databasesOnly; |
|
327
|
|
|
} |
|
328
|
|
|
|
|
329
|
|
|
/** |
|
330
|
|
|
* @return int |
|
331
|
|
|
*/ |
|
332
|
62 |
|
public static function getTableCacheSize() |
|
333
|
|
|
{ |
|
334
|
62 |
|
return self::$tableCacheSize; |
|
335
|
|
|
} |
|
336
|
|
|
|
|
337
|
|
|
/** |
|
338
|
|
|
* @param string $database |
|
339
|
|
|
* @return bool |
|
340
|
|
|
*/ |
|
341
|
53 |
|
public static function checkDataBasesOnly($database) |
|
342
|
|
|
{ |
|
343
|
53 |
|
return [] !== Config::getDatabasesOnly() && !in_array($database, Config::getDatabasesOnly(), true); |
|
344
|
|
|
} |
|
345
|
|
|
|
|
346
|
|
|
/** |
|
347
|
|
|
* @param string $table |
|
348
|
|
|
* @return bool |
|
349
|
|
|
*/ |
|
350
|
53 |
|
public static function checkTablesOnly($table) |
|
351
|
|
|
{ |
|
352
|
53 |
|
return [] !== Config::getTablesOnly() && !in_array($table, Config::getTablesOnly(), true); |
|
353
|
|
|
} |
|
354
|
|
|
|
|
355
|
|
|
/** |
|
356
|
|
|
* @param int $type |
|
357
|
|
|
* @return bool |
|
358
|
|
|
*/ |
|
359
|
55 |
|
public static function checkEvent($type) |
|
360
|
|
|
{ |
|
361
|
55 |
|
if ([] !== Config::getEventsOnly() && !in_array($type, Config::getEventsOnly(), true)) { |
|
362
|
4 |
|
return false; |
|
363
|
|
|
} |
|
364
|
|
|
|
|
365
|
55 |
|
if (in_array($type, Config::getEventsIgnore(), true)) { |
|
366
|
1 |
|
return false; |
|
367
|
|
|
} |
|
368
|
|
|
|
|
369
|
55 |
|
return true; |
|
370
|
|
|
} |
|
371
|
|
|
|
|
372
|
|
|
/** |
|
373
|
|
|
* @return int |
|
374
|
|
|
*/ |
|
375
|
55 |
|
public static function getHeartbeatPeriod() |
|
376
|
|
|
{ |
|
377
|
55 |
|
return self::$heartbeatPeriod; |
|
378
|
|
|
} |
|
379
|
|
|
|
|
380
|
|
|
/** |
|
381
|
|
|
* Specify data which should be serialized to JSON |
|
382
|
|
|
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php |
|
383
|
|
|
* @return mixed data which can be serialized by <b>json_encode</b>, |
|
384
|
|
|
* which is a value of any type other than a resource. |
|
385
|
|
|
* @since 5.4.0 |
|
386
|
|
|
*/ |
|
387
|
1 |
|
public function jsonSerialize() |
|
388
|
|
|
{ |
|
389
|
1 |
|
return get_class_vars(self::class); |
|
390
|
|
|
} |
|
391
|
|
|
} |