1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MySQLReplication\Config; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class Config |
7
|
|
|
* @package MySQLReplication\Config |
8
|
|
|
*/ |
9
|
|
|
class Config |
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
|
54 |
|
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
|
54 |
|
self::$user = $user; |
120
|
54 |
|
self::$host = $host; |
121
|
54 |
|
self::$port = $port; |
122
|
54 |
|
self::$password = $password; |
123
|
54 |
|
self::$charset = $charset; |
124
|
54 |
|
self::$gtid = $gtid; |
125
|
54 |
|
self::$slaveId = $slaveId; |
126
|
54 |
|
self::$binLogFileName = $binLogFileName; |
127
|
54 |
|
self::$binLogPosition = $binLogPosition; |
128
|
54 |
|
self::$eventsOnly = $eventsOnly; |
129
|
54 |
|
self::$eventsIgnore = $eventsIgnore; |
130
|
54 |
|
self::$tablesOnly = $tablesOnly; |
131
|
54 |
|
self::$databasesOnly = $databasesOnly; |
132
|
54 |
|
self::$mariaDbGtid = $mariaGtid; |
133
|
54 |
|
self::$tableCacheSize = $tableCacheSize; |
134
|
54 |
|
self::$custom = $custom; |
135
|
54 |
|
self::$heartbeatPeriod = $heartbeatPeriod; |
136
|
54 |
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @throws ConfigException |
140
|
|
|
*/ |
141
|
54 |
|
public static function validate() |
142
|
|
|
{ |
143
|
54 |
View Code Duplication |
if (!empty(self::$user) && !is_string(self::$user)) { |
|
|
|
|
144
|
|
|
throw new ConfigException(ConfigException::USER_ERROR_MESSAGE, ConfigException::USER_ERROR_CODE); |
145
|
|
|
} |
146
|
54 |
|
if (!empty(self::$host)) { |
147
|
54 |
|
$ip = gethostbyname(self::$host); |
148
|
54 |
|
if (!filter_var($ip, FILTER_VALIDATE_IP)) { |
149
|
|
|
throw new ConfigException(ConfigException::IP_ERROR_MESSAGE, ConfigException::IP_ERROR_CODE); |
150
|
|
|
} |
151
|
54 |
|
} |
152
|
54 |
View Code Duplication |
if (!empty(self::$port) && !filter_var( |
|
|
|
|
153
|
54 |
|
self::$port, FILTER_VALIDATE_INT, ['options' => ['min_range' => 0]] |
154
|
54 |
|
) |
155
|
54 |
|
) { |
156
|
|
|
throw new ConfigException(ConfigException::PORT_ERROR_MESSAGE, ConfigException::PORT_ERROR_CODE); |
157
|
|
|
} |
158
|
54 |
|
if (!empty(self::$password) && !is_string(self::$password) && !is_numeric(self::$password)) { |
159
|
|
|
throw new ConfigException(ConfigException::PASSWORD_ERROR_MESSAGE, ConfigException::PASSWORD_ERROR_CODE); |
160
|
|
|
} |
161
|
54 |
View Code Duplication |
if (!empty(self::$charset) && !is_string(self::$charset)) { |
|
|
|
|
162
|
|
|
throw new ConfigException(ConfigException::CHARSET_ERROR_MESSAGE, ConfigException::CHARSET_ERROR_CODE); |
163
|
|
|
} |
164
|
54 |
|
if (!empty(self::$gtid) && !is_string(self::$gtid)) { |
165
|
|
|
foreach (explode(',', self::$gtid) as $gtid) { |
166
|
|
|
if (!(bool)preg_match( |
167
|
|
|
'/^([0-9a-fA-F]{8}(?:-[0-9a-fA-F]{4}){3}-[0-9a-fA-F]{12})((?::[0-9-]+)+)$/', $gtid, $matches |
168
|
|
|
) |
169
|
|
|
) { |
170
|
|
|
throw new ConfigException(ConfigException::GTID_ERROR_MESSAGE, ConfigException::GTID_ERROR_CODE); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
} |
174
|
54 |
View Code Duplication |
if (!empty(self::$slaveId) && !filter_var( |
|
|
|
|
175
|
54 |
|
self::$slaveId, FILTER_VALIDATE_INT, ['options' => ['min_range' => 0]] |
176
|
54 |
|
) |
177
|
54 |
|
) { |
178
|
|
|
throw new ConfigException(ConfigException::SLAVE_ID_ERROR_MESSAGE, ConfigException::SLAVE_ID_ERROR_CODE); |
179
|
|
|
} |
180
|
54 |
View Code Duplication |
if (!empty(self::$binLogFileName) && !is_string(self::$binLogFileName)) { |
|
|
|
|
181
|
|
|
throw new ConfigException( |
182
|
|
|
ConfigException::BIN_LOG_FILE_NAME_ERROR_MESSAGE, ConfigException::BIN_LOG_FILE_NAME_ERROR_CODE |
183
|
|
|
); |
184
|
|
|
} |
185
|
54 |
View Code Duplication |
if (!empty(self::$binLogPosition) && !filter_var( |
|
|
|
|
186
|
|
|
self::$binLogPosition, FILTER_VALIDATE_INT, ['options' => ['min_range' => 0]] |
187
|
|
|
) |
188
|
54 |
|
) { |
189
|
|
|
throw new ConfigException( |
190
|
|
|
ConfigException::BIN_LOG_FILE_POSITION_ERROR_MESSAGE, ConfigException::BIN_LOG_FILE_POSITION_ERROR_CODE |
191
|
|
|
); |
192
|
|
|
} |
193
|
54 |
View Code Duplication |
if (!empty(self::$mariaDbGtid) && !is_string(self::$mariaDbGtid)) { |
|
|
|
|
194
|
|
|
throw new ConfigException( |
195
|
|
|
ConfigException::MARIADBGTID_ERROR_MESSAGE, ConfigException::MARIADBGTID_ERROR_CODE |
196
|
|
|
); |
197
|
|
|
} |
198
|
54 |
View Code Duplication |
if (!filter_var(self::$tableCacheSize, FILTER_VALIDATE_INT, ['options' => ['min_range' => 0]])) { |
|
|
|
|
199
|
|
|
throw new ConfigException( |
200
|
|
|
ConfigException::TABLE_CACHE_SIZE_ERROR_MESSAGE, ConfigException::TABLE_CACHE_SIZE_ERROR_CODE |
201
|
|
|
); |
202
|
|
|
} |
203
|
54 |
|
if (0 !== self::$heartbeatPeriod && !filter_var( |
204
|
|
|
self::$heartbeatPeriod, FILTER_VALIDATE_INT, ['options' => ['min_range' => 1, 'max_range' => 4294967]] |
205
|
|
|
) |
206
|
54 |
|
) { |
207
|
|
|
throw new ConfigException( |
208
|
|
|
ConfigException::HEARTBEAT_PERIOD_ERROR_MESSAGE, ConfigException::HEARTBEAT_PERIOD_ERROR_CODE |
209
|
|
|
); |
210
|
|
|
} |
211
|
54 |
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @return array |
215
|
|
|
*/ |
216
|
|
|
public static function getCustom() |
217
|
|
|
{ |
218
|
|
|
return self::$custom; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @return string |
223
|
|
|
*/ |
224
|
54 |
|
public static function getUser() |
225
|
|
|
{ |
226
|
54 |
|
return self::$user; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* @return string |
231
|
|
|
*/ |
232
|
54 |
|
public static function getHost() |
233
|
|
|
{ |
234
|
54 |
|
return self::$host; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* @return int |
239
|
|
|
*/ |
240
|
54 |
|
public static function getPort() |
241
|
|
|
{ |
242
|
54 |
|
return self::$port; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* @return string |
247
|
|
|
*/ |
248
|
54 |
|
public static function getPassword() |
249
|
|
|
{ |
250
|
54 |
|
return self::$password; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* @return string |
255
|
|
|
*/ |
256
|
54 |
|
public static function getCharset() |
257
|
|
|
{ |
258
|
54 |
|
return self::$charset; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* @return string |
263
|
|
|
*/ |
264
|
54 |
|
public static function getGtid() |
265
|
|
|
{ |
266
|
54 |
|
return self::$gtid; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* @return string |
271
|
|
|
*/ |
272
|
54 |
|
public static function getMariaDbGtid() |
273
|
|
|
{ |
274
|
54 |
|
return self::$mariaDbGtid; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* @return int |
279
|
|
|
*/ |
280
|
54 |
|
public static function getSlaveId() |
281
|
|
|
{ |
282
|
54 |
|
return self::$slaveId; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* @return string |
287
|
|
|
*/ |
288
|
54 |
|
public static function getBinLogFileName() |
289
|
|
|
{ |
290
|
54 |
|
return self::$binLogFileName; |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* @return int |
295
|
|
|
*/ |
296
|
54 |
|
public static function getBinLogPosition() |
297
|
|
|
{ |
298
|
54 |
|
return self::$binLogPosition; |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* @return array |
303
|
|
|
*/ |
304
|
54 |
|
public static function getEventsOnly() |
305
|
|
|
{ |
306
|
54 |
|
return self::$eventsOnly; |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* @return array |
311
|
|
|
*/ |
312
|
54 |
|
public static function getEventsIgnore() |
313
|
|
|
{ |
314
|
54 |
|
return self::$eventsIgnore; |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* @return array |
319
|
|
|
*/ |
320
|
52 |
|
public static function getTablesOnly() |
321
|
|
|
{ |
322
|
52 |
|
return self::$tablesOnly; |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* @return array |
327
|
|
|
*/ |
328
|
52 |
|
public static function getDatabasesOnly() |
329
|
|
|
{ |
330
|
52 |
|
return self::$databasesOnly; |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* @return int |
335
|
|
|
*/ |
336
|
52 |
|
public static function getTableCacheSize() |
337
|
|
|
{ |
338
|
52 |
|
return self::$tableCacheSize; |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
/** |
342
|
|
|
* @param string $database |
343
|
|
|
* @return bool |
344
|
|
|
*/ |
345
|
52 |
|
public static function checkDataBasesOnly($database) |
346
|
|
|
{ |
347
|
52 |
|
return [] !== Config::getDatabasesOnly() && !in_array($database, Config::getDatabasesOnly(), true); |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
/** |
351
|
|
|
* @param string $table |
352
|
|
|
* @return bool |
353
|
|
|
*/ |
354
|
52 |
|
public static function checkTablesOnly($table) |
355
|
|
|
{ |
356
|
52 |
|
return [] !== Config::getTablesOnly() && !in_array($table, Config::getTablesOnly(), true); |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
/** |
360
|
|
|
* @param int $type |
361
|
|
|
* @return bool |
362
|
|
|
*/ |
363
|
54 |
|
public static function checkEvent($type) |
364
|
|
|
{ |
365
|
54 |
|
if ([] !== Config::getEventsOnly() && !in_array($type, Config::getEventsOnly(), true)) { |
366
|
3 |
|
return false; |
367
|
|
|
} |
368
|
|
|
|
369
|
54 |
|
if (in_array($type, Config::getEventsIgnore(), true)) { |
370
|
|
|
return false; |
371
|
|
|
} |
372
|
|
|
|
373
|
54 |
|
return true; |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
/** |
377
|
|
|
* @return int |
378
|
|
|
*/ |
379
|
54 |
|
public static function getHeartbeatPeriod() |
380
|
|
|
{ |
381
|
54 |
|
return self::$heartbeatPeriod; |
382
|
|
|
} |
383
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.