Skip to content

Commit e41f69a

Browse files
author
Giacomo Fabbian
committed
Merge branch 'master' into pr_1145
2 parents af8c275 + 89fe91f commit e41f69a

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@
2323
"illuminate/container": "^5.8|^6.0",
2424
"illuminate/database": "^5.8|^6.0",
2525
"illuminate/events": "^5.8|^6.0",
26-
"mongodb/mongodb": "^1.4",
27-
"cedx/coveralls": "^11.2"
26+
"mongodb/mongodb": "^1.4"
2827
},
2928
"require-dev": {
3029
"phpunit/phpunit": "^6.0|^7.0|^8.0",
3130
"orchestra/testbench": "^3.1|^4.0",
3231
"mockery/mockery": "^1.0",
3332
"doctrine/dbal": "^2.5",
34-
"phpunit/phpcov": "^6.0"
33+
"phpunit/phpcov": "^6.0",
34+
"cedx/coveralls": "^11.2"
3535
},
3636
"autoload": {
3737
"psr-0": {

src/Jenssegers/Mongodb/Connection.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function __construct(array $config)
3838
$this->connection = $this->createConnection($dsn, $config, $options);
3939

4040
// Select database
41-
$this->db = $this->connection->selectDatabase($config['database']);
41+
$this->db = $this->connection->selectDatabase($this->getDatabaseDsn($dsn, $config['database']));
4242

4343
$this->useDefaultPostProcessor();
4444

@@ -188,10 +188,21 @@ protected function getHostDsn(array $config)
188188

189189
// Check if we want to authenticate against a specific database.
190190
$auth_database = isset($config['options']) && !empty($config['options']['database']) ? $config['options']['database'] : null;
191-
192191
return 'mongodb://' . implode(',', $hosts) . ($auth_database ? '/' . $auth_database : '');
193192
}
194193

194+
/**
195+
* Get database name from DSN string, if there is no database in DSN path - returns back $database argument.
196+
* @param string $dsn
197+
* @param $database
198+
* @return string
199+
*/
200+
protected function getDatabaseDsn($dsn, $database)
201+
{
202+
$dsnDatabase = trim(parse_url($dsn, PHP_URL_PATH), '/');
203+
return trim($dsnDatabase) ? $dsnDatabase : $database;
204+
}
205+
195206
/**
196207
* Create a DSN string from a configuration.
197208
* @param array $config

0 commit comments

Comments
 (0)