Skip to content

Gets database name from DSN string #1491

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions src/Jenssegers/Mongodb/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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
Expand All @@ -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'];
}

/**
Expand All @@ -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.
*
Expand Down
14 changes: 14 additions & 0 deletions tests/DsnTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

class DsnTest extends TestCase
{
public function test_dsn_works()
{
$this->assertInstanceOf(\Illuminate\Database\Eloquent\Collection::class, DsnAddress::all());
}
}

class DsnAddress extends Address
{
protected $connection = 'dsn_mongodb';
}
1 change: 1 addition & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
6 changes: 6 additions & 0 deletions tests/config/database.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
'database' => 'unittest',
],

'dsn_mongodb' => [
'driver' => 'mongodb',
'dsn' => 'mongodb://mongodb:27017',
'database' => 'unittest',
],

'mysql' => [
'driver' => 'mysql',
'host' => 'mysql',
Expand Down