Skip to content

Commit 0105225

Browse files
authored
Cleancode (#16)
* change ip to host * tests * tests, support for JSON column type in mysql 5.7 (wip), some clean up * wip json binary decoding * wip json * clean up * clean up * Scrutinizer * Scrutinizer 2
1 parent 15c40a7 commit 0105225

28 files changed

+1181
-145
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ php-mysql-replication
33
[![Build Status](https://travis-ci.org/krowinski/php-mysql-replication.svg?branch=master)](https://travis-ci.org/krowinski/php-mysql-replication)
44
[![Latest Stable Version](https://poser.pugx.org/krowinski/php-mysql-replication/v/stable)](https://packagist.org/packages/krowinski/php-mysql-replication) [![Total Downloads](https://poser.pugx.org/krowinski/php-mysql-replication/downloads)](https://packagist.org/packages/krowinski/php-mysql-replication) [![Latest Unstable Version](https://poser.pugx.org/krowinski/php-mysql-replication/v/unstable)](https://packagist.org/packages/krowinski/php-mysql-replication)
55
[![SensioLabsInsight](https://insight.sensiolabs.com/projects/4a0e49d4-3802-41d3-bb32-0a8194d0fd4d/mini.png)](https://insight.sensiolabs.com/projects/4a0e49d4-3802-41d3-bb32-0a8194d0fd4d) [![License](https://poser.pugx.org/krowinski/php-mysql-replication/license)](https://packagist.org/packages/krowinski/php-mysql-replication)
6-
6+
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/krowinski/php-mysql-replication/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/krowinski/php-mysql-replication/?branch=master)
77

88
Pure PHP Implementation of MySQL replication protocol. This allow you to receive event like insert, update, delete with their data and raw SQL queries.
99

composer.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
"doctrine/dbal": "^2.5",
1414
"doctrine/collections": "^1.3",
1515
"ext-sockets": "*",
16-
"symfony/event-dispatcher": "^3.1",
17-
"symfony/dependency-injection": "^3.1"
16+
"symfony/event-dispatcher": ">=2.8 <=3.1",
17+
"symfony/dependency-injection": ">=2.8 <=3.1"
1818
},
1919
"require-dev": {
20-
"phpunit/phpunit": "*"
20+
"phpunit/phpunit": ">=4.8 <=5.6"
2121
},
2222
"license": "MIT",
2323
"authors": [

example/dump_events.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,5 @@ public function allEvents(EventDTO $event)
4848
// start consuming events
4949
while (1) {
5050
$binLogStream->binLogEvent();
51-
}
51+
}
52+

src/MySQLReplication/BinLog/BinLogConnect.php

+7-3
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ class BinLogConnect
3535
* @var BinLogAuth
3636
*/
3737
private $packAuth;
38+
/**
39+
* @var GtidService
40+
*/
41+
private $gtidService;
3842
/**
3943
* http://dev.mysql.com/doc/internals/en/auth-phase-fast-path.html 00 FE
4044
* @var array
@@ -96,7 +100,7 @@ public function connectToStream()
96100
socket_set_block($this->socket);
97101
socket_set_option($this->socket, SOL_SOCKET, SO_KEEPALIVE, 1);
98102

99-
if (false === socket_connect($this->socket, $this->config->getIp(), $this->config->getPort()))
103+
if (false === socket_connect($this->socket, $this->config->getHost(), $this->config->getPort()))
100104
{
101105
throw new BinLogException(socket_strerror(socket_last_error()), socket_last_error());
102106
}
@@ -125,7 +129,7 @@ public function getPacket($checkForOkByte = true)
125129
$header = $this->readFromSocket(4);
126130
if (false === $header)
127131
{
128-
return false;
132+
return '';
129133
}
130134
$dataLength = unpack('L', $header[0] . $header[1] . $header[2] . chr(0))[1];
131135

@@ -310,7 +314,7 @@ private function setBinLogDump()
310314
$this->execute('SET @slave_gtid_ignore_duplicates = 0');
311315
}
312316

313-
if ('' === $binFilePos || '' === $binFileName)
317+
if (0 === $binFilePos || '' === $binFileName)
314318
{
315319
$master = $this->mySQLRepository->getMasterStatus();
316320
$binFilePos = $master['Position'];

src/MySQLReplication/BinLog/BinLogServerInfo.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public static function parsePackage($pack)
4343
}
4444

4545
//connection_id 4 bytes
46-
self::$serverInfo['connection_id'] = $pack[$i] . $pack[++$i] . $pack[++$i] . $pack[++$i];
46+
self::$serverInfo['connection_id'] = unpack('I', $pack[$i] . $pack[++$i] . $pack[++$i] . $pack[++$i])[1];
4747
$i++;
4848

4949
//auth_plugin_data_part_1

src/MySQLReplication/BinaryDataReader/BinaryDataReader.php

+18-11
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ public function readInt24()
183183
public function readInt64()
184184
{
185185
$data = unpack('V*', $this->read(self::UNSIGNED_INT64_LENGTH));
186-
return bcadd($data[1], ($data[2] << 32));
186+
return bcadd($data[1], $data[2] << 32);
187187
}
188188

189189
/**
@@ -424,15 +424,22 @@ public function isComplete($size)
424424
*/
425425
public static function pack64bit($value)
426426
{
427-
return pack('C8',
428-
($value >> 0) & 0xFF,
429-
($value >> 8) & 0xFF,
430-
($value >> 16) & 0xFF,
431-
($value >> 24) & 0xFF,
432-
($value >> 32) & 0xFF,
433-
($value >> 40) & 0xFF,
434-
($value >> 48) & 0xFF,
435-
($value >> 56) & 0xFF
436-
);
427+
return pack('C8', ($value >> 0) & 0xFF, ($value >> 8) & 0xFF, ($value >> 16) & 0xFF, ($value >> 24) & 0xFF, ($value >> 32) & 0xFF, ($value >> 40) & 0xFF, ($value >> 48) & 0xFF, ($value >> 56) & 0xFF);
428+
}
429+
430+
/**
431+
* @return int
432+
*/
433+
public function getBinaryDataLength()
434+
{
435+
return strlen($this->binaryData);
436+
}
437+
438+
/**
439+
* @return string
440+
*/
441+
public function getBinaryData()
442+
{
443+
return $this->binaryData;
437444
}
438445
}

src/MySQLReplication/BinaryDataReader/BinaryDataReaderBuilder.php

+6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
*/
99
class BinaryDataReaderBuilder
1010
{
11+
/**
12+
* @var string
13+
*/
1114
private $binaryData = '';
1215

1316
/**
@@ -18,6 +21,9 @@ public function withBinaryData($binaryData)
1821
$this->binaryData = $binaryData;
1922
}
2023

24+
/**
25+
* @return BinaryDataReader
26+
*/
2127
public function build()
2228
{
2329
return new BinaryDataReader(

src/MySQLReplication/BinaryDataReader/BinaryDataReaderService.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
class BinaryDataReaderService
1010
{
1111
/**
12-
* @param $binaryData
12+
* @param string $binaryData
1313
* @return BinaryDataReader
1414
*/
1515
public function makePackageFromBinaryData($binaryData)

src/MySQLReplication/Config/Config.php

+12-9
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class Config
1717
/**
1818
* @var string
1919
*/
20-
private $ip;
20+
private $host;
2121
/**
2222
* @var int
2323
*/
@@ -74,7 +74,7 @@ class Config
7474
/**
7575
* Config constructor.
7676
* @param string $user
77-
* @param string $ip
77+
* @param string $host
7878
* @param int $port
7979
* @param string $password
8080
* @param string $dbName
@@ -91,7 +91,7 @@ class Config
9191
*/
9292
public function __construct(
9393
$user,
94-
$ip,
94+
$host,
9595
$port,
9696
$password,
9797
$dbName,
@@ -107,7 +107,7 @@ public function __construct(
107107
array $databasesOnly
108108
) {
109109
$this->user = $user;
110-
$this->ip = $ip;
110+
$this->host = $host;
111111
$this->port = $port;
112112
$this->password = $password;
113113
$this->dbName = $dbName;
@@ -132,9 +132,13 @@ public function validate()
132132
{
133133
throw new ConfigException(ConfigException::USER_ERROR_MESSAGE, ConfigException::USER_ERROR_CODE);
134134
}
135-
if (!empty($this->ip) && false === filter_var($this->ip, FILTER_VALIDATE_IP))
135+
if (!empty($this->host))
136136
{
137-
throw new ConfigException(ConfigException::IP_ERROR_MESSAGE, ConfigException::IP_ERROR_CODE);
137+
$ip = gethostbyname($this->host);
138+
if (false === filter_var($ip, FILTER_VALIDATE_IP))
139+
{
140+
throw new ConfigException(ConfigException::IP_ERROR_MESSAGE, ConfigException::IP_ERROR_CODE);
141+
}
138142
}
139143
if (!empty($this->port) && false === filter_var($this->port, FILTER_VALIDATE_INT, ['options' => ['min_range' => 0]]))
140144
{
@@ -191,9 +195,9 @@ public function getUser()
191195
/**
192196
* @return string
193197
*/
194-
public function getIp()
198+
public function getHost()
195199
{
196-
return $this->ip;
200+
return $this->host;
197201
}
198202

199203
/**
@@ -299,5 +303,4 @@ public function getDatabasesOnly()
299303
{
300304
return $this->databasesOnly;
301305
}
302-
303306
}

0 commit comments

Comments
 (0)