From 8a20b57db81245457ca995230164740f2dbd4ba8 Mon Sep 17 00:00:00 2001 From: Arthur Darcet Date: Thu, 13 Nov 2014 14:41:32 +0100 Subject: [PATCH] =?UTF-8?q?Add=20remaining=20config=20options=20as=20GET?= =?UTF-8?q?=C2=A0parameters=20of=20the=20DSN?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 ++ src/Mongovel/DB.php | 18 ++++++++++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 041a0d0..0d15ab7 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/src/Mongovel/DB.php b/src/Mongovel/DB.php index 7722c20..057eb0f 100644 --- a/src/Mongovel/DB.php +++ b/src/Mongovel/DB.php @@ -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); } } }