Skip to content

Commit b60a417

Browse files
committed
added validator for config, callback example added
1 parent 0016ed4 commit b60a417

File tree

9 files changed

+149
-20
lines changed

9 files changed

+149
-20
lines changed

example/dump_events_callback.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
error_reporting(E_ALL);
3+
ini_set('display_errors', 1);
4+
date_default_timezone_set('UTC');
5+
include __DIR__ . '/../vendor/autoload.php';
6+
7+
use MySQLReplication\Event\DTO\DeleteRowsDTO;
8+
use MySQLReplication\Event\DTO\EventDTO;
9+
use MySQLReplication\Event\DTO\GTIDLogDTO;
10+
use MySQLReplication\Event\DTO\QueryDTO;
11+
use MySQLReplication\Event\DTO\RotateDTO;
12+
use MySQLReplication\Event\DTO\TableMapDTO;
13+
use MySQLReplication\Event\DTO\UpdateRowsDTO;
14+
use MySQLReplication\Event\DTO\WriteRowsDTO;
15+
use MySQLReplication\Event\DTO\XidDTO;
16+
use MySQLReplication\MySQLReplicationFactory;
17+
use MySQLReplication\Config\ConfigService;
18+
19+
$binLogStream = new MySQLReplicationFactory(
20+
(new ConfigService())->makeConfigFromArray([
21+
'user' => 'root',
22+
'ip' => '192.168.1.100',
23+
'password' => 'root'
24+
])
25+
);
26+
27+
$binLogStream->parseBinLogUsingCallback(function($event)
28+
{
29+
/** @var DeleteRowsDTO|EventDTO|GTIDLogDTO|QueryDTO|RotateDTO|TableMapDTO|UpdateRowsDTO|WriteRowsDTO|XidDTO $event */
30+
echo $event;
31+
});

src/MySQLReplication/BinLog/BinLogConnect.php

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
namespace MySQLReplication\BinLog;
33

4-
use MySQLReplication\BinLog\Exception\BinLogException;
4+
use MySQLReplication\BinLog\Exception\ConfigException;
55
use MySQLReplication\Config\Config;
66
use MySQLReplication\Repository\MySQLRepository;
77
use MySQLReplication\Definitions\ConstCapabilityFlags;
@@ -95,21 +95,16 @@ public function getCheckSum()
9595
*/
9696
public function connectToStream()
9797
{
98-
if (false === filter_var($this->config->getIp(), FILTER_VALIDATE_IP))
99-
{
100-
throw new BinLogException('Given parameter "' . $this->config->getIp() . '" is not a valid IP');
101-
}
102-
10398
if (false === ($this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)))
10499
{
105-
throw new BinLogException('Unable to create a socket:' . socket_strerror(socket_last_error()), socket_last_error());
100+
throw new ConfigException('Unable to create a socket:' . socket_strerror(socket_last_error()), socket_last_error());
106101
}
107102
socket_set_block($this->socket);
108103
socket_set_option($this->socket, SOL_SOCKET, SO_KEEPALIVE, 1);
109104

110105
if (false === socket_connect($this->socket, $this->config->getIp(), $this->config->getPort()))
111106
{
112-
throw new BinLogException(socket_strerror(socket_last_error()), socket_last_error());
107+
throw new ConfigException(socket_strerror(socket_last_error()), socket_last_error());
113108
}
114109

115110
$this->serverInfo();
@@ -169,7 +164,7 @@ public function isWriteSuccessful($packet)
169164
$error_msg .= $packet[$i];
170165
}
171166

172-
throw new BinLogException($error_msg, $error_code);
167+
throw new ConfigException($error_msg, $error_code);
173168
}
174169
}
175170

@@ -182,15 +177,15 @@ private function readFromSocket($length)
182177
{
183178
if ($length == 5)
184179
{
185-
throw new BinLogException('read 5 bytes from mysql server has gone away');
180+
throw new ConfigException('read 5 bytes from mysql server has gone away');
186181
}
187182

188183
if ($length === socket_recv($this->socket, $buf, $length, MSG_WAITALL))
189184
{
190185
return $buf;
191186
}
192187

193-
throw new BinLogException(socket_strerror(socket_last_error()), socket_last_error());
188+
throw new ConfigException(socket_strerror(socket_last_error()), socket_last_error());
194189
}
195190

196191
/**
@@ -217,7 +212,7 @@ private function writeToSocket($data)
217212
{
218213
if (false === socket_write($this->socket, $data, strlen($data)))
219214
{
220-
throw new BinLogException('Unable to write to socket: ' . socket_strerror(socket_last_error()), socket_last_error());
215+
throw new ConfigException('Unable to write to socket: ' . socket_strerror(socket_last_error()), socket_last_error());
221216
}
222217
}
223218

src/MySQLReplication/BinaryDataReader/BinaryDataReader.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace MySQLReplication\BinaryDataReader;
44

5+
use MySQLReplication\BinaryDataReader\Exception\BinaryDataReaderException;
6+
57
/**
68
* Class BinaryDataReader
79
* @package MySQLReplication\BinaryDataReader

src/MySQLReplication/BinaryDataReader/BinaryDataReaderException.php renamed to src/MySQLReplication/BinaryDataReader/Exception/BinaryDataReaderException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace MySQLReplication\BinaryDataReader;
3+
namespace MySQLReplication\BinaryDataReader\Exception;
44

55
use MySQLReplication\Exception\MySQLReplicationException;
66

src/MySQLReplication/Config/Config.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace MySQLReplication\Config;
44

5+
use MySQLReplication\Config\Exception\ConfigException;
6+
57
/**
68
* Class Config
79
* @package MySQLReplication\Config
@@ -114,6 +116,59 @@ public function __construct(
114116
$this->databasesOnly = $databasesOnly;
115117
}
116118

119+
/**
120+
* @throws ConfigException
121+
*/
122+
public function validate()
123+
{
124+
if (!empty($this->user) && false === is_string($this->user))
125+
{
126+
throw new ConfigException(ConfigException::USER_ERROR_MESSAGE, ConfigException::USER_ERROR_CODE);
127+
}
128+
if (!empty($this->ip) && false === filter_var($this->ip, FILTER_VALIDATE_IP))
129+
{
130+
throw new ConfigException(ConfigException::IP_ERROR_MESSAGE, ConfigException::IP_ERROR_CODE);
131+
}
132+
if (!empty($this->port) && false === filter_var($this->port, FILTER_VALIDATE_INT, ['options' => ['min_range' => 0]]))
133+
{
134+
throw new ConfigException(ConfigException::PORT_ERROR_MESSAGE, ConfigException::PORT_ERROR_CODE);
135+
}
136+
if (!empty($this->password) && false === is_string($this->password) && false === is_numeric($this->password))
137+
{
138+
throw new ConfigException(ConfigException::PASSWORD_ERROR_MESSAGE, ConfigException::PASSWORD_ERROR_CODE);
139+
}
140+
if (!empty($this->dbName) && false === is_string($this->dbName))
141+
{
142+
throw new ConfigException(ConfigException::DB_NAME_ERROR_MESSAGE, ConfigException::DB_NAME_ERROR_CODE);
143+
}
144+
if (!empty($this->charset) && false === is_string($this->charset))
145+
{
146+
throw new ConfigException(ConfigException::CHARSET_ERROR_MESSAGE, ConfigException::CHARSET_ERROR_CODE);
147+
}
148+
if (!empty($this->gtid) && false === is_string($this->gtid))
149+
{
150+
foreach (explode(',', $this->gtid) as $gtid)
151+
{
152+
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))
153+
{
154+
throw new ConfigException(ConfigException::GTID_ERROR_MESSAGE, ConfigException::GTID_ERROR_CODE);
155+
}
156+
}
157+
}
158+
if (!empty($this->slaveId) && false === filter_var($this->slaveId, FILTER_VALIDATE_INT, ['options' => ['min_range' => 0]]))
159+
{
160+
throw new ConfigException(ConfigException::SLAVE_ID_ERROR_MESSAGE, ConfigException::SLAVE_ID_ERROR_CODE);
161+
}
162+
if (!empty($this->binLogFileName) && false === is_string($this->binLogFileName))
163+
{
164+
throw new ConfigException(ConfigException::BIN_LOG_FILE_NAME_ERROR_MESSAGE, ConfigException::BIN_LOG_FILE_NAME_ERROR_CODE);
165+
}
166+
if (!empty($this->binLogPosition) && false === filter_var($this->binLogPosition, FILTER_VALIDATE_INT, ['options' => ['min_range' => 0]]))
167+
{
168+
throw new ConfigException(ConfigException::BIN_LOG_FILE_POSITION_ERROR_MESSAGE, ConfigException::BIN_LOG_FILE_POSITION_ERROR_CODE);
169+
}
170+
}
171+
117172
/**
118173
* @return string
119174
*/
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace MySQLReplication\Config\Exception;
4+
5+
use MySQLReplication\Exception\MySQLReplicationException;
6+
7+
class ConfigException extends MySQLReplicationException
8+
{
9+
const USER_ERROR_MESSAGE = 'Incorrect user given';
10+
const USER_ERROR_CODE = 1;
11+
const IP_ERROR_MESSAGE = 'Incorrect IP given';
12+
const IP_ERROR_CODE = 2;
13+
const PORT_ERROR_MESSAGE = 'Incorrect port given should be numeric ';
14+
const PORT_ERROR_CODE = 3;
15+
const PASSWORD_ERROR_MESSAGE = 'Incorrect password type';
16+
const PASSWORD_ERROR_CODE = 4;
17+
const DB_NAME_ERROR_MESSAGE = 'Incorrect db name type';
18+
const DB_NAME_ERROR_CODE = 5;
19+
const CHARSET_ERROR_MESSAGE = 'Incorrect charset type';
20+
const CHARSET_ERROR_CODE = 6;
21+
const GTID_ERROR_MESSAGE = 'Incorrect gtid';
22+
const GTID_ERROR_CODE = 7;
23+
const SLAVE_ID_ERROR_MESSAGE = 'Incorrect slave id type';
24+
const SLAVE_ID_ERROR_CODE = 8;
25+
const BIN_LOG_FILE_NAME_ERROR_MESSAGE = 'Incorrect binlog name type';
26+
const BIN_LOG_FILE_NAME_ERROR_CODE = 9;
27+
const BIN_LOG_FILE_POSITION_ERROR_MESSAGE = 'Incorrect binlog position type';
28+
const BIN_LOG_FILE_POSITION_ERROR_CODE = 10;
29+
}

src/MySQLReplication/Event/RowEvent/Columns.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace MySQLReplication\Event\RowEvent;
44

55
use MySQLReplication\BinaryDataReader\BinaryDataReader;
6-
use MySQLReplication\BinLog\Exception\BinLogException;
6+
use MySQLReplication\BinLog\Exception\ConfigException;
77
use MySQLReplication\Definitions\ConstFieldType;
88

99
/**
@@ -128,7 +128,7 @@ private static function getFieldSpecialValues($columnSchema)
128128
}
129129
else
130130
{
131-
throw new BinLogException('Type not handled! - ' . self::$field['type']);
131+
throw new ConfigException('Type not handled! - ' . self::$field['type']);
132132
}
133133
}
134134
}

src/MySQLReplication/MySQLReplicationFactory.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ class MySQLReplicationFactory
6060
*/
6161
public function __construct(Config $config)
6262
{
63+
$config->validate();
64+
6365
$this->connection = DriverManager::getConnection([
6466
'user' => $config->getUser(),
6567
'password' => $config->getPassword(),
@@ -93,4 +95,19 @@ public function getBinLogEvent()
9395
{
9496
return $this->event->consume();
9597
}
98+
99+
/**
100+
* @param callable $callback
101+
*/
102+
public function parseBinLogUsingCallback(Callable $callback)
103+
{
104+
while (1)
105+
{
106+
$event = $this->event->consume();
107+
if (!is_null($event))
108+
{
109+
call_user_func($callback, $event);
110+
}
111+
}
112+
}
96113
}

tests/Unit/BinaryDataReader/BinaryDataReaderTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function shouldReadCodedBinary()
4444

4545
/**
4646
* @test
47-
* @expectedException \MySQLReplication\BinaryDataReader\BinaryDataReaderException
47+
* @expectedException \MySQLReplication\BinaryDataReader\Exception\BinaryDataReaderException
4848
*/
4949
public function shouldThrowErrorOnUnknownCodedBinary()
5050
{
@@ -71,7 +71,7 @@ public function dataProviderForUInt()
7171
* @param $size
7272
* @param $data
7373
* @param $expected
74-
* @throws \MySQLReplication\BinaryDataReader\BinaryDataReaderException
74+
* @throws \MySQLReplication\BinaryDataReader\Exception\BinaryDataReaderException
7575
*/
7676
public function shouldReadUIntBySize($size, $data, $expected)
7777
{
@@ -80,7 +80,7 @@ public function shouldReadUIntBySize($size, $data, $expected)
8080

8181
/**
8282
* @test
83-
* @expectedException \MySQLReplication\BinaryDataReader\BinaryDataReaderException
83+
* @expectedException \MySQLReplication\BinaryDataReader\Exception\BinaryDataReaderException
8484
*/
8585
public function shouldThrowErrorOnReadUIntBySizeNotSupported()
8686
{
@@ -104,7 +104,7 @@ public function dataProviderForBeInt()
104104
* @param $size
105105
* @param $data
106106
* @param $expected
107-
* @throws \MySQLReplication\BinaryDataReader\BinaryDataReaderException
107+
* @throws \MySQLReplication\BinaryDataReader\Exception\BinaryDataReaderException
108108
*/
109109
public function shouldReadIntBeBySize($size, $data, $expected)
110110
{
@@ -113,7 +113,7 @@ public function shouldReadIntBeBySize($size, $data, $expected)
113113

114114
/**
115115
* @test
116-
* @expectedException \MySQLReplication\BinaryDataReader\BinaryDataReaderException
116+
* @expectedException \MySQLReplication\BinaryDataReader\Exception\BinaryDataReaderException
117117
*/
118118
public function shouldThrowErrorOnReadIntBeBySizeNotSupported()
119119
{

0 commit comments

Comments
 (0)