Skip to content

fix: fixes dsn connection strings by url encoding #1397

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 2 commits into from
Jan 8, 2018
Merged
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
51 changes: 45 additions & 6 deletions src/Jenssegers/Mongodb/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

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 @@ -150,18 +151,43 @@ public function disconnect()
}

/**
* Create a DSN string from a configuration.
* Determine if the given configuration array has a UNIX socket value.
*
* @param array $config
* @param array $config
* @return bool
*/
protected function hasDsnString(array $config)
{
return isset($config['dsn']) && ! empty($config['dsn']);
}

/**
* Get the DSN string for a socket configuration.
*
* @param array $config
* @return string
*/
protected function getDsn(array $config)
protected function getDsnString(array $config)
{
// Check if the user passed a complete dsn to the configuration.
if (!empty($config['dsn'])) {
return $config['dsn'];
$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}";
}

/**
* Get the DSN string for a host / port configuration.
*
* @param array $config
* @return string
*/
protected function getHostDsn(array $config)
{
// Treat host option as array of hosts
$hosts = is_array($config['host']) ? $config['host'] : [$config['host']];

Expand All @@ -178,6 +204,19 @@ protected function getDsn(array $config)
return 'mongodb://' . implode(',', $hosts) . ($auth_database ? '/' . $auth_database : '');
}

/**
* Create a DSN string from a configuration.
*
* @param array $config
* @return string
*/
protected function getDsn(array $config)
{
return $this->hasDsnString($config)
? $this->getDsnString($config)
: $this->getHostDsn($config);
}

/**
* @inheritdoc
*/
Expand Down