protected function retrieveReplSetHosts()
{
if (!$this->replSet) {
throw new MongoConnectionException("Cannot retrieve replication set host list. Connection not configured as a replSet connection.");
}
$protocol = $this->connectToFirstAvailableHost();
$is_master = $this->selectDB('admin')->command(['isMaster' => 1], ['protocol' => $protocol]);
if (!isset($is_master['setName']) || $is_master['setName'] != $this->replSet) {
throw new MongoConnectionException("Could not ascertain replSet information from seed host ({$protocol->getServerHash()}). Are you sure this host is configured as a member of replset: '{$this->replSet}'?");
}
$hosts = [];
$replset_hosts = isset($is_master['hosts']) ? $is_master['hosts'] : array();
$replset_passive_hosts = isset($is_master['passive']) ? $is_master['passive'] : array();
$all_hosts = array_merge($replset_hosts, $replset_passive_hosts);
foreach ($all_hosts as $host_str) {
$host_parts = $this->parseHostString($host_str);
if ($host_str === $is_master['primary']) {
$host_parts['state'] = static::STATE_PRIMARY;
} else {
$host_parts['state'] = static::STATE_STARTUP;
}
$hosts[$host_parts['hash']] = $host_parts;
}
return $hosts;
}