public function connect()
{
if ($this->_connection) {
return;
}
$host = $this->_config['connection']['server'];
if (isset($this->_config['connection']['username']) && isset($this->_config['connection']['password'])) {
$host = $this->_config['connection']['username'] . ':' . $this->_config['connection']['password'] . '@' . $host;
}
if (strpos($host, 'mongodb://') !== 0) {
$host = 'mongodb://' . $host;
}
if (!isset($options)) {
$options = array();
}
$options['connect'] = FALSE;
$this->_connection = new MongoClient($host, $options);
try {
$this->_connection->connect();
} catch (MongoConnectionException $e) {
throw new Exception('Unable to connect to Mongo server at :hostnames', array(':hostnames' => $e->getMessage()));
}
if (!isset($this->_config['connection']['db'])) {
throw new Exception('No database specified in MangoDB Config');
}
$this->_db = $this->_connection->selectDB($this->_config['connection']['db']);
return $this->_connected = TRUE;
}