示例#1
0
 public function getById($id)
 {
     $cluster = new Cluster($id);
     $filename = $this->basepath . $id . '.json';
     if (!file_exists($filename)) {
         throw new RuntimeException("Cluster config jsonfile not found: " . $filename);
     }
     $json = file_get_contents($filename);
     $data = json_decode($json, true);
     $cluster->setComment($data['comment']);
     foreach ($data['servers'] as $serverdata) {
         $serverid = $serverdata['id'];
         $server = new Server($serverid);
         $server->setAddress($serverdata['address']);
         $server->setPort($serverdata['port']);
         $cluster->addServer($server);
     }
     foreach ($data['accounts'] as $accountdata) {
         $accountid = $accountdata['id'];
         $account = new Account($accountid);
         $account->setUsername($accountdata['username']);
         $account->setPassword($accountdata['password']);
         $cluster->addAccount($account);
     }
     foreach ($data['connections'] as $connectiondata) {
         $connectionid = $connectiondata['id'];
         $connection = new Connection($connectionid);
         $server = $cluster->getServerById($connectiondata['server']);
         $connection->setServer($server);
         $account = $cluster->getAccountById($connectiondata['account']);
         $connection->setAccount($account);
         $cluster->addConnection($connection);
     }
     return $cluster;
 }
 protected function getByArray($id, $data)
 {
     $database = new Database($id);
     if (isset($data['cluster'])) {
         $cluster = $this->clusterrepository->getById($data['cluster']);
         $database->setCluster($cluster);
     } else {
         $connection = new Connection('local');
         if (isset($data['username'])) {
             $account = new Account('local');
             $account->setUsername($data['username']);
             if (isset($data['password'])) {
                 $account->setPassword($data['password']);
             }
             $connection->setAccount($account);
         }
         if (isset($data['server'])) {
             $server = new Server('local');
             $server->setAddress($data['server']);
             if (isset($data['port'])) {
                 $server->setPort($data['port']);
             }
             $connection->setServer($server);
         }
         $database->addConnection($connection);
     }
     return $database;
 }