Skip to content

Commit

Permalink
Merge pull request mongodb#18 from divine/pr/17
Browse files Browse the repository at this point in the history
Get database from dsn
  • Loading branch information
mnphpexpert committed Feb 9, 2020
2 parents 8b9dccb + 720c903 commit 525d846
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/Jenssegers/Mongodb/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,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 @@ -188,10 +188,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.
* @param array $config
Expand Down

0 comments on commit 525d846

Please sign in to comment.