public function __construct($server = 'mongodb://localhost:27017', array $options = ['connect' => true])
{
if (!$server || strpos($server, 'mongodb://') !== 0) {
throw new MongoConnectionException('malformed uri: ' . $server);
}
$this->uri = $server;
$uri = substr($server, 10);
$serverPart = '';
$nsPart = null;
$optionsPart = '';
$idx = strrpos($uri, '/');
if ($idx === false) {
if (strpos($uri, '?') !== false) {
throw new MongoConnectionException('malformed uri: ' . $server);
}
$serverPart = $uri;
$nsPart = null;
$optionsPart = '';
} else {
$serverPart = substr($uri, 0, $idx);
$nsPart = substr($uri, $idx + 1);
$idx = strrpos($nsPart, '?');
if ($idx !== false) {
$optionsPart = substr($nsPart, $idx + 1);
$nsPart = substr($nsPart, 0, $idx);
} else {
$optionsPart = '';
}
}
// username,password,hosts
$idx = strrpos($serverPart, '@');
if ($idx !== false) {
$authPart = substr($serverPart, 0, $idx);
$serverPart = substr($serverPart, $idx + 1);
$idx = strrpos($authPart, ':');
if ($idx === false) {
$this->username = urldecode($authPart);
$this->password = '';
} else {
$this->username = urldecode(substr($authPart, 0, $idx));
$this->password = urldecode(substr($authPart, $idx + 1));
}
}
if (strlen($serverPart) === 0) {
throw new MongoConnectionException('malformed uri: ' . $server);
}
foreach (explode(',', $serverPart) as $host_str) {
$host_parts = $this->parseHostString($host_str);
$this->hosts[$host_parts['hash']] = $host_parts;
}
if ($nsPart != null && strlen($nsPart) != 0) {
// database
$this->database = $nsPart;
}
$uri_options = [];
$split_options = preg_split('/[&;]+/', $optionsPart);
foreach ($split_options as $part) {
$idx = strrpos($part, '=');
if ($idx !== false) {
$key = substr($part, 0, $idx);
$value = substr($part, $idx + 1);
$uri_options[$key] = $value;
}
}
if (!$options) {
$options = ['connect' => true];
}
$this->options = array_replace($uri_options, $options);
// handle legacy settings
if (array_key_exists('timeout', $this->options) && !array_key_exists('connectTimeoutMS', $this->options)) {
$this->options['connectTimeoutMS'] = $this->options['timeout'];
unset($this->options['timeout']);
}
if (array_key_exists('wtimeout', $this->options) && !array_key_exists('wtimeoutms', $this->options)) {
$this->options['wtimeoutms'] = $this->options['wtimeout'];
unset($this->options['wtimeout']);
}
if (isset($this->options['connectTimeoutMS'])) {
$this->connectTimeoutMS = $this->options['connectTimeoutMS'];
} else {
$this->connectTimeoutMS = self::DEFAULT_CONNECT_TIMEOUT_MS;
}
if (array_key_exists('username', $this->options)) {
$this->username = $this->options['username'];
}
if (array_key_exists('password', $this->options)) {
$this->password = $this->options['password'];
}
if (array_key_exists('db', $this->options)) {
$this->database = $this->options['db'];
}
if ($this->database == null && $this->username != null) {
$this->database = self::DEFAULT_DATABASE;
}
if (isset($this->options['replicaSet'])) {
$this->replSet = $this->options['replicaSet'];
}
if (isset($this->options['readPreference'])) {
$tags = isset($this->options['readPreferenceTags']) ? $this->options['readPreferenceTags'] : null;
$this->setReadPreference($this->options['readPreference'], $tags);
}
if (!isset($this->options['connect']) || $this->options['connect'] === true) {
$this->connect();
}
}