Skip to content

Commit c3b622a

Browse files
author
Steve Porter
committed
fix: fixes dsn connection strings by url encoding
1 parent f12f766 commit c3b622a

File tree

1 file changed

+39
-7
lines changed

1 file changed

+39
-7
lines changed

src/Jenssegers/Mongodb/Connection.php

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -150,18 +150,37 @@ public function disconnect()
150150
}
151151

152152
/**
153-
* Create a DSN string from a configuration.
153+
* Determine if the given configuration array has a UNIX socket value.
154154
*
155-
* @param array $config
155+
* @param array $config
156+
* @return bool
157+
*/
158+
protected function hasDsnString(array $config)
159+
{
160+
return isset($config['dsn']) && ! empty($config['dsn']);
161+
}
162+
163+
/**
164+
* Get the DSN string for a socket configuration.
165+
*
166+
* @param array $config
156167
* @return string
157168
*/
158-
protected function getDsn(array $config)
169+
protected function getDsnString(array $config)
159170
{
160-
// Check if the user passed a complete dsn to the configuration.
161-
if (!empty($config['dsn'])) {
162-
return $config['dsn'];
163-
}
171+
$dsn = rawurlencode($config['dsn']);
172+
173+
return "mongodb://{$dsn}";
174+
}
164175

176+
/**
177+
* Get the DSN string for a host / port configuration.
178+
*
179+
* @param array $config
180+
* @return string
181+
*/
182+
protected function getHostDsn(array $config)
183+
{
165184
// Treat host option as array of hosts
166185
$hosts = is_array($config['host']) ? $config['host'] : [$config['host']];
167186

@@ -178,6 +197,19 @@ protected function getDsn(array $config)
178197
return 'mongodb://' . implode(',', $hosts) . ($auth_database ? '/' . $auth_database : '');
179198
}
180199

200+
/**
201+
* Create a DSN string from a configuration.
202+
*
203+
* @param array $config
204+
* @return string
205+
*/
206+
protected function getDsn(array $config)
207+
{
208+
return $this->hasDsnString($config)
209+
? $this->getDsnString($config)
210+
: $this->getHostDsn($config);
211+
}
212+
181213
/**
182214
* @inheritdoc
183215
*/

0 commit comments

Comments
 (0)