Skip to content

Commit 80ac3a9

Browse files
authored
Next release (#34)
- Removed unused (probably?) classes ConfigService, BinaryDataReaderService - Changed Event class broke into smaller methods to be cleaner - Added some unit test - Added BinLogCurrent to keep current binlogFile, binlog position and gtid also added example how to resume script based on this data - Moved to php 5.6 sorry.. the future is now ;)
1 parent a6a24c5 commit 80ac3a9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+959
-371
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ log
44
composer.phar
55
composer.lock
66
.php_cs.cache
7-
/example/profiler.php
7+
/example/profiler.php
8+
.idea/

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ dist: trusty
33
language: php
44

55
php:
6-
- 5.5
76
- 5.6
87
- 7.0
8+
- 7.1
99
env:
1010
- DB=mysql57
1111
- DB=mysql56

CHANGELOG.md

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

33
# Release Notes
44

5+
## v4.0.0 (2018-03-10)
6+
- Removed unused (probably?) classes ConfigService, BinaryDataReaderService
7+
- Changed Event class broke into smaller methods to be cleaner
8+
- Added some unit test
9+
- Added BinLogCurrent to keep current binlogFile, binlog position and gtid also added example how to resume script based on this data
10+
- Moved to php 5.6 sorry.. the future is now ;)
11+
512
## v3.0.1 (2017-08-16)
613
- Fixed in config filter_var validation if 0 given
714
- Changed if bin log and bin log file not given then use master otherwise given data will be send to master

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ 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)
66
[![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)
78

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

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
],
1414
"type": "library",
1515
"require": {
16-
"php": ">=5.5.9",
16+
"php": ">=5.6",
1717
"ext-sockets": "*",
1818
"doctrine/dbal": "^2.5",
1919
"doctrine/collections": "^1.3",
@@ -22,7 +22,7 @@
2222
"psr/simple-cache": "^1.0"
2323
},
2424
"require-dev": {
25-
"phpunit/phpunit": "^4.8|^5.7"
25+
"phpunit/phpunit": "^5.7"
2626
},
2727
"license": "MIT",
2828
"authors": [

example/benchmark.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,7 @@ public function run()
155155
*/
156156
private function consume()
157157
{
158-
while (1) {
159-
$this->binLogStream->consume();
160-
}
158+
$this->binLogStream->run();
161159
}
162160

163161
/**

example/dump_events.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,4 @@ public function allEvents(EventDTO $event)
4747
$binLogStream->registerSubscriber(new MyEventSubscribers());
4848

4949
// start consuming events
50-
while (1) {
51-
$binLogStream->consume();
52-
}
53-
50+
$binLogStream->run();

example/resuming.php

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?php
2+
3+
namespace example;
4+
5+
error_reporting(E_ALL);
6+
date_default_timezone_set('UTC');
7+
include __DIR__ . '/../vendor/autoload.php';
8+
9+
use MySQLReplication\BinLog\BinLogCurrent;
10+
use MySQLReplication\Config\ConfigBuilder;
11+
use MySQLReplication\Event\DTO\EventDTO;
12+
use MySQLReplication\Event\EventSubscribers;
13+
use MySQLReplication\MySQLReplicationFactory;
14+
15+
/**
16+
* Your db configuration @see ConfigBuilder for more options
17+
*/
18+
$binLogStream = new MySQLReplicationFactory(
19+
BinLogBootstrap::startFromPosition(new ConfigBuilder())
20+
->withUser('root')
21+
->withHost('127.0.0.1')
22+
->withPassword('root')
23+
->build()
24+
);
25+
26+
/**
27+
* Class BenchmarkEventSubscribers
28+
* @package example
29+
*/
30+
class MyEventSubscribers extends EventSubscribers
31+
{
32+
/**
33+
* @param EventDTO $event (your own handler more in EventSubscribers class )
34+
*/
35+
public function allEvents(EventDTO $event)
36+
{
37+
// all events got __toString() implementation
38+
echo $event;
39+
40+
// all events got JsonSerializable implementation
41+
//echo json_encode($event, JSON_PRETTY_PRINT);
42+
43+
echo 'Memory usage ' . round(memory_get_usage() / 1048576, 2) . ' MB' . PHP_EOL;
44+
45+
// save event for resuming it later
46+
BinLogBootstrap::save($event->getEventInfo()->getBinLogCurrent());
47+
}
48+
}
49+
50+
/**
51+
* Class SaveBinLogPos
52+
* @package example
53+
*/
54+
class BinLogBootstrap
55+
{
56+
/**
57+
* @var string
58+
*/
59+
private static $fileAndPath;
60+
61+
/**
62+
* @return string
63+
*/
64+
private static function getFileAndPath()
65+
{
66+
if (null === self::$fileAndPath) {
67+
self::$fileAndPath = sys_get_temp_dir() . '/bin-log-replicator-last-position';
68+
}
69+
return self::$fileAndPath;
70+
}
71+
72+
/**
73+
* @param BinLogCurrent $binLogCurrent
74+
*/
75+
public static function save(BinLogCurrent $binLogCurrent)
76+
{
77+
78+
echo 'saving file:' . $binLogCurrent->getBinFileName() . ', position:' . $binLogCurrent->getBinLogPosition() . ' bin log position' . PHP_EOL;
79+
80+
// can be redis/nosql/file - something fast!
81+
// to speed up you can save every xxx time
82+
// you can also use signal handler for ctrl + c exiting script to wait for last event
83+
file_put_contents(self::getFileAndPath(), serialize($binLogCurrent));
84+
}
85+
86+
/**
87+
* @param ConfigBuilder $builder
88+
* @return ConfigBuilder
89+
*/
90+
public static function startFromPosition(ConfigBuilder $builder)
91+
{
92+
if (!is_file(self::getFileAndPath())) {
93+
return $builder;
94+
}
95+
96+
/** @var BinLogCurrent $binLogCurrent */
97+
$binLogCurrent = unserialize(file_get_contents(self::getFileAndPath()));
98+
99+
echo 'starting from file:' . $binLogCurrent->getBinFileName() . ', position:' . $binLogCurrent->getBinLogPosition() . ' bin log position' . PHP_EOL;
100+
101+
return $builder
102+
->withBinLogFileName($binLogCurrent->getBinFileName())
103+
->withBinLogPosition($binLogCurrent->getBinLogPosition());
104+
}
105+
}
106+
107+
// register your events handler here
108+
$binLogStream->registerSubscriber(new MyEventSubscribers());
109+
110+
// start consuming events
111+
$binLogStream->run();
112+

phpunit.xml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@
99
processIsolation="false"
1010
stopOnFailure="false">
1111
<testsuites>
12-
<testsuite name="Application Test Suite">
13-
<directory>./tests/</directory>
12+
<testsuite name="Integration Suite">
13+
<directory>./tests/Integration</directory>
14+
</testsuite>
15+
<testsuite name="Unit Suite">
16+
<directory>./tests/Unit</directory>
1417
</testsuite>
1518
</testsuites>
1619
<filter>
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
3+
namespace MySQLReplication\BinLog;
4+
5+
/**
6+
* Class BinLogCurrent
7+
* @package MySQLReplication\BinLog
8+
*/
9+
class BinLogCurrent implements \JsonSerializable
10+
{
11+
/**
12+
* @var int
13+
*/
14+
private $binLogPosition;
15+
/**
16+
* @var string
17+
*/
18+
private $binFileName;
19+
/**
20+
* @var string
21+
*/
22+
private $gtid;
23+
/**
24+
* @var string
25+
*/
26+
private $mariaDbGtid;
27+
28+
/**
29+
* @return int
30+
*/
31+
public function getBinLogPosition()
32+
{
33+
return $this->binLogPosition;
34+
}
35+
36+
/**
37+
* @param int $binLogPosition
38+
*/
39+
public function setBinLogPosition($binLogPosition)
40+
{
41+
$this->binLogPosition = $binLogPosition;
42+
}
43+
44+
/**
45+
* @return string
46+
*/
47+
public function getBinFileName()
48+
{
49+
return $this->binFileName;
50+
}
51+
52+
/**
53+
* @param string $binFileName
54+
*/
55+
public function setBinFileName($binFileName)
56+
{
57+
$this->binFileName = $binFileName;
58+
}
59+
60+
/**
61+
* @return string
62+
*/
63+
public function getGtid()
64+
{
65+
return $this->gtid;
66+
}
67+
68+
/**
69+
* @param string $gtid
70+
*/
71+
public function setGtid($gtid)
72+
{
73+
$this->gtid = $gtid;
74+
}
75+
76+
/**
77+
* @return string
78+
*/
79+
public function getMariaDbGtid()
80+
{
81+
return $this->mariaDbGtid;
82+
}
83+
84+
/**
85+
* @param string $mariaDbGtid
86+
*/
87+
public function setMariaDbGtid($mariaDbGtid)
88+
{
89+
$this->mariaDbGtid = $mariaDbGtid;
90+
}
91+
92+
/**
93+
* Specify data which should be serialized to JSON
94+
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php
95+
* @return mixed data which can be serialized by <b>json_encode</b>,
96+
* which is a value of any type other than a resource.
97+
* @since 5.4.0
98+
*/
99+
public function jsonSerialize()
100+
{
101+
return get_object_vars($this);
102+
}
103+
}

0 commit comments

Comments
 (0)