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