|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace MySQLReplication; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Connection; |
|
6
|
|
|
use Doctrine\DBAL\DBALException; |
|
7
|
|
|
use Doctrine\DBAL\DriverManager; |
|
8
|
|
|
use MySQLReplication\BinaryDataReader\BinaryDataReaderService; |
|
9
|
|
|
use MySQLReplication\BinLog\BinLogAuth; |
|
10
|
|
|
use MySQLReplication\BinLog\BinLogConnect; |
|
11
|
|
|
use MySQLReplication\BinLog\Exception\BinLogException; |
|
12
|
|
|
use MySQLReplication\Config\Config; |
|
13
|
|
|
use MySQLReplication\Config\Exception\ConfigException; |
|
14
|
|
|
use MySQLReplication\Event\Event; |
|
15
|
|
|
use MySQLReplication\Event\EventSubscribers; |
|
16
|
|
|
use MySQLReplication\Event\RowEvent\RowEventService; |
|
17
|
|
|
use MySQLReplication\Exception\MySQLReplicationException; |
|
18
|
|
|
use MySQLReplication\Gtid\GtidService; |
|
19
|
|
|
use MySQLReplication\JsonBinaryDecoder\JsonBinaryDecoderFactory; |
|
20
|
|
|
use MySQLReplication\Repository\MySQLRepository; |
|
21
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcher; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Class MySQLReplicationFactory |
|
25
|
|
|
* @package MySQLReplication |
|
26
|
|
|
*/ |
|
27
|
|
|
class MySQLReplicationFactory |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* @var EventDispatcher |
|
31
|
|
|
*/ |
|
32
|
|
|
private $eventDispatcher; |
|
33
|
|
|
/** |
|
34
|
|
|
* @var MySQLRepository |
|
35
|
|
|
*/ |
|
36
|
|
|
private $MySQLRepository; |
|
37
|
|
|
/** |
|
38
|
|
|
* @var BinLogConnect |
|
39
|
|
|
*/ |
|
40
|
|
|
private $binLogConnect; |
|
41
|
|
|
/** |
|
42
|
|
|
* @var Event |
|
43
|
|
|
*/ |
|
44
|
|
|
private $event; |
|
45
|
|
|
/** |
|
46
|
|
|
* @var BinLogAuth |
|
47
|
|
|
*/ |
|
48
|
|
|
private $binLogAuth; |
|
49
|
|
|
/** |
|
50
|
|
|
* @var Connection |
|
51
|
|
|
*/ |
|
52
|
|
|
private $connection; |
|
53
|
|
|
/** |
|
54
|
|
|
* @var BinaryDataReaderService |
|
55
|
|
|
*/ |
|
56
|
|
|
private $binaryDataReaderService; |
|
57
|
|
|
/** |
|
58
|
|
|
* @var GtidService |
|
59
|
|
|
*/ |
|
60
|
|
|
private $GtiService; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param Config $config |
|
64
|
|
|
* @throws MySQLReplicationException |
|
65
|
|
|
* @throws DBALException |
|
66
|
|
|
* @throws ConfigException |
|
67
|
|
|
* @throws BinLogException |
|
68
|
|
|
*/ |
|
69
|
|
|
public function __construct(Config $config) |
|
70
|
|
|
{ |
|
71
|
|
|
$config->validate(); |
|
72
|
|
|
|
|
73
|
|
|
$this->connection = DriverManager::getConnection([ |
|
74
|
|
|
'user' => $config->getUser(), |
|
75
|
|
|
'password' => $config->getPassword(), |
|
76
|
|
|
'host' => $config->getHost(), |
|
77
|
|
|
'port' => $config->getPort(), |
|
78
|
|
|
'driver' => 'pdo_mysql', |
|
79
|
|
|
'charset' => $config->getCharset() |
|
80
|
|
|
]); |
|
81
|
|
|
$this->binLogAuth = new BinLogAuth(); |
|
82
|
|
|
$this->MySQLRepository = new MySQLRepository($this->connection); |
|
83
|
|
|
$this->GtiService = new GtidService(); |
|
84
|
|
|
|
|
85
|
|
|
$this->binLogConnect = new BinLogConnect($config, $this->MySQLRepository, $this->binLogAuth, $this->GtiService); |
|
86
|
|
|
$this->binLogConnect->connectToStream(); |
|
87
|
|
|
|
|
88
|
|
|
$this->jsonBinaryDecoderFactory = new JsonBinaryDecoderFactory(); |
|
|
|
|
|
|
89
|
|
|
$this->binaryDataReaderService = new BinaryDataReaderService(); |
|
90
|
|
|
$this->rowEventService = new RowEventService($config, $this->MySQLRepository, $this->jsonBinaryDecoderFactory); |
|
|
|
|
|
|
91
|
|
|
$this->eventDispatcher = new EventDispatcher(); |
|
92
|
|
|
|
|
93
|
|
|
$this->event = new Event( |
|
94
|
|
|
$config, |
|
95
|
|
|
$this->binLogConnect, |
|
96
|
|
|
$this->binaryDataReaderService, |
|
97
|
|
|
$this->rowEventService, |
|
98
|
|
|
$this->eventDispatcher |
|
99
|
|
|
); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param EventSubscribers $eventSubscribers |
|
104
|
|
|
*/ |
|
105
|
|
|
public function registerSubscriber(EventSubscribers $eventSubscribers) |
|
106
|
|
|
{ |
|
107
|
|
|
$this->eventDispatcher->addSubscriber($eventSubscribers); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @return Connection |
|
112
|
|
|
*/ |
|
113
|
|
|
public function getDbConnection() |
|
114
|
|
|
{ |
|
115
|
|
|
return $this->connection; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @throws MySQLReplicationException |
|
120
|
|
|
*/ |
|
121
|
|
|
public function binLogEvent() |
|
122
|
|
|
{ |
|
123
|
|
|
$this->event->consume(); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: