diff --git a/src/Jenssegers/Mongodb/Connection.php b/src/Jenssegers/Mongodb/Connection.php index 15bd3589c..6e8896dfd 100644 --- a/src/Jenssegers/Mongodb/Connection.php +++ b/src/Jenssegers/Mongodb/Connection.php @@ -4,6 +4,7 @@ use Illuminate\Database\Connection as BaseConnection; use Illuminate\Support\Arr; +use InvalidArgumentException; use MongoDB\Client; class Connection extends BaseConnection @@ -37,8 +38,11 @@ public function __construct(array $config) // Create the connection $this->connection = $this->createConnection($dsn, $config, $options); + // Get default database name + $default_db = $this->getDefaultDatabaseName($dsn, $config); + // Select database - $this->db = $this->connection->selectDatabase($this->getDatabaseDsn($dsn, $config['database'])); + $this->db = $this->connection->selectDatabase($default_db); $this->useDefaultPostProcessor(); @@ -114,6 +118,26 @@ public function getDatabaseName() return $this->getMongoDB()->getDatabaseName(); } + /** + * Get the name of the default database based on db config or try to detect it from dsn + * @param string $dsn + * @param array $config + * @return string + * @throws InvalidArgumentException + */ + protected function getDefaultDatabaseName($dsn, $config) + { + if (empty($config['database'])) { + if (preg_match('/^mongodb:\\/\\/.+\\/([^?&]+)/s', $dsn, $matches)) { + $config['database'] = $matches[1]; + } else { + throw new InvalidArgumentException("Database is not properly configured."); + } + } + + return $config['database']; + } + /** * Create a new MongoDB connection. * @param string $dsn @@ -191,18 +215,6 @@ protected function getHostDsn(array $config) return 'mongodb://' . implode(',', $hosts) . ($auth_database ? '/' . $auth_database : ''); } - /** - * Get database name from DSN string, if there is no database in DSN path - returns back $database argument. - * @param string $dsn - * @param $database - * @return string - */ - protected function getDatabaseDsn($dsn, $database) - { - $dsnDatabase = trim(parse_url($dsn, PHP_URL_PATH), '/'); - return trim($dsnDatabase) ? $dsnDatabase : $database; - } - /** * Create a DSN string from a configuration. * @param array $config diff --git a/tests/ConnectionTest.php b/tests/ConnectionTest.php index b636ef2fd..9a77b5d81 100644 --- a/tests/ConnectionTest.php +++ b/tests/ConnectionTest.php @@ -32,6 +32,15 @@ public function testDb() $this->assertInstanceOf(\MongoDB\Client::class, $connection->getMongoClient()); } + public function testDsnDb() + { + $connection = DB::connection('dsn_mongodb_db'); + $this->assertInstanceOf(\MongoDB\Database::class, $connection->getMongoDB()); + + $connection = DB::connection('dsn_mongodb_db'); + $this->assertInstanceOf(\MongoDB\Client::class, $connection->getMongoClient()); + } + public function testCollection() { $collection = DB::connection('mongodb')->getCollection('unittest'); diff --git a/tests/TestCase.php b/tests/TestCase.php index a455b8576..4c01d5755 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -53,6 +53,7 @@ protected function getEnvironmentSetUp($app) $app['config']->set('database.connections.mongodb', $config['connections']['mongodb']); $app['config']->set('database.connections.mongodb2', $config['connections']['mongodb']); $app['config']->set('database.connections.dsn_mongodb', $config['connections']['dsn_mongodb']); + $app['config']->set('database.connections.dsn_mongodb_db', $config['connections']['dsn_mongodb_db']); $app['config']->set('auth.model', 'User'); $app['config']->set('auth.providers.users.model', 'User'); diff --git a/tests/config/database.php b/tests/config/database.php index 906f8cd2a..556b71d33 100644 --- a/tests/config/database.php +++ b/tests/config/database.php @@ -21,6 +21,11 @@ 'database' => env('MONGO_DATABASE', 'unittest'), ], + 'dsn_mongodb_db' => [ + 'driver' => 'mongodb', + 'dsn' => "mongodb://$mongoHost:$mongoPort/" . env('MONGO_DATABASE', 'unittest'), + ], + 'mysql' => [ 'driver' => 'mysql', 'host' => env('MYSQL_HOST', 'mysql'),