1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MySQLReplication\Config; |
4
|
|
|
|
5
|
|
|
use MySQLReplication\Config\Exception\ConfigException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class Config |
9
|
|
|
* @package MySQLReplication\Config |
10
|
|
|
*/ |
11
|
|
|
class Config |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
private $user; |
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
private $host; |
21
|
|
|
/** |
22
|
|
|
* @var int |
23
|
|
|
*/ |
24
|
|
|
private $port; |
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private $password; |
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
private $dbName; |
33
|
|
|
/** |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
private $charset; |
37
|
|
|
/** |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
private $gtid; |
41
|
|
|
/** |
42
|
|
|
* @var int |
43
|
|
|
*/ |
44
|
|
|
private $slaveId; |
45
|
|
|
/** |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
private $binLogFileName; |
49
|
|
|
/** |
50
|
|
|
* @var int |
51
|
|
|
*/ |
52
|
|
|
private $binLogPosition; |
53
|
|
|
/** |
54
|
|
|
* @var array |
55
|
|
|
*/ |
56
|
|
|
private $eventsOnly; |
57
|
|
|
/** |
58
|
|
|
* @var array |
59
|
|
|
*/ |
60
|
|
|
private $eventsIgnore; |
61
|
|
|
/** |
62
|
|
|
* @var array |
63
|
|
|
*/ |
64
|
|
|
private $tablesOnly; |
65
|
|
|
/** |
66
|
|
|
* @var array |
67
|
|
|
*/ |
68
|
|
|
private $databasesOnly; |
69
|
|
|
/** |
70
|
|
|
* @var string |
71
|
|
|
*/ |
72
|
|
|
private $mariaDbGtid; |
73
|
|
|
/** |
74
|
|
|
* @var int |
75
|
|
|
*/ |
76
|
|
|
private $tableCacheSize; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Config constructor. |
80
|
|
|
* @param string $user |
81
|
|
|
* @param string $host |
82
|
|
|
* @param int $port |
83
|
|
|
* @param string $password |
84
|
|
|
* @param string $dbName |
85
|
|
|
* @param string $charset |
86
|
|
|
* @param string $gtid |
87
|
|
|
* @param string $mariaGtid |
88
|
|
|
* @param int $slaveId |
89
|
|
|
* @param string $binLogFileName |
90
|
|
|
* @param $binLogPosition |
91
|
|
|
* @param array $eventsOnly |
92
|
|
|
* @param array $eventsIgnore |
93
|
|
|
* @param array $tablesOnly |
94
|
|
|
* @param array $databasesOnly |
95
|
|
|
* @param int $tableCacheSize |
96
|
|
|
*/ |
97
|
|
|
public function __construct( |
98
|
|
|
$user, |
99
|
|
|
$host, |
100
|
|
|
$port, |
101
|
|
|
$password, |
102
|
|
|
$dbName, |
103
|
|
|
$charset, |
104
|
|
|
$gtid, |
105
|
|
|
$mariaGtid, |
106
|
|
|
$slaveId, |
107
|
|
|
$binLogFileName, |
108
|
|
|
$binLogPosition, |
109
|
|
|
array $eventsOnly, |
110
|
|
|
array $eventsIgnore, |
111
|
|
|
array $tablesOnly, |
112
|
|
|
array $databasesOnly, |
113
|
|
|
$tableCacheSize |
114
|
|
|
) { |
115
|
|
|
$this->user = $user; |
116
|
|
|
$this->host = $host; |
117
|
|
|
$this->port = $port; |
118
|
|
|
$this->password = $password; |
119
|
|
|
$this->dbName = $dbName; |
120
|
|
|
$this->charset = $charset; |
121
|
|
|
$this->gtid = $gtid; |
122
|
|
|
$this->slaveId = $slaveId; |
123
|
|
|
$this->binLogFileName = $binLogFileName; |
124
|
|
|
$this->binLogPosition = $binLogPosition; |
125
|
|
|
$this->eventsOnly = $eventsOnly; |
126
|
|
|
$this->eventsIgnore = $eventsIgnore; |
127
|
|
|
$this->tablesOnly = $tablesOnly; |
128
|
|
|
$this->databasesOnly = $databasesOnly; |
129
|
|
|
$this->mariaDbGtid = $mariaGtid; |
130
|
|
|
$this->tableCacheSize = $tableCacheSize; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @throws ConfigException |
135
|
|
|
*/ |
136
|
|
|
public function validate() |
137
|
|
|
{ |
138
|
|
View Code Duplication |
if (!empty($this->user) && false === is_string($this->user)) |
|
|
|
|
139
|
|
|
{ |
140
|
|
|
throw new ConfigException(ConfigException::USER_ERROR_MESSAGE, ConfigException::USER_ERROR_CODE); |
141
|
|
|
} |
142
|
|
|
if (!empty($this->host)) |
143
|
|
|
{ |
144
|
|
|
$ip = gethostbyname($this->host); |
145
|
|
|
if (false === filter_var($ip, FILTER_VALIDATE_IP)) |
146
|
|
|
{ |
147
|
|
|
throw new ConfigException(ConfigException::IP_ERROR_MESSAGE, ConfigException::IP_ERROR_CODE); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
View Code Duplication |
if (!empty($this->port) && false === filter_var($this->port, FILTER_VALIDATE_INT, ['options' => ['min_range' => 0]])) |
|
|
|
|
151
|
|
|
{ |
152
|
|
|
throw new ConfigException(ConfigException::PORT_ERROR_MESSAGE, ConfigException::PORT_ERROR_CODE); |
153
|
|
|
} |
154
|
|
|
if (!empty($this->password) && false === is_string($this->password) && false === is_numeric($this->password)) |
155
|
|
|
{ |
156
|
|
|
throw new ConfigException(ConfigException::PASSWORD_ERROR_MESSAGE, ConfigException::PASSWORD_ERROR_CODE); |
157
|
|
|
} |
158
|
|
View Code Duplication |
if (!empty($this->dbName) && false === is_string($this->dbName)) |
|
|
|
|
159
|
|
|
{ |
160
|
|
|
throw new ConfigException(ConfigException::DB_NAME_ERROR_MESSAGE, ConfigException::DB_NAME_ERROR_CODE); |
161
|
|
|
} |
162
|
|
View Code Duplication |
if (!empty($this->charset) && false === is_string($this->charset)) |
|
|
|
|
163
|
|
|
{ |
164
|
|
|
throw new ConfigException(ConfigException::CHARSET_ERROR_MESSAGE, ConfigException::CHARSET_ERROR_CODE); |
165
|
|
|
} |
166
|
|
|
if (!empty($this->gtid) && false === is_string($this->gtid)) |
167
|
|
|
{ |
168
|
|
|
foreach (explode(',', $this->gtid) as $gtid) |
169
|
|
|
{ |
170
|
|
|
if (false === (bool)preg_match('/^([0-9a-fA-F]{8}(?:-[0-9a-fA-F]{4}){3}-[0-9a-fA-F]{12})((?::[0-9-]+)+)$/', $gtid, $matches)) |
171
|
|
|
{ |
172
|
|
|
throw new ConfigException(ConfigException::GTID_ERROR_MESSAGE, ConfigException::GTID_ERROR_CODE); |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
View Code Duplication |
if (!empty($this->slaveId) && false === filter_var($this->slaveId, FILTER_VALIDATE_INT, ['options' => ['min_range' => 0]])) |
|
|
|
|
177
|
|
|
{ |
178
|
|
|
throw new ConfigException(ConfigException::SLAVE_ID_ERROR_MESSAGE, ConfigException::SLAVE_ID_ERROR_CODE); |
179
|
|
|
} |
180
|
|
View Code Duplication |
if (!empty($this->binLogFileName) && false === is_string($this->binLogFileName)) |
|
|
|
|
181
|
|
|
{ |
182
|
|
|
throw new ConfigException(ConfigException::BIN_LOG_FILE_NAME_ERROR_MESSAGE, ConfigException::BIN_LOG_FILE_NAME_ERROR_CODE); |
183
|
|
|
} |
184
|
|
View Code Duplication |
if (!empty($this->binLogPosition) && false === filter_var($this->binLogPosition, FILTER_VALIDATE_INT, ['options' => ['min_range' => 0]])) |
|
|
|
|
185
|
|
|
{ |
186
|
|
|
throw new ConfigException(ConfigException::BIN_LOG_FILE_POSITION_ERROR_MESSAGE, ConfigException::BIN_LOG_FILE_POSITION_ERROR_CODE); |
187
|
|
|
} |
188
|
|
View Code Duplication |
if (!empty($this->mariaDbGtid) && false === is_string($this->mariaDbGtid)) |
|
|
|
|
189
|
|
|
{ |
190
|
|
|
throw new ConfigException(ConfigException::MARIADBGTID_ERROR_MESSAGE, ConfigException::MARIADBGTID_ERROR_CODE); |
191
|
|
|
} |
192
|
|
View Code Duplication |
if (false === filter_var($this->tableCacheSize, FILTER_VALIDATE_INT, ['options' => ['min_range' => 0]])) |
|
|
|
|
193
|
|
|
{ |
194
|
|
|
throw new ConfigException(ConfigException::TABLE_CACHE_SIZE_ERROR_MESSAGE, ConfigException::TABLE_CACHE_SIZE_ERROR_CODE); |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @return string |
200
|
|
|
*/ |
201
|
|
|
public function getUser() |
202
|
|
|
{ |
203
|
|
|
return $this->user; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @return string |
208
|
|
|
*/ |
209
|
|
|
public function getHost() |
210
|
|
|
{ |
211
|
|
|
return $this->host; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @return int |
216
|
|
|
*/ |
217
|
|
|
public function getPort() |
218
|
|
|
{ |
219
|
|
|
return $this->port; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @return string |
224
|
|
|
*/ |
225
|
|
|
public function getPassword() |
226
|
|
|
{ |
227
|
|
|
return $this->password; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* @return string |
232
|
|
|
*/ |
233
|
|
|
public function getDbName() |
234
|
|
|
{ |
235
|
|
|
return $this->dbName; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* @return string |
240
|
|
|
*/ |
241
|
|
|
public function getCharset() |
242
|
|
|
{ |
243
|
|
|
return $this->charset; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* @return string |
248
|
|
|
*/ |
249
|
|
|
public function getGtid() |
250
|
|
|
{ |
251
|
|
|
return $this->gtid; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* @return string |
256
|
|
|
*/ |
257
|
|
|
public function getMariaDbGtid() |
258
|
|
|
{ |
259
|
|
|
return $this->mariaDbGtid; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* @return int |
264
|
|
|
*/ |
265
|
|
|
public function getSlaveId() |
266
|
|
|
{ |
267
|
|
|
return $this->slaveId; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* @return string |
272
|
|
|
*/ |
273
|
|
|
public function getBinLogFileName() |
274
|
|
|
{ |
275
|
|
|
return $this->binLogFileName; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* @return int |
280
|
|
|
*/ |
281
|
|
|
public function getBinLogPosition() |
282
|
|
|
{ |
283
|
|
|
return $this->binLogPosition; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* @return array |
288
|
|
|
*/ |
289
|
|
|
public function getEventsOnly() |
290
|
|
|
{ |
291
|
|
|
return $this->eventsOnly; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* @return array |
296
|
|
|
*/ |
297
|
|
|
public function getEventsIgnore() |
298
|
|
|
{ |
299
|
|
|
return $this->eventsIgnore; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* @return array |
304
|
|
|
*/ |
305
|
|
|
public function getTablesOnly() |
306
|
|
|
{ |
307
|
|
|
return $this->tablesOnly; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* @return array |
312
|
|
|
*/ |
313
|
|
|
public function getDatabasesOnly() |
314
|
|
|
{ |
315
|
|
|
return $this->databasesOnly; |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
/** |
319
|
|
|
* @return int |
320
|
|
|
*/ |
321
|
|
|
public function getTableCacheSize() |
322
|
|
|
{ |
323
|
|
|
return $this->tableCacheSize; |
324
|
|
|
} |
325
|
|
|
} |
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.