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