Skip to content

Commit 3c1e08f

Browse files
committed
Merge pull request mongodb#1971 from divine/refactor_and_fix_db_selection
[fix] default database detection from dsn
2 parents 907002c + 86c05a3 commit 3c1e08f

File tree

4 files changed

+40
-13
lines changed

4 files changed

+40
-13
lines changed

src/Jenssegers/Mongodb/Connection.php

+25-13
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Database\Connection as BaseConnection;
66
use Illuminate\Support\Arr;
7+
use InvalidArgumentException;
78
use MongoDB\Client;
89

910
class Connection extends BaseConnection
@@ -37,8 +38,11 @@ public function __construct(array $config)
3738
// Create the connection
3839
$this->connection = $this->createConnection($dsn, $config, $options);
3940

41+
// Get default database name
42+
$default_db = $this->getDefaultDatabaseName($dsn, $config);
43+
4044
// Select database
41-
$this->db = $this->connection->selectDatabase($this->getDatabaseDsn($dsn, $config['database']));
45+
$this->db = $this->connection->selectDatabase($default_db);
4246

4347
$this->useDefaultPostProcessor();
4448

@@ -114,6 +118,26 @@ public function getDatabaseName()
114118
return $this->getMongoDB()->getDatabaseName();
115119
}
116120

121+
/**
122+
* Get the name of the default database based on db config or try to detect it from dsn
123+
* @param string $dsn
124+
* @param array $config
125+
* @return string
126+
* @throws InvalidArgumentException
127+
*/
128+
protected function getDefaultDatabaseName($dsn, $config)
129+
{
130+
if (empty($config['database'])) {
131+
if (preg_match('/^mongodb:\\/\\/.+\\/([^?&]+)/s', $dsn, $matches)) {
132+
$config['database'] = $matches[1];
133+
} else {
134+
throw new InvalidArgumentException("Database is not properly configured.");
135+
}
136+
}
137+
138+
return $config['database'];
139+
}
140+
117141
/**
118142
* Create a new MongoDB connection.
119143
* @param string $dsn
@@ -191,18 +215,6 @@ protected function getHostDsn(array $config)
191215
return 'mongodb://' . implode(',', $hosts) . ($auth_database ? '/' . $auth_database : '');
192216
}
193217

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-
206218
/**
207219
* Create a DSN string from a configuration.
208220
* @param array $config

tests/ConnectionTest.php

+9
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ public function testDb()
3232
$this->assertInstanceOf(\MongoDB\Client::class, $connection->getMongoClient());
3333
}
3434

35+
public function testDsnDb()
36+
{
37+
$connection = DB::connection('dsn_mongodb_db');
38+
$this->assertInstanceOf(\MongoDB\Database::class, $connection->getMongoDB());
39+
40+
$connection = DB::connection('dsn_mongodb_db');
41+
$this->assertInstanceOf(\MongoDB\Client::class, $connection->getMongoClient());
42+
}
43+
3544
public function testCollection()
3645
{
3746
$collection = DB::connection('mongodb')->getCollection('unittest');

tests/TestCase.php

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ protected function getEnvironmentSetUp($app)
5353
$app['config']->set('database.connections.mongodb', $config['connections']['mongodb']);
5454
$app['config']->set('database.connections.mongodb2', $config['connections']['mongodb']);
5555
$app['config']->set('database.connections.dsn_mongodb', $config['connections']['dsn_mongodb']);
56+
$app['config']->set('database.connections.dsn_mongodb_db', $config['connections']['dsn_mongodb_db']);
5657

5758
$app['config']->set('auth.model', 'User');
5859
$app['config']->set('auth.providers.users.model', 'User');

tests/config/database.php

+5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@
2121
'database' => env('MONGO_DATABASE', 'unittest'),
2222
],
2323

24+
'dsn_mongodb_db' => [
25+
'driver' => 'mongodb',
26+
'dsn' => "mongodb://$mongoHost:$mongoPort/" . env('MONGO_DATABASE', 'unittest'),
27+
],
28+
2429
'mysql' => [
2530
'driver' => 'mysql',
2631
'host' => env('MYSQL_HOST', 'mysql'),

0 commit comments

Comments
 (0)