Skip to content

Commit d9375e3

Browse files
committed
Refactor and fix default database detection from dsn or database config
Refactored default database function which detects based on db config or tries to detect it from dsn
1 parent 50e75d6 commit d9375e3

File tree

4 files changed

+41
-13
lines changed

4 files changed

+41
-13
lines changed

src/Jenssegers/Mongodb/Connection.php

+26-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,27 @@ 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+
132+
if (preg_match('/^mongodb:\\/\\/.+\\/([^?&]+)/s', $dsn, $matches)) {
133+
$config['database'] = $matches[1];
134+
} else {
135+
throw new InvalidArgumentException("Database is not properly configured.");
136+
}
137+
}
138+
139+
return $config['database'];
140+
}
141+
117142
/**
118143
* Create a new MongoDB connection.
119144
* @param string $dsn
@@ -191,18 +216,6 @@ protected function getHostDsn(array $config)
191216
return 'mongodb://' . implode(',', $hosts) . ($auth_database ? '/' . $auth_database : '');
192217
}
193218

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-
206219
/**
207220
* Create a DSN string from a configuration.
208221
* @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)