File tree Expand file tree Collapse file tree 2 files changed +41
-5
lines changed
lib/Phpfastcache/Drivers/Mongodb Expand file tree Collapse file tree 2 files changed +41
-5
lines changed Original file line number Diff line number Diff line change @@ -38,9 +38,9 @@ class Config extends ConfigurationOption
3838 protected $ password = '' ;
3939
4040 /**
41- * @var string
41+ * @var array
4242 */
43- protected $ servers = '' ;
43+ protected $ servers = [] ;
4444
4545 /**
4646 * @var string
@@ -52,6 +52,11 @@ class Config extends ConfigurationOption
5252 */
5353 protected $ databaseName = Driver::MONGODB_DEFAULT_DB_NAME ;
5454
55+ /**
56+ * @var array
57+ */
58+ protected $ options = [];
59+
5560 /**
5661 * @return string
5762 */
@@ -145,7 +150,7 @@ public function setPassword(string $password): self
145150 /**
146151 * @return string
147152 */
148- public function getServers (): string
153+ public function getServers (): array
149154 {
150155 return $ this ->servers ;
151156 }
@@ -154,7 +159,7 @@ public function getServers(): string
154159 * @param string $servers
155160 * @return self
156161 */
157- public function setServers (string $ servers ): self
162+ public function setServers (array $ servers ): self
158163 {
159164 $ this ->servers = $ servers ;
160165 return $ this ;
@@ -195,4 +200,23 @@ public function setDatabaseName(string $databaseName): self
195200 $ this ->databaseName = $ databaseName ;
196201 return $ this ;
197202 }
203+
204+ /**
205+ * @return array
206+ */
207+ public function getOptions (): array
208+ {
209+ return $ this ->options ;
210+ }
211+
212+ /**
213+ * @see https://docs.mongodb.com/manual/reference/connection-string/#connections-connection-options
214+ * @param array $options
215+ * @return Config
216+ */
217+ public function setOptions (array $ options ): self
218+ {
219+ $ this ->options = $ options ;
220+ return $ this ;
221+ }
198222}
Original file line number Diff line number Diff line change @@ -224,19 +224,31 @@ protected function collectionExists($collectionName): bool
224224 */
225225 protected function buildConnectionURI ($ databaseName = '' ): string
226226 {
227+ $ servers = $ this ->getConfig ()->getServers ();
228+ $ options = $ this ->getConfig ()->getOptions ();
229+
227230 $ host = $ this ->getConfig ()->getHost ();
228231 $ port = $ this ->getConfig ()->getPort ();
229232 $ username = $ this ->getConfig ()->getUsername ();
230233 $ password = $ this ->getConfig ()->getPassword ();
231234
235+ if ( \count ($ servers ) > 0 ){
236+ $ host = \array_reduce ($ servers , function ($ carry , $ data ){
237+ $ carry .= ($ carry === '' ? '' : ', ' ).$ data ['host ' ].': ' .$ data ['port ' ];
238+ return $ carry ;
239+ }, '' );
240+ $ port = false ;
241+ }
242+
232243 return implode ('' , [
233244 'mongodb:// ' ,
234245 ($ username ?: '' ),
235246 ($ password ? ": {$ password }" : '' ),
236247 ($ username ? '@ ' : '' ),
237248 $ host ,
238- ($ port !== 27017 ? ": {$ port }" : '' ),
249+ ($ port !== 27017 && $ port !== false ? ": {$ port }" : '' ),
239250 ($ databaseName ? "/ {$ databaseName }" : '' ),
251+ (\count ($ options ) > 0 ? '? ' .\http_build_query ($ options ) : '' ),
240252 ]);
241253 }
242254
You can’t perform that action at this time.
0 commit comments