Skip to content

Commit

Permalink
Cleancode (#16)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
krowinski authored Nov 19, 2016
1 parent 15c40a7 commit 0105225
Show file tree
Hide file tree
Showing 28 changed files with 1,181 additions and 145 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ php-mysql-replication
[![Build Status](https://travis-ci.org/krowinski/php-mysql-replication.svg?branch=master)](https://travis-ci.org/krowinski/php-mysql-replication)
[![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)
[![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)

[![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)

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

Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
"doctrine/dbal": "^2.5",
"doctrine/collections": "^1.3",
"ext-sockets": "*",
"symfony/event-dispatcher": "^3.1",
"symfony/dependency-injection": "^3.1"
"symfony/event-dispatcher": ">=2.8 <=3.1",
"symfony/dependency-injection": ">=2.8 <=3.1"
},
"require-dev": {
"phpunit/phpunit": "*"
"phpunit/phpunit": ">=4.8 <=5.6"
},
"license": "MIT",
"authors": [
Expand Down
3 changes: 2 additions & 1 deletion example/dump_events.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,5 @@ public function allEvents(EventDTO $event)
// start consuming events
while (1) {
$binLogStream->binLogEvent();
}
}

10 changes: 7 additions & 3 deletions src/MySQLReplication/BinLog/BinLogConnect.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ class BinLogConnect
* @var BinLogAuth
*/
private $packAuth;
/**
* @var GtidService
*/
private $gtidService;
/**
* http://dev.mysql.com/doc/internals/en/auth-phase-fast-path.html 00 FE
* @var array
Expand Down Expand Up @@ -96,7 +100,7 @@ public function connectToStream()
socket_set_block($this->socket);
socket_set_option($this->socket, SOL_SOCKET, SO_KEEPALIVE, 1);

if (false === socket_connect($this->socket, $this->config->getIp(), $this->config->getPort()))
if (false === socket_connect($this->socket, $this->config->getHost(), $this->config->getPort()))
{
throw new BinLogException(socket_strerror(socket_last_error()), socket_last_error());
}
Expand Down Expand Up @@ -125,7 +129,7 @@ public function getPacket($checkForOkByte = true)
$header = $this->readFromSocket(4);
if (false === $header)
{
return false;
return '';
}
$dataLength = unpack('L', $header[0] . $header[1] . $header[2] . chr(0))[1];

Expand Down Expand Up @@ -310,7 +314,7 @@ private function setBinLogDump()
$this->execute('SET @slave_gtid_ignore_duplicates = 0');
}

if ('' === $binFilePos || '' === $binFileName)
if (0 === $binFilePos || '' === $binFileName)
{
$master = $this->mySQLRepository->getMasterStatus();
$binFilePos = $master['Position'];
Expand Down
2 changes: 1 addition & 1 deletion src/MySQLReplication/BinLog/BinLogServerInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static function parsePackage($pack)
}

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

//auth_plugin_data_part_1
Expand Down
29 changes: 18 additions & 11 deletions src/MySQLReplication/BinaryDataReader/BinaryDataReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public function readInt24()
public function readInt64()
{
$data = unpack('V*', $this->read(self::UNSIGNED_INT64_LENGTH));
return bcadd($data[1], ($data[2] << 32));
return bcadd($data[1], $data[2] << 32);
}

/**
Expand Down Expand Up @@ -424,15 +424,22 @@ public function isComplete($size)
*/
public static function pack64bit($value)
{
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
);
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);
}

/**
* @return int
*/
public function getBinaryDataLength()
{
return strlen($this->binaryData);
}

/**
* @return string
*/
public function getBinaryData()
{
return $this->binaryData;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
*/
class BinaryDataReaderBuilder
{
/**
* @var string
*/
private $binaryData = '';

/**
Expand All @@ -18,6 +21,9 @@ public function withBinaryData($binaryData)
$this->binaryData = $binaryData;
}

/**
* @return BinaryDataReader
*/
public function build()
{
return new BinaryDataReader(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
class BinaryDataReaderService
{
/**
* @param $binaryData
* @param string $binaryData
* @return BinaryDataReader
*/
public function makePackageFromBinaryData($binaryData)
Expand Down
21 changes: 12 additions & 9 deletions src/MySQLReplication/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Config
/**
* @var string
*/
private $ip;
private $host;
/**
* @var int
*/
Expand Down Expand Up @@ -74,7 +74,7 @@ class Config
/**
* Config constructor.
* @param string $user
* @param string $ip
* @param string $host
* @param int $port
* @param string $password
* @param string $dbName
Expand All @@ -91,7 +91,7 @@ class Config
*/
public function __construct(
$user,
$ip,
$host,
$port,
$password,
$dbName,
Expand All @@ -107,7 +107,7 @@ public function __construct(
array $databasesOnly
) {
$this->user = $user;
$this->ip = $ip;
$this->host = $host;
$this->port = $port;
$this->password = $password;
$this->dbName = $dbName;
Expand All @@ -132,9 +132,13 @@ public function validate()
{
throw new ConfigException(ConfigException::USER_ERROR_MESSAGE, ConfigException::USER_ERROR_CODE);
}
if (!empty($this->ip) && false === filter_var($this->ip, FILTER_VALIDATE_IP))
if (!empty($this->host))
{
throw new ConfigException(ConfigException::IP_ERROR_MESSAGE, ConfigException::IP_ERROR_CODE);
$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]]))
{
Expand Down Expand Up @@ -191,9 +195,9 @@ public function getUser()
/**
* @return string
*/
public function getIp()
public function getHost()
{
return $this->ip;
return $this->host;
}

/**
Expand Down Expand Up @@ -299,5 +303,4 @@ public function getDatabasesOnly()
{
return $this->databasesOnly;
}

}
Loading

0 comments on commit 0105225

Please sign in to comment.