Skip to content

Commit

Permalink
DP-487 Add +srv option support for calling mongodb
Browse files Browse the repository at this point in the history
  • Loading branch information
tomonorman committed Mar 30, 2022
1 parent 0aa1901 commit c054463
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
4 changes: 4 additions & 0 deletions src/Components/GridFsSystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public function __construct($config, $name)

if (!empty($dsn = strval(array_get($config, 'dsn')))) {
// add prefix if not there
// NOTE: We may want to change this to match the code in MondoDB.php as a regex check
// if this breaks for calling Atlas / replica sets with the +srv option
if (0 != substr_compare($dsn, static::DSN_PREFIX, 0, static::DSN_PREFIX_LENGTH, true)) {
$dsn = static::DSN_PREFIX . $dsn;
$config['dsn'] = $dsn;
Expand All @@ -64,6 +66,8 @@ public function __construct($config, $name)
$config['database'] = $db;
} else {
// Attempt to find db in connection string
// NOTE: We may want to change this to match the code in MondoDB.php as a regex replacement
// if this breaks for calling Atlas / replica sets with the +srv option
$db = strstr(substr($dsn, static::DSN_PREFIX_LENGTH), '/');
if (false !== $pos = strpos($db, '?')) {
$db = substr($db, 0, $pos);
Expand Down
2 changes: 1 addition & 1 deletion src/Models/MongoDbConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function validate($data, $throwException = true)
) {
// Attempt to find db in connection string
$dsn = strval(array_get($data, 'dsn'));
$db = strstr(substr($dsn, MongoDb::DSN_PREFIX_LENGTH), '/');
$db = strstr(preg_replace('/mongodb(\+srv)?\:\/\//', '', $dsn), '/');
if (false !== $pos = strpos($db, '?')) {
$db = substr($db, 0, $pos);
}
Expand Down
8 changes: 2 additions & 6 deletions src/Services/MongoDb.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ class MongoDb extends BaseDbService
* Connection string prefix
*/
const DSN_PREFIX = 'mongodb://';
/**
* Connection string prefix length
*/
const DSN_PREFIX_LENGTH = 10;

//*************************************************************************
// Members
Expand Down Expand Up @@ -65,7 +61,7 @@ public function __construct($settings = [])
$this->config['driver'] = 'mongodb';
if (!empty($dsn = strval(array_get($this->config, 'dsn')))) {
// add prefix if not there
if (0 != substr_compare($dsn, static::DSN_PREFIX, 0, static::DSN_PREFIX_LENGTH, true)) {
if (!preg_match('/mongodb(\+srv)?\:\/\//', $dsn)) {
$dsn = static::DSN_PREFIX . $dsn;
$this->config['dsn'] = $dsn;
}
Expand All @@ -82,7 +78,7 @@ public function __construct($settings = [])
$this->config['database'] = $db;
} else {
// Attempt to find db in connection string
$db = strstr(substr($dsn, static::DSN_PREFIX_LENGTH), '/');
$db = strstr(preg_replace('/mongodb(\+srv)?\:\/\//', '', $dsn), '/');
if (false !== $pos = strpos($db, '?')) {
$db = substr($db, 0, $pos);
}
Expand Down

0 comments on commit c054463

Please sign in to comment.