Unverified Commit da9f4a1a authored by Jens Segers's avatar Jens Segers Committed by GitHub

Merge pull request #1397 from DMNSteve/1396

fix: fixes dsn connection strings by url encoding
parents f12f766d b1ed1667
......@@ -4,6 +4,7 @@ namespace Jenssegers\Mongodb;
use Illuminate\Database\Connection as BaseConnection;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use MongoDB\Client;
class Connection extends BaseConnection
......@@ -150,18 +151,43 @@ class Connection extends BaseConnection
}
/**
* Create a DSN string from a configuration.
* Determine if the given configuration array has a UNIX socket value.
*
* @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']];
......@@ -178,6 +204,19 @@ class Connection extends BaseConnection
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
*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment