Skip to content

Commit 2bcd956

Browse files
committed
Change static properties to non-static.
1 parent 4107322 commit 2bcd956

File tree

16 files changed

+333
-231
lines changed

16 files changed

+333
-231
lines changed

README.md

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,35 @@
11
php-mysql-replication
22
=========
3-
[![Build Status](https://travis-ci.org/krowinski/php-mysql-replication.svg?branch=master)](https://travis-ci.org/krowinski/php-mysql-replication)
4-
[![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)
5-
[![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-
[![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)
7-
[![Code Coverage](https://scrutinizer-ci.com/g/krowinski/php-mysql-replication/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/krowinski/php-mysql-replication/?branch=master)
3+
[![Build Status](https://travis-ci.org/moln/php-mysql-replication.svg?branch=master)](https://travis-ci.org/moln/php-mysql-replication)
4+
[![Latest Stable Version](https://poser.pugx.org/moln/php-mysql-replication/v/stable)](https://packagist.org/packages/moln/php-mysql-replication) [![Total Downloads](https://poser.pugx.org/moln/php-mysql-replication/downloads)](https://packagist.org/packages/moln/php-mysql-replication) [![Latest Unstable Version](https://poser.pugx.org/moln/php-mysql-replication/v/unstable)](https://packagist.org/packages/moln/php-mysql-replication)
5+
[![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/moln/php-mysql-replication/license)](https://packagist.org/packages/moln/php-mysql-replication)
6+
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/moln/php-mysql-replication/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/moln/php-mysql-replication/?branch=master)
7+
[![Code Coverage](https://scrutinizer-ci.com/g/moln/php-mysql-replication/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/moln/php-mysql-replication/?branch=master)
88

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

1111
Based on a great work of creators:https://github.com/noplay/python-mysql-replication and https://github.com/fengxiangyun/mysql-replication
1212

13+
--------------------------------------------------
14+
15+
**Note:** Resolve [krowinski/php-mysql-replication#94](https://github.com/krowinski/php-mysql-replication/issues/94), change static config properties to non-static.
16+
17+
--------------------------------------------------
18+
19+
1320
Installation
1421
=========
1522

1623
In you project
1724

1825
```sh
19-
composer require krowinski/php-mysql-replication
26+
composer require moln/php-mysql-replication
2027
```
2128

2229
or standalone
2330

2431
```sh
25-
git clone https://github.com/krowinski/php-mysql-replication.git
32+
git clone https://github.com/moln/php-mysql-replication.git
2633

2734
composer install -o
2835
```
@@ -89,9 +96,9 @@ Available options:
8996

9097
'binLogPosition' - bin log position to start from
9198

92-
'eventsOnly' - array to listen on events (full list in [ConstEventType.php](https://github.com/krowinski/php-mysql-replication/blob/master/src/MySQLReplication/Definitions/ConstEventType.php) file)
99+
'eventsOnly' - array to listen on events (full list in [ConstEventType.php](https://github.com/moln/php-mysql-replication/blob/master/src/MySQLReplication/Definitions/ConstEventType.php) file)
93100

94-
'eventsIgnore' - array to ignore events (full list in [ConstEventType.php](https://github.com/krowinski/php-mysql-replication/blob/master/src/MySQLReplication/Definitions/ConstEventType.php) file)
101+
'eventsIgnore' - array to ignore events (full list in [ConstEventType.php](https://github.com/moln/php-mysql-replication/blob/master/src/MySQLReplication/Definitions/ConstEventType.php) file)
95102

96103
'tablesOnly' - array to only listen on given tables (default all tables)
97104

@@ -118,7 +125,7 @@ Python: https://github.com/noplay/python-mysql-replication
118125
Examples
119126
=========
120127

121-
All examples are available in the [examples directory](https://github.com/krowinski/php-mysql-replication/tree/master/example)
128+
All examples are available in the [examples directory](https://github.com/moln/php-mysql-replication/tree/master/example)
122129

123130
This example will dump all replication events to the console:
124131

@@ -397,14 +404,14 @@ FAQ
397404
Well first of all mysql don't give you async calls. You usually need to program this in your application (by event dispaching and adding to some queue system and if your db have many point of entry like web, backend other microservices its not always cheap to add processing to all of them. But using mysql replication protocol you can lisen on write events and process then asynchronously (best combo it's to add item to some queue system like rabbitmq, redis or kafka). Also in invalidate cache, search engine replication, real time analytics and audits.
398405

399406
2. ### It's awsome ! but what is the catch ?
400-
Well first of all you need to know that a lot of events may come through, like if you update 1 000 000 records in table "bar" and you need this one insert from your table "foo" Then all must be processed by script and you need to wait for your data. This is normal and this how it's work. You can speed up using [config options](https://github.com/krowinski/php-mysql-replication#configuration).
407+
Well first of all you need to know that a lot of events may come through, like if you update 1 000 000 records in table "bar" and you need this one insert from your table "foo" Then all must be processed by script and you need to wait for your data. This is normal and this how it's work. You can speed up using [config options](https://github.com/moln/php-mysql-replication#configuration).
401408
Also if script crashes you need to save from time to time position form binlog (or gtid) to start from this position when you run this script again to avoid duplicates.
402409

403410
3. ### I need to process 1 000 000 records and its taking forever!!
404411
Like I mention in 1 point use queue system like rabbitmq, redis or kafka, they will give you ability to process data in multiple scripts.
405412

406413
4. ### I have a problem ? you script is missing something ! I have found a bug !
407-
Create an [issue](https://github.com/krowinski/php-mysql-replication/issues) I will try to work on it in my free time :)
414+
Create an [issue](https://github.com/moln/php-mysql-replication/issues) I will try to work on it in my free time :)
408415

409416
5. ### How much its give overhead to mysql server ?
410417
It work like any other mysql in slave mode and its giving same overhead.

src/MySQLReplication/BinLog/BinLogServerInfo.php

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,35 +8,38 @@ class BinLogServerInfo
88
private const MYSQL_VERSION_MARIADB = 'MariaDB';
99
private const MYSQL_VERSION_PERCONA = 'Percona';
1010
private const MYSQL_VERSION_GENERIC = 'MySQL';
11-
private static $serverInfo = [];
11+
/**
12+
* @var array
13+
*/
14+
private $serverInfo;
1215

13-
public static function parsePackage(string $data, string $version): void
16+
public static function parsePackage(string $data, string $version): BinLogServerInfo
1417
{
1518
$i = 0;
1619
$length = strlen($data);
17-
self::$serverInfo['protocol_version'] = ord($data[$i]);
20+
$serverInfo['protocol_version'] = ord($data[$i]);
1821
++$i;
1922

2023
//version
21-
self::$serverInfo['server_version'] = '';
24+
$serverInfo['server_version'] = '';
2225
$start = $i;
2326
for ($i = $start; $i < $length; ++$i) {
2427
if ($data[$i] === chr(0)) {
2528
++$i;
2629
break;
2730
}
28-
self::$serverInfo['server_version'] .= $data[$i];
31+
$serverInfo['server_version'] .= $data[$i];
2932
}
3033

3134
//connection_id 4 bytes
32-
self::$serverInfo['connection_id'] = unpack('I', $data[$i] . $data[++$i] . $data[++$i] . $data[++$i])[1];
35+
$serverInfo['connection_id'] = unpack('I', $data[$i] . $data[++$i] . $data[++$i] . $data[++$i])[1];
3336
++$i;
3437

3538
//auth_plugin_data_part_1
3639
//[len=8] first 8 bytes of the auth-plugin data
37-
self::$serverInfo['salt'] = '';
40+
$serverInfo['salt'] = '';
3841
for ($j = $i; $j < $i + 8; ++$j) {
39-
self::$serverInfo['salt'] .= $data[$j];
42+
$serverInfo['salt'] .= $data[$j];
4043
}
4144
$i += 8;
4245

@@ -47,7 +50,7 @@ public static function parsePackage(string $data, string $version): void
4750
$i += 2;
4851

4952
//character_set (1) -- default server character-set, only the lower 8-bits Protocol::CharacterSet (optional)
50-
self::$serverInfo['character_set'] = $data[$i];
53+
$serverInfo['character_set'] = $data[$i];
5154
++$i;
5255

5356
//status_flags (2) -- Protocol::StatusFlags (optional)
@@ -67,23 +70,30 @@ public static function parsePackage(string $data, string $version): void
6770
//next salt
6871
if ($length >= $i + $salt_len) {
6972
for ($j = $i; $j < $i + $salt_len; ++$j) {
70-
self::$serverInfo['salt'] .= $data[$j];
73+
$serverInfo['salt'] .= $data[$j];
7174
}
7275

7376
}
74-
self::$serverInfo['auth_plugin_name'] = '';
77+
$serverInfo['auth_plugin_name'] = '';
7578
$i += $salt_len + 1;
7679
for ($j = $i; $j < $length - 1; ++$j) {
77-
self::$serverInfo['auth_plugin_name'] .= $data[$j];
80+
$serverInfo['auth_plugin_name'] .= $data[$j];
7881
}
7982

80-
self::$serverInfo['version_name'] = self::parseVersion($version);
81-
self::$serverInfo['version_revision'] = self::parseRevision($version);
83+
$serverInfo['version_name'] = self::parseVersion($version);
84+
$serverInfo['version_revision'] = self::parseRevision($version);
85+
86+
return new self($serverInfo);
87+
}
88+
89+
public function __construct(array $serverInfo)
90+
{
91+
$this->serverInfo = $serverInfo;
8292
}
8393

84-
public static function getSalt(): string
94+
public function getSalt(): string
8595
{
86-
return self::$serverInfo['salt'];
96+
return $this->serverInfo['salt'];
8797
}
8898

8999
/**
@@ -103,29 +113,29 @@ private static function parseVersion(string $version): string
103113
return self::MYSQL_VERSION_GENERIC;
104114
}
105115

106-
public static function getRevision(): float
116+
public function getRevision(): float
107117
{
108-
return self::$serverInfo['version_revision'];
118+
return $this->serverInfo['version_revision'];
109119
}
110120

111-
public static function getVersion(): string
121+
public function getVersion(): string
112122
{
113-
return self::$serverInfo['version_name'];
123+
return $this->serverInfo['version_name'];
114124
}
115125

116-
public static function isMariaDb(): bool
126+
public function isMariaDb(): bool
117127
{
118-
return self::MYSQL_VERSION_MARIADB === self::getVersion();
128+
return self::MYSQL_VERSION_MARIADB === $this->getVersion();
119129
}
120130

121-
public static function isPercona(): bool
131+
public function isPercona(): bool
122132
{
123-
return self::MYSQL_VERSION_PERCONA === self::getVersion();
133+
return self::MYSQL_VERSION_PERCONA === $this->getVersion();
124134
}
125135

126-
public static function isGeneric(): bool
136+
public function isGeneric(): bool
127137
{
128-
return self::MYSQL_VERSION_GENERIC === self::getVersion();
138+
return self::MYSQL_VERSION_GENERIC === $this->getVersion();
129139
}
130140

131141
private static function parseRevision(string $version): float

src/MySQLReplication/BinLog/BinLogSocketConnect.php

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,44 @@ class BinLogSocketConnect
2828
private $socket;
2929
private $binLogCurrent;
3030

31+
/**
32+
* @var Config
33+
*/
34+
private $config;
35+
/**
36+
* @var BinLogServerInfo
37+
*/
38+
private $binLogServerInfo;
39+
3140
/**
3241
* @throws BinLogException
3342
* @throws GtidException
3443
* @throws SocketException
3544
*/
3645
public function __construct(
46+
Config $config,
3747
RepositoryInterface $repository,
3848
SocketInterface $socket
3949
) {
50+
$this->config = $config;
4051
$this->repository = $repository;
4152
$this->socket = $socket;
4253
$this->binLogCurrent = new BinLogCurrent();
4354

44-
$this->socket->connectToStream(Config::getHost(), Config::getPort());
45-
BinLogServerInfo::parsePackage($this->getResponse(false), $this->repository->getVersion());
55+
$this->socket->connectToStream($config->getHost(), $config->getPort());
56+
$this->binLogServerInfo = BinLogServerInfo::parsePackage(
57+
$this->getResponse(false),
58+
$this->repository->getVersion()
59+
);
4660
$this->authenticate();
4761
$this->getBinlogStream();
4862
}
4963

64+
public function getBinLogServerInfo(): BinLogServerInfo
65+
{
66+
return $this->binLogServerInfo;
67+
}
68+
5069
/**
5170
* @throws BinLogException
5271
* @throws SocketException
@@ -111,11 +130,11 @@ private function authenticate(): void
111130
for ($i = 0; $i < 23; ++$i) {
112131
$data .= chr(0);
113132
}
114-
$result = sha1(Config::getPassword(), true) ^ sha1(
115-
BinLogServerInfo::getSalt() . sha1(sha1(Config::getPassword(), true), true), true
133+
$result = sha1($this->config->getPassword(), true) ^ sha1(
134+
$this->binLogServerInfo->getSalt() . sha1(sha1($this->config->getPassword(), true), true), true
116135
);
117136

118-
$data = $data . Config::getUser() . chr(0) . chr(strlen($result)) . $result;
137+
$data = $data . $this->config->getUser() . chr(0) . chr(strlen($result)) . $result;
119138
$str = pack('L', strlen($data));
120139
$s = $str[0] . $str[1] . $str[2];
121140
$data = $s . chr(1) . $data;
@@ -152,17 +171,17 @@ private function getBinlogStream(): void
152171
$this->execute('SET @master_binlog_checksum = @@global.binlog_checksum');
153172
}
154173

155-
if (0 !== Config::getHeartbeatPeriod()) {
174+
if (0 !== $this->config->getHeartbeatPeriod()) {
156175
// master_heartbeat_period is in nanoseconds
157-
$this->execute('SET @master_heartbeat_period = ' . Config::getHeartbeatPeriod() * 1000000000);
176+
$this->execute('SET @master_heartbeat_period = ' . $this->config->getHeartbeatPeriod() * 1000000000);
158177
}
159178

160179
$this->registerSlave();
161180

162-
if ('' !== Config::getMariaDbGtid()) {
181+
if ('' !== $this->config->getMariaDbGtid()) {
163182
$this->setBinLogDumpMariaGtid();
164183
}
165-
if ('' !== Config::getGtid()) {
184+
if ('' !== $this->config->getGtid()) {
166185
$this->setBinLogDumpGtid();
167186
} else {
168187
$this->setBinLogDump();
@@ -188,19 +207,19 @@ private function registerSlave(): void
188207
{
189208
$host = gethostname();
190209
$hostLength = strlen($host);
191-
$userLength = strlen(Config::getUser());
192-
$passLength = strlen(Config::getPassword());
210+
$userLength = strlen($this->config->getUser());
211+
$passLength = strlen($this->config->getPassword());
193212

194213
$data = pack('l', 18 + $hostLength + $userLength + $passLength);
195214
$data .= chr(self::COM_REGISTER_SLAVE);
196-
$data .= pack('V', Config::getSlaveId());
215+
$data .= pack('V', $this->config->getSlaveId());
197216
$data .= pack('C', $hostLength);
198217
$data .= $host;
199218
$data .= pack('C', $userLength);
200-
$data .= Config::getUser();
219+
$data .= $this->config->getUser();
201220
$data .= pack('C', $passLength);
202-
$data .= Config::getPassword();
203-
$data .= pack('v', Config::getPort());
221+
$data .= $this->config->getPassword();
222+
$data .= pack('v', $this->config->getPort());
204223
$data .= pack('V', 0);
205224
$data .= pack('V', 0);
206225

@@ -215,11 +234,11 @@ private function registerSlave(): void
215234
private function setBinLogDumpMariaGtid(): void
216235
{
217236
$this->execute('SET @mariadb_slave_capability = 4');
218-
$this->execute('SET @slave_connect_state = \'' . Config::getMariaDbGtid() . '\'');
237+
$this->execute('SET @slave_connect_state = \'' . $this->config->getMariaDbGtid() . '\'');
219238
$this->execute('SET @slave_gtid_strict_mode = 0');
220239
$this->execute('SET @slave_gtid_ignore_duplicates = 0');
221240

222-
$this->binLogCurrent->setMariaDbGtid(Config::getMariaDbGtid());
241+
$this->binLogCurrent->setMariaDbGtid($this->config->getMariaDbGtid());
223242
}
224243

225244
/**
@@ -230,11 +249,11 @@ private function setBinLogDumpMariaGtid(): void
230249
*/
231250
private function setBinLogDumpGtid(): void
232251
{
233-
$collection = GtidCollection::makeCollectionFromString(Config::getGtid());
252+
$collection = GtidCollection::makeCollectionFromString($this->config->getGtid());
234253

235254
$data = pack('l', 26 + $collection->getEncodedLength()) . chr(self::COM_BINLOG_DUMP_GTID);
236255
$data .= pack('S', 0);
237-
$data .= pack('I', Config::getSlaveId());
256+
$data .= pack('I', $this->config->getSlaveId());
238257
$data .= pack('I', 3);
239258
$data .= chr(0);
240259
$data .= chr(0);
@@ -246,7 +265,7 @@ private function setBinLogDumpGtid(): void
246265
$this->socket->writeToSocket($data);
247266
$this->getResponse();
248267

249-
$this->binLogCurrent->setGtid(Config::getGtid());
268+
$this->binLogCurrent->setGtid($this->config->getGtid());
250269
}
251270

252271
/**
@@ -256,8 +275,8 @@ private function setBinLogDumpGtid(): void
256275
*/
257276
private function setBinLogDump(): void
258277
{
259-
$binFilePos = Config::getBinLogPosition();
260-
$binFileName = Config::getBinLogFileName();
278+
$binFilePos = $this->config->getBinLogPosition();
279+
$binFileName = $this->config->getBinLogFileName();
261280
if (0 === $binFilePos && '' === $binFileName) {
262281
$masterStatusDTO = $this->repository->getMasterStatus();
263282
$binFilePos = $masterStatusDTO->getPosition();
@@ -267,7 +286,7 @@ private function setBinLogDump(): void
267286
$data = pack('i', strlen($binFileName) + 11) . chr(self::COM_BINLOG_DUMP);
268287
$data .= pack('I', $binFilePos);
269288
$data .= pack('v', 0);
270-
$data .= pack('I', Config::getSlaveId());
289+
$data .= pack('I', $this->config->getSlaveId());
271290
$data .= $binFileName;
272291

273292
$this->socket->writeToSocket($data);

0 commit comments

Comments
 (0)