/** * @param Config $config * @throws MySQLReplicationException * @throws DBALException * @throws ConfigException * @throws BinLogException */ public function __construct(Config $config) { $config->validate(); $this->connection = DriverManager::getConnection(['user' => $config->getUser(), 'password' => $config->getPassword(), 'host' => $config->getHost(), 'port' => $config->getPort(), 'driver' => 'pdo_mysql', 'charset' => $config->getCharset()]); $this->repository = new MySQLRepository($this->connection); $this->gtiService = new GtidService(); $this->binLogAuth = new BinLogAuth(); $this->socketConnect = new BinLogSocketConnect($config, $this->repository, $this->binLogAuth, $this->gtiService); $this->socketConnect->connectToStream(); $this->jsonBinaryDecoderFactory = new JsonBinaryDecoderFactory(); $this->rowEventService = new RowEventService($config, $this->repository, $this->jsonBinaryDecoderFactory); $this->binaryDataReaderService = new BinaryDataReaderService(); $this->eventDispatcher = new EventDispatcher(); $this->event = new Event($config, $this->socketConnect, $this->binaryDataReaderService, $this->rowEventService, $this->eventDispatcher); }
/** * @throws BinaryDataReaderException * @throws BinLogException * @throws ConfigException * @throws EventException * @throws MySQLReplicationException * @throws JsonBinaryDecoderException */ public function consume() { $binaryDataReader = $this->packageService->makePackageFromBinaryData($this->socketConnect->getPacket(false)); // "ok" value on first byte continue $binaryDataReader->advance(1); // decode all events data $eventInfo = new EventInfo($binaryDataReader->readInt32(), $binaryDataReader->readUInt8(), $binaryDataReader->readInt32(), $binaryDataReader->readInt32(), $binaryDataReader->readInt32(), $binaryDataReader->readUInt16(), $this->socketConnect->getCheckSum()); if (ConstEventType::TABLE_MAP_EVENT === $eventInfo->getType()) { $event = $this->rowEventService->makeRowEvent($binaryDataReader, $eventInfo)->makeTableMapDTO(); if ($event !== null) { $this->eventDispatcher->dispatch(ConstEventsNames::TABLE_MAP, $event); } } else { if ([] !== $this->config->getEventsOnly() && !in_array($eventInfo->getType(), $this->config->getEventsOnly(), true)) { return; } if (in_array($eventInfo->getType(), $this->config->getEventsIgnore(), true)) { return; } if (in_array($eventInfo->getType(), [ConstEventType::UPDATE_ROWS_EVENT_V1, ConstEventType::UPDATE_ROWS_EVENT_V2], true)) { $event = $this->rowEventService->makeRowEvent($binaryDataReader, $eventInfo)->makeUpdateRowsDTO(); if ($event !== null) { $this->eventDispatcher->dispatch(ConstEventsNames::UPDATE, $event); } } elseif (in_array($eventInfo->getType(), [ConstEventType::WRITE_ROWS_EVENT_V1, ConstEventType::WRITE_ROWS_EVENT_V2], true)) { $event = $this->rowEventService->makeRowEvent($binaryDataReader, $eventInfo)->makeWriteRowsDTO(); if ($event !== null) { $this->eventDispatcher->dispatch(ConstEventsNames::WRITE, $event); } } elseif (in_array($eventInfo->getType(), [ConstEventType::DELETE_ROWS_EVENT_V1, ConstEventType::DELETE_ROWS_EVENT_V2], true)) { $event = $this->rowEventService->makeRowEvent($binaryDataReader, $eventInfo)->makeDeleteRowsDTO(); if ($event !== null) { $this->eventDispatcher->dispatch(ConstEventsNames::DELETE, $event); } } elseif (ConstEventType::XID_EVENT === $eventInfo->getType()) { $this->eventDispatcher->dispatch(ConstEventsNames::XID, (new XidEvent($eventInfo, $binaryDataReader))->makeXidDTO()); } elseif (ConstEventType::ROTATE_EVENT === $eventInfo->getType()) { $this->eventDispatcher->dispatch(ConstEventsNames::ROTATE, (new RotateEvent($eventInfo, $binaryDataReader))->makeRotateEventDTO()); } elseif (ConstEventType::GTID_LOG_EVENT === $eventInfo->getType()) { $this->eventDispatcher->dispatch(ConstEventsNames::GTID, (new GtidEvent($eventInfo, $binaryDataReader))->makeGTIDLogDTO()); } elseif (ConstEventType::QUERY_EVENT === $eventInfo->getType()) { $this->eventDispatcher->dispatch(ConstEventsNames::QUERY, (new QueryEvent($eventInfo, $binaryDataReader))->makeQueryDTO()); } elseif (ConstEventType::MARIA_GTID_EVENT === $eventInfo->getType()) { $this->eventDispatcher->dispatch(ConstEventsNames::MARIADB_GTID, (new MariaDbGtidEvent($eventInfo, $binaryDataReader))->makeMariaDbGTIDLogDTO()); } } }