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
|
75 |
|
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
|
|
|
float $heartbeatPeriod |
46
|
|
|
) { |
47
|
75 |
|
self::$user = $user; |
48
|
75 |
|
self::$host = $host; |
49
|
75 |
|
self::$port = $port; |
50
|
75 |
|
self::$password = $password; |
51
|
75 |
|
self::$charset = $charset; |
52
|
75 |
|
self::$gtid = $gtid; |
53
|
75 |
|
self::$slaveId = $slaveId; |
54
|
75 |
|
self::$binLogFileName = $binLogFileName; |
55
|
75 |
|
self::$binLogPosition = $binLogPosition; |
56
|
75 |
|
self::$eventsOnly = $eventsOnly; |
57
|
75 |
|
self::$eventsIgnore = $eventsIgnore; |
58
|
75 |
|
self::$tablesOnly = $tablesOnly; |
59
|
75 |
|
self::$databasesOnly = $databasesOnly; |
60
|
75 |
|
self::$mariaDbGtid = $mariaGtid; |
61
|
75 |
|
self::$tableCacheSize = $tableCacheSize; |
62
|
75 |
|
self::$custom = $custom; |
63
|
75 |
|
self::$heartbeatPeriod = $heartbeatPeriod; |
64
|
75 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @throws ConfigException |
68
|
|
|
*/ |
69
|
71 |
|
public static function validate(): void |
70
|
|
|
{ |
71
|
71 |
|
if (!empty(self::$host)) { |
72
|
71 |
|
$ip = gethostbyname(self::$host); |
73
|
71 |
|
if (false === filter_var($ip, FILTER_VALIDATE_IP)) { |
74
|
1 |
|
throw new ConfigException(ConfigException::IP_ERROR_MESSAGE, ConfigException::IP_ERROR_CODE); |
75
|
|
|
} |
76
|
|
|
} |
77
|
70 |
|
if (!empty(self::$port) && false === filter_var( |
78
|
70 |
|
self::$port, FILTER_VALIDATE_INT, ['options' => ['min_range' => 0]] |
79
|
|
|
)) { |
80
|
1 |
|
throw new ConfigException(ConfigException::PORT_ERROR_MESSAGE, ConfigException::PORT_ERROR_CODE); |
81
|
|
|
} |
82
|
69 |
|
if (!empty(self::$gtid)) { |
83
|
1 |
|
foreach (explode(',', self::$gtid) as $gtid) { |
84
|
1 |
|
if (!(bool)preg_match( |
85
|
1 |
|
'/^([0-9a-fA-F]{8}(?:-[0-9a-fA-F]{4}){3}-[0-9a-fA-F]{12})((?::[0-9-]+)+)$/', $gtid, $matches |
86
|
|
|
)) { |
87
|
1 |
|
throw new ConfigException(ConfigException::GTID_ERROR_MESSAGE, ConfigException::GTID_ERROR_CODE); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
} |
91
|
68 |
|
if (!empty(self::$slaveId) && false === filter_var( |
92
|
68 |
|
self::$slaveId, FILTER_VALIDATE_INT, ['options' => ['min_range' => 0]] |
93
|
|
|
)) { |
94
|
1 |
|
throw new ConfigException(ConfigException::SLAVE_ID_ERROR_MESSAGE, ConfigException::SLAVE_ID_ERROR_CODE); |
95
|
|
|
} |
96
|
67 |
|
if (false === filter_var(self::$binLogPosition, FILTER_VALIDATE_INT, ['options' => ['min_range' => 0]])) { |
97
|
1 |
|
throw new ConfigException( |
98
|
1 |
|
ConfigException::BIN_LOG_FILE_POSITION_ERROR_MESSAGE, ConfigException::BIN_LOG_FILE_POSITION_ERROR_CODE |
99
|
|
|
); |
100
|
|
|
} |
101
|
66 |
|
if (false === filter_var(self::$tableCacheSize, FILTER_VALIDATE_INT, ['options' => ['min_range' => 0]])) { |
102
|
1 |
|
throw new ConfigException( |
103
|
1 |
|
ConfigException::TABLE_CACHE_SIZE_ERROR_MESSAGE, ConfigException::TABLE_CACHE_SIZE_ERROR_CODE |
104
|
|
|
); |
105
|
|
|
} |
106
|
65 |
|
if (0.0 !== self::$heartbeatPeriod && false === ( |
107
|
65 |
|
self::$heartbeatPeriod >= 0.001 && self::$heartbeatPeriod <= 4294967.0 |
108
|
|
|
)) { |
109
|
2 |
|
throw new ConfigException( |
110
|
2 |
|
ConfigException::HEARTBEAT_PERIOD_ERROR_MESSAGE, ConfigException::HEARTBEAT_PERIOD_ERROR_CODE |
111
|
|
|
); |
112
|
|
|
} |
113
|
63 |
|
} |
114
|
|
|
|
115
|
|
|
public static function getCustom(): array |
116
|
|
|
{ |
117
|
|
|
return self::$custom; |
118
|
|
|
} |
119
|
|
|
|
120
|
58 |
|
public static function getUser(): string |
121
|
|
|
{ |
122
|
58 |
|
return self::$user; |
123
|
|
|
} |
124
|
|
|
|
125
|
58 |
|
public static function getHost(): string |
126
|
|
|
{ |
127
|
58 |
|
return self::$host; |
128
|
|
|
} |
129
|
|
|
|
130
|
58 |
|
public static function getPort(): int |
131
|
|
|
{ |
132
|
58 |
|
return self::$port; |
133
|
|
|
} |
134
|
|
|
|
135
|
58 |
|
public static function getPassword(): string |
136
|
|
|
{ |
137
|
58 |
|
return self::$password; |
138
|
|
|
} |
139
|
|
|
|
140
|
58 |
|
public static function getCharset(): string |
141
|
|
|
{ |
142
|
58 |
|
return self::$charset; |
143
|
|
|
} |
144
|
|
|
|
145
|
58 |
|
public static function getGtid(): string |
146
|
|
|
{ |
147
|
58 |
|
return self::$gtid; |
148
|
|
|
} |
149
|
|
|
|
150
|
58 |
|
public static function getMariaDbGtid(): string |
151
|
|
|
{ |
152
|
58 |
|
return self::$mariaDbGtid; |
153
|
|
|
} |
154
|
|
|
|
155
|
58 |
|
public static function getSlaveId(): int |
156
|
|
|
{ |
157
|
58 |
|
return self::$slaveId; |
158
|
|
|
} |
159
|
|
|
|
160
|
58 |
|
public static function getBinLogFileName(): string |
161
|
|
|
{ |
162
|
58 |
|
return self::$binLogFileName; |
163
|
|
|
} |
164
|
|
|
|
165
|
58 |
|
public static function getBinLogPosition(): int |
166
|
|
|
{ |
167
|
58 |
|
return self::$binLogPosition; |
168
|
|
|
} |
169
|
|
|
|
170
|
63 |
|
public static function getTableCacheSize(): int |
171
|
|
|
{ |
172
|
63 |
|
return self::$tableCacheSize; |
173
|
|
|
} |
174
|
|
|
|
175
|
55 |
|
public static function checkDataBasesOnly(string $database): bool |
176
|
|
|
{ |
177
|
55 |
|
return [] !== self::getDatabasesOnly() && !in_array($database, self::getDatabasesOnly(), true); |
178
|
|
|
} |
179
|
|
|
|
180
|
55 |
|
public static function getDatabasesOnly(): array |
181
|
|
|
{ |
182
|
55 |
|
return self::$databasesOnly; |
183
|
|
|
} |
184
|
|
|
|
185
|
55 |
|
public static function checkTablesOnly(string $table): bool |
186
|
|
|
{ |
187
|
55 |
|
return [] !== self::getTablesOnly() && !in_array($table, self::getTablesOnly(), true); |
188
|
|
|
} |
189
|
|
|
|
190
|
55 |
|
public static function getTablesOnly(): array |
191
|
|
|
{ |
192
|
55 |
|
return self::$tablesOnly; |
193
|
|
|
} |
194
|
|
|
|
195
|
59 |
|
public static function checkEvent(int $type): bool |
196
|
|
|
{ |
197
|
59 |
|
if ([] !== self::getEventsOnly() && !in_array($type, self::getEventsOnly(), true)) { |
198
|
5 |
|
return false; |
199
|
|
|
} |
200
|
|
|
|
201
|
59 |
|
if (in_array($type, self::getEventsIgnore(), true)) { |
202
|
1 |
|
return false; |
203
|
|
|
} |
204
|
|
|
|
205
|
59 |
|
return true; |
206
|
|
|
} |
207
|
|
|
|
208
|
59 |
|
public static function getEventsOnly(): array |
209
|
|
|
{ |
210
|
59 |
|
return self::$eventsOnly; |
211
|
|
|
} |
212
|
|
|
|
213
|
59 |
|
public static function getEventsIgnore(): array |
214
|
|
|
{ |
215
|
59 |
|
return self::$eventsIgnore; |
216
|
|
|
} |
217
|
|
|
|
218
|
63 |
|
public static function getHeartbeatPeriod(): float |
219
|
|
|
{ |
220
|
63 |
|
return self::$heartbeatPeriod; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
public function jsonSerialize() |
224
|
|
|
{ |
225
|
|
|
return get_class_vars(self::class); |
226
|
|
|
} |
227
|
|
|
} |