Skip to content

Commit e422134

Browse files
committed
implmented __toString and JsonSerializable
1 parent b199850 commit e422134

17 files changed

+481
-35
lines changed

README.md

Lines changed: 218 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,235 @@ while (1)
5050
$result = $binLogStream->analysisBinLog();
5151
if (!is_null($result))
5252
{
53-
var_dump($result);
54-
echo 'Memory usage ' . round(memory_get_usage() / 1048576, 2) . ' MB' . PHP_EOL;
53+
// all events got __toString() implementation
54+
echo $result;
55+
56+
// all events got JsonSerializable implementation
57+
//echo json_encode($result, JSON_PRETTY_PRINT);
58+
59+
//echo 'Memory usage ' . round(memory_get_usage() / 1048576, 2) . ' MB' . PHP_EOL;
5560
}
5661
}
5762
```
5863

5964
For this SQL sessions:
6065

6166
```sql
62-
CREATE DATABASE test;
63-
use test;
67+
CREATE DATABASE php-mysql-replication;
68+
use php-mysql-replication;
6469
CREATE TABLE test4 (id int NOT NULL AUTO_INCREMENT, data VARCHAR(255), data2 VARCHAR(255), PRIMARY KEY(id));
6570
INSERT INTO test4 (data,data2) VALUES ("Hello", "World");
6671
UPDATE test4 SET data = "World", data2="Hello" WHERE id = 1;
6772
DELETE FROM test4 WHERE id = 1;
6873
```
6974

70-
75+
Output will be similar to this:
7176

77+
=== MySQLReplication\DTO\GTIDLogDTO ===
78+
Date: 2016-02-20T18:33:17+00:00
79+
Log position: 46343
80+
Event size: 48
81+
Read bytes: 25
82+
Commit: true
83+
GTID NEXT: 9b1c8d18-2a76-11e5-a26b-000c2976f3f3:12984
84+
85+
=== MySQLReplication\DTO\QueryDTO ===
86+
Date: 2016-02-20T18:33:17+00:00
87+
Log position: 46488
88+
Event size: 145
89+
Read bytes: 122
90+
Database: php_mysql_replication
91+
Execution time: 0
92+
Query: CREATE DATABASE php_mysql_replication
93+
94+
=== MySQLReplication\DTO\GTIDLogDTO ===
95+
Date: 2016-02-20T18:33:17+00:00
96+
Log position: 46536
97+
Event size: 48
98+
Read bytes: 25
99+
Commit: true
100+
GTID NEXT: 9b1c8d18-2a76-11e5-a26b-000c2976f3f3:12985
101+
102+
=== MySQLReplication\DTO\QueryDTO ===
103+
Date: 2016-02-20T18:33:17+00:00
104+
Log position: 46751
105+
Event size: 215
106+
Read bytes: 192
107+
Database: php_mysql_replication
108+
Execution time: 0
109+
Query: CREATE TABLE test4 (id int NOT NULL AUTO_INCREMENT, data VARCHAR(255), data2 VARCHAR(255), PRIMARY KEY(id))
110+
111+
=== MySQLReplication\DTO\GTIDLogDTO ===
112+
Date: 2016-02-20T18:33:17+00:00
113+
Log position: 46799
114+
Event size: 48
115+
Read bytes: 25
116+
Commit: true
117+
GTID NEXT: 9b1c8d18-2a76-11e5-a26b-000c2976f3f3:12986
118+
119+
=== MySQLReplication\DTO\QueryDTO ===
120+
Date: 2016-02-20T18:33:17+00:00
121+
Log position: 46888
122+
Event size: 89
123+
Read bytes: 66
124+
Database: php_mysql_replication
125+
Execution time: 0
126+
Query: BEGIN
127+
128+
=== MySQLReplication\DTO\TableMapDTO ===
129+
Date: 2016-02-20T18:33:17+00:00
130+
Log position: 46959
131+
Event size: 71
132+
Read bytes: 48
133+
Table: test4
134+
Database: php_mysql_replication
135+
Table Id: 7794
136+
Columns: 3
137+
138+
=== MySQLReplication\DTO\WriteRowsDTO ===
139+
Date: 2016-02-20T18:33:17+00:00
140+
Log position: 47011
141+
Event size: 52
142+
Read bytes: 29
143+
Table: test4
144+
Affected columns: 3
145+
Changed rows: 1
146+
Values: Array
147+
(
148+
[0] => Array
149+
(
150+
[id] => 1
151+
[data] => Hello
152+
[data2] => World
153+
)
154+
155+
)
156+
157+
158+
=== MySQLReplication\DTO\XidDTO ===
159+
Date: 2016-02-20T18:33:17+00:00
160+
Log position: 47042
161+
Event size: 31
162+
Read bytes: 8
163+
Transaction ID: 10153
164+
165+
=== MySQLReplication\DTO\GTIDLogDTO ===
166+
Date: 2016-02-20T18:33:17+00:00
167+
Log position: 47090
168+
Event size: 48
169+
Read bytes: 25
170+
Commit: true
171+
GTID NEXT: 9b1c8d18-2a76-11e5-a26b-000c2976f3f3:12987
172+
173+
=== MySQLReplication\DTO\QueryDTO ===
174+
Date: 2016-02-20T18:33:17+00:00
175+
Log position: 47179
176+
Event size: 89
177+
Read bytes: 66
178+
Database: php_mysql_replication
179+
Execution time: 0
180+
Query: BEGIN
181+
182+
=== MySQLReplication\DTO\TableMapDTO ===
183+
Date: 2016-02-20T18:33:17+00:00
184+
Log position: 47250
185+
Event size: 71
186+
Read bytes: 48
187+
Table: test4
188+
Database: php_mysql_replication
189+
Table Id: 7794
190+
Columns: 3
191+
192+
=== MySQLReplication\DTO\UpdateRowsDTO ===
193+
Date: 2016-02-20T18:33:17+00:00
194+
Log position: 47320
195+
Event size: 70
196+
Read bytes: 47
197+
Table: test4
198+
Affected columns: 3
199+
Changed rows: 1
200+
Values: Array
201+
(
202+
[0] => Array
203+
(
204+
[before] => Array
205+
(
206+
[id] => 1
207+
[data] => Hello
208+
[data2] => World
209+
)
210+
211+
[after] => Array
212+
(
213+
[id] => 1
214+
[data] => World
215+
[data2] => Hello
216+
)
217+
218+
)
219+
220+
)
221+
222+
223+
=== MySQLReplication\DTO\XidDTO ===
224+
Date: 2016-02-20T18:33:17+00:00
225+
Log position: 47351
226+
Event size: 31
227+
Read bytes: 8
228+
Transaction ID: 10156
229+
230+
=== MySQLReplication\DTO\GTIDLogDTO ===
231+
Date: 2016-02-20T18:33:17+00:00
232+
Log position: 47399
233+
Event size: 48
234+
Read bytes: 25
235+
Commit: true
236+
GTID NEXT: 9b1c8d18-2a76-11e5-a26b-000c2976f3f3:12988
237+
238+
=== MySQLReplication\DTO\QueryDTO ===
239+
Date: 2016-02-20T18:33:17+00:00
240+
Log position: 47488
241+
Event size: 89
242+
Read bytes: 66
243+
Database: php_mysql_replication
244+
Execution time: 0
245+
Query: BEGIN
246+
247+
=== MySQLReplication\DTO\TableMapDTO ===
248+
Date: 2016-02-20T18:33:17+00:00
249+
Log position: 47559
250+
Event size: 71
251+
Read bytes: 48
252+
Table: test4
253+
Database: php_mysql_replication
254+
Table Id: 7794
255+
Columns: 3
256+
257+
=== MySQLReplication\DTO\DeleteRowsDTO ===
258+
Date: 2016-02-20T18:33:17+00:00
259+
Log position: 47611
260+
Event size: 52
261+
Read bytes: 29
262+
Table: test4
263+
Affected columns: 3
264+
Changed rows: 1
265+
Values: Array
266+
(
267+
[0] => Array
268+
(
269+
[id] => 1
270+
[data] => World
271+
[data2] => Hello
272+
)
273+
274+
)
275+
276+
277+
=== MySQLReplication\DTO\XidDTO ===
278+
Date: 2016-02-20T18:33:17+00:00
279+
Log position: 47642
280+
Event size: 31
281+
Read bytes: 8
282+
Transaction ID: 10160
283+
284+

example/dump_events.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@
1616
$result = $binLogStream->analysisBinLog();
1717
if (!is_null($result))
1818
{
19-
var_dump($result);
20-
echo 'Memory usage ' . round(memory_get_usage() / 1048576, 2) . ' MB' . PHP_EOL;
19+
// all events got __toString() implementation
20+
echo $result;
21+
22+
// all events got JsonSerializable implementation
23+
//echo json_encode($result, JSON_PRETTY_PRINT);
24+
25+
//echo 'Memory usage ' . round(memory_get_usage() / 1048576, 2) . ' MB' . PHP_EOL;
2126
}
2227
}

src/MySQLReplication/DTO/DeleteRowsDTO.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
namespace MySQLReplication\DTO;
44

5-
5+
/**
6+
* Class DeleteRowsDTO
7+
* @package MySQLReplication\DTO
8+
*/
69
class DeleteRowsDTO extends RowsDTO
710
{
811
}

src/MySQLReplication/DTO/EventDTO.php

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,24 @@
66
* Class EventDTO
77
* @package MySQLReplication\DTO
88
*/
9-
class EventDTO
9+
class EventDTO implements \JsonSerializable
1010
{
1111
/**
1212
* @var int
1313
*/
14-
private $date;
14+
protected $date;
1515
/**
1616
* @var int
1717
*/
18-
private $binLogPos;
18+
protected $binLogPos;
1919
/**
2020
* @var int
2121
*/
22-
private $eventSize;
22+
protected $eventSize;
2323
/**
2424
* @var int
2525
*/
26-
private $readBytes;
26+
protected $readBytes;
2727

2828
/**
2929
* EventDTO constructor.
@@ -75,4 +75,29 @@ public function getReadBytes()
7575
{
7676
return $this->readBytes;
7777
}
78+
79+
/**
80+
* @return string
81+
*/
82+
public function __toString()
83+
{
84+
return PHP_EOL .
85+
'=== ' . get_class($this) . ' === ' . PHP_EOL .
86+
'Date: ' . $this->date . PHP_EOL .
87+
'Log position: ' . $this->binLogPos . PHP_EOL .
88+
'Event size: ' . $this->eventSize . PHP_EOL .
89+
'Read bytes: ' . $this->readBytes . PHP_EOL;
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+
}
78103
}

src/MySQLReplication/DTO/GTIDLogDTO.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Class GTIDLogEventDTO
77
* @package MySQLReplication\DTO
88
*/
9-
class GTIDLogDTO extends EventDTO
9+
class GTIDLogDTO extends EventDTO implements \JsonSerializable
1010
{
1111
/**
1212
* @var bool
@@ -55,4 +55,31 @@ public function getGtid()
5555
{
5656
return $this->gtid;
5757
}
58+
59+
/**
60+
* @return string
61+
*/
62+
public function __toString()
63+
{
64+
return PHP_EOL .
65+
'=== ' . __CLASS__ . ' === ' . PHP_EOL .
66+
'Date: ' . $this->date . PHP_EOL .
67+
'Log position: ' . $this->binLogPos . PHP_EOL .
68+
'Event size: ' . $this->eventSize . PHP_EOL .
69+
'Read bytes: ' . $this->readBytes . PHP_EOL .
70+
'Commit: ' . var_export($this->commit, true) . PHP_EOL .
71+
'GTID NEXT: ' . $this->gtid . PHP_EOL;
72+
}
73+
74+
/**
75+
* Specify data which should be serialized to JSON
76+
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php
77+
* @return mixed data which can be serialized by <b>json_encode</b>,
78+
* which is a value of any type other than a resource.
79+
* @since 5.4.0
80+
*/
81+
public function jsonSerialize()
82+
{
83+
return get_object_vars($this);
84+
}
5885
}

0 commit comments

Comments
 (0)