Skip to content

Commit

Permalink
Add remaining config options as GET parameters of the DSN
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurdarcet committed Nov 13, 2014
1 parent 42510c7 commit 8a20b57
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ Finally, add a MongoDB hash at the end of your `app/config/database.php` (so, ou

If needed (for MongoHq and the likes), you can specify a `username` and `password` in this array as well.

Any other options will be added as GET parameters to the DSN.

### Authentification

To use Mongovel as your Auth provided, you'll simply need to go in the `app/config/auth.php` file and set `mongo` as your driver.
Expand Down
18 changes: 16 additions & 2 deletions src/Mongovel/DB.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,24 @@ public function setDatabaseDSN($server = null, $database = null)
$this->database = $dsn['database'];

if (isset($dsn['username']) && isset($dsn['password'])) {
$this->server = sprintf('mongodb://%s:%s@%s:%d/%s', $dsn['username'], $dsn['password'], $dsn['host'], $dsn['port'], $dsn['database']);
$this->server = sprintf('mongodb://%s:%s@%s:%d/%s/', $dsn['username'], $dsn['password'], $dsn['host'], $dsn['port'], $dsn['database']);
}
else {
$this->server = sprintf('mongodb://%s:%d', $dsn['host'], $dsn['port']);
$this->server = sprintf('mongodb://%s:%d/', $dsn['host'], $dsn['port']);
}

$options = array_diff_key($dsn, array(
'host' => true,
'port' => true,
'database' => true,
'username' => true,
'password' => true,
));
$options = array_map(function($key, $value) {
return $key . '=' . $value;
}, array_keys($options), array_values($options));
if ($options) {
$this->server .= '?' . implode('&', $options);
}
}
}
Expand Down

0 comments on commit 8a20b57

Please sign in to comment.