diff --git a/src/Jenssegers/Mongodb/Connection.php b/src/Jenssegers/Mongodb/Connection.php index bbc5c437c..b0fd98d0b 100644 --- a/src/Jenssegers/Mongodb/Connection.php +++ b/src/Jenssegers/Mongodb/Connection.php @@ -4,7 +4,6 @@ use Illuminate\Database\Connection as BaseConnection; use Illuminate\Support\Arr; -use Illuminate\Support\Str; use MongoDB\Client; class Connection extends BaseConnection @@ -42,7 +41,7 @@ public function __construct(array $config) $this->connection = $this->createConnection($dsn, $config, $options); // Select database - $this->db = $this->connection->selectDatabase($config['database']); + $this->db = $this->connection->selectDatabase($this->getDatabaseDsn($dsn, $config['database'])); $this->useDefaultPostProcessor(); @@ -151,7 +150,7 @@ public function disconnect() } /** - * Determine if the given configuration array has a UNIX socket value. + * Determine if the given configuration array has a dsn string. * * @param array $config * @return bool @@ -162,22 +161,14 @@ protected function hasDsnString(array $config) } /** - * Get the DSN string for a socket configuration. + * Get the DSN string form configuration. * * @param array $config * @return string */ protected function getDsnString(array $config) { - $dsn_string = $config['dsn']; - - if (Str::contains($dsn_string, 'mongodb://')) { - $dsn_string = Str::replaceFirst('mongodb://', '', $dsn_string); - } - - $dsn_string = rawurlencode($dsn_string); - - return "mongodb://{$dsn_string}"; + return $config['dsn']; } /** @@ -200,10 +191,21 @@ protected function getHostDsn(array $config) // Check if we want to authenticate against a specific database. $auth_database = isset($config['options']) && !empty($config['options']['database']) ? $config['options']['database'] : null; - 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. * diff --git a/tests/DsnTest.php b/tests/DsnTest.php new file mode 100644 index 000000000..08fa0a8aa --- /dev/null +++ b/tests/DsnTest.php @@ -0,0 +1,14 @@ +assertInstanceOf(\Illuminate\Database\Eloquent\Collection::class, DsnAddress::all()); + } +} + +class DsnAddress extends Address +{ + protected $connection = 'dsn_mongodb'; +} diff --git a/tests/TestCase.php b/tests/TestCase.php index 13988623b..f4b26be2d 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -51,6 +51,7 @@ protected function getEnvironmentSetUp($app) $app['config']->set('database.default', 'mongodb'); $app['config']->set('database.connections.mysql', $config['connections']['mysql']); $app['config']->set('database.connections.mongodb', $config['connections']['mongodb']); + $app['config']->set('database.connections.dsn_mongodb', $config['connections']['dsn_mongodb']); $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 1986807a3..f24d20d2f 100644 --- a/tests/config/database.php +++ b/tests/config/database.php @@ -11,6 +11,12 @@ 'database' => 'unittest', ], + 'dsn_mongodb' => [ + 'driver' => 'mongodb', + 'dsn' => 'mongodb://mongodb:27017', + 'database' => 'unittest', + ], + 'mysql' => [ 'driver' => 'mysql', 'host' => 'mysql',