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
|
|
|
* @var RowEventService |
63
|
|
|
*/ |
64
|
|
|
private $rowEventService; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param Config $config |
68
|
|
|
* @throws MySQLReplicationException |
69
|
|
|
* @throws DBALException |
70
|
|
|
* @throws ConfigException |
71
|
|
|
* @throws BinLogException |
72
|
|
|
*/ |
73
|
|
|
public function __construct(Config $config) |
74
|
|
|
{ |
75
|
|
|
$config->validate(); |
76
|
|
|
|
77
|
|
|
$this->connection = DriverManager::getConnection([ |
78
|
|
|
'user' => $config->getUser(), |
79
|
|
|
'password' => $config->getPassword(), |
80
|
|
|
'host' => $config->getHost(), |
81
|
|
|
'port' => $config->getPort(), |
82
|
|
|
'driver' => 'pdo_mysql', |
83
|
|
|
'charset' => $config->getCharset() |
84
|
|
|
]); |
85
|
|
|
$this->binLogAuth = new BinLogAuth(); |
86
|
|
|
$this->MySQLRepository = new MySQLRepository($this->connection); |
87
|
|
|
$this->GtiService = new GtidService(); |
88
|
|
|
|
89
|
|
|
$this->binLogConnect = new BinLogConnect($config, $this->MySQLRepository, $this->binLogAuth, $this->GtiService); |
90
|
|
|
$this->binLogConnect->connectToStream(); |
91
|
|
|
|
92
|
|
|
$this->jsonBinaryDecoderFactory = new JsonBinaryDecoderFactory(); |
|
|
|
|
93
|
|
|
$this->binaryDataReaderService = new BinaryDataReaderService(); |
94
|
|
|
$this->rowEventService = new RowEventService($config, $this->MySQLRepository, $this->jsonBinaryDecoderFactory); |
95
|
|
|
$this->eventDispatcher = new EventDispatcher(); |
96
|
|
|
|
97
|
|
|
$this->event = new Event( |
98
|
|
|
$config, |
99
|
|
|
$this->binLogConnect, |
100
|
|
|
$this->binaryDataReaderService, |
101
|
|
|
$this->rowEventService, |
102
|
|
|
$this->eventDispatcher |
103
|
|
|
); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param EventSubscribers $eventSubscribers |
108
|
|
|
*/ |
109
|
|
|
public function registerSubscriber(EventSubscribers $eventSubscribers) |
110
|
|
|
{ |
111
|
|
|
$this->eventDispatcher->addSubscriber($eventSubscribers); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return Connection |
116
|
|
|
*/ |
117
|
|
|
public function getDbConnection() |
118
|
|
|
{ |
119
|
|
|
return $this->connection; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @throws MySQLReplicationException |
124
|
|
|
*/ |
125
|
|
|
public function binLogEvent() |
126
|
|
|
{ |
127
|
|
|
$this->event->consume(); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
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: