4
4
5
5
use Illuminate \Database \Connection as BaseConnection ;
6
6
use Illuminate \Support \Arr ;
7
+ use Illuminate \Support \Str ;
7
8
use MongoDB \Client ;
8
9
9
10
class Connection extends BaseConnection
@@ -150,18 +151,43 @@ public function disconnect()
150
151
}
151
152
152
153
/**
153
- * Create a DSN string from a configuration .
154
+ * Determine if the given configuration array has a UNIX socket value .
154
155
*
155
- * @param array $config
156
+ * @param array $config
157
+ * @return bool
158
+ */
159
+ protected function hasDsnString (array $ config )
160
+ {
161
+ return isset ($ config ['dsn ' ]) && ! empty ($ config ['dsn ' ]);
162
+ }
163
+
164
+ /**
165
+ * Get the DSN string for a socket configuration.
166
+ *
167
+ * @param array $config
156
168
* @return string
157
169
*/
158
- protected function getDsn (array $ config )
170
+ protected function getDsnString (array $ config )
159
171
{
160
- // Check if the user passed a complete dsn to the configuration.
161
- if (!empty ($ config ['dsn ' ])) {
162
- return $ config ['dsn ' ];
172
+ $ dsn_string = $ config ['dsn ' ];
173
+
174
+ if ( Str::contains ($ dsn_string , 'mongodb:// ' ) ){
175
+ $ dsn_string = Str::replaceFirst ('mongodb:// ' , '' , $ dsn_string );
163
176
}
164
177
178
+ $ dsn_string = rawurlencode ($ dsn_string );
179
+
180
+ return "mongodb:// {$ dsn_string }" ;
181
+ }
182
+
183
+ /**
184
+ * Get the DSN string for a host / port configuration.
185
+ *
186
+ * @param array $config
187
+ * @return string
188
+ */
189
+ protected function getHostDsn (array $ config )
190
+ {
165
191
// Treat host option as array of hosts
166
192
$ hosts = is_array ($ config ['host ' ]) ? $ config ['host ' ] : [$ config ['host ' ]];
167
193
@@ -178,6 +204,19 @@ protected function getDsn(array $config)
178
204
return 'mongodb:// ' . implode (', ' , $ hosts ) . ($ auth_database ? '/ ' . $ auth_database : '' );
179
205
}
180
206
207
+ /**
208
+ * Create a DSN string from a configuration.
209
+ *
210
+ * @param array $config
211
+ * @return string
212
+ */
213
+ protected function getDsn (array $ config )
214
+ {
215
+ return $ this ->hasDsnString ($ config )
216
+ ? $ this ->getDsnString ($ config )
217
+ : $ this ->getHostDsn ($ config );
218
+ }
219
+
181
220
/**
182
221
* @inheritdoc
183
222
*/
0 commit comments