예제 #1
0
 protected function setUp()
 {
     parent::setUp();
     $config = (new ConfigService())->makeConfigFromArray(['user' => 'root', 'ip' => '127.0.0.1', 'password' => 'root']);
     $this->binLogStream = new MySQLReplicationFactory($config);
     $this->binLogStream->registerSubscriber(new MyEventSubscribers($this));
     $this->conn = $this->binLogStream->getDbConnection();
     $this->conn->exec('SET GLOBAL time_zone = "UTC"');
     $this->conn->exec('DROP DATABASE IF EXISTS ' . $this->database);
     $this->conn->exec('CREATE DATABASE ' . $this->database);
     $this->conn->exec('USE ' . $this->database);
     $this->conn->exec('SET SESSION sql_mode = \'\';');
 }
date_default_timezone_set('UTC');
include __DIR__ . '/../vendor/autoload.php';
use MySQLReplication\Config\ConfigService;
use MySQLReplication\Event\DTO\EventDTO;
use MySQLReplication\Event\EventSubscribers;
use MySQLReplication\MySQLReplicationFactory;
$binLogStream = new MySQLReplicationFactory((new ConfigService())->makeConfigFromArray(['user' => 'root', 'ip' => '127.0.0.1', 'password' => 'root']));
/**
 * Class BenchmarkEventSubscribers
 * @package example
 */
class MyEventSubscribers extends EventSubscribers
{
    /**
     * @param EventDTO $event (your own handler more in EventSubscribers class )
     */
    public function allEvents(EventDTO $event)
    {
        // all events got __toString() implementation
        echo $event;
        // all events got JsonSerializable implementation
        echo json_encode($event, JSON_PRETTY_PRINT);
        echo 'Memory usage ' . round(memory_get_usage() / 1048576, 2) . ' MB' . PHP_EOL;
    }
}
// register your events handler here
$binLogStream->registerSubscriber(new MyEventSubscribers());
// start consuming events
while (1) {
    $binLogStream->binLogEvent();
}