@@ -12,6 +12,22 @@ class MongodbConnectionFactory implements PsrConnectionFactory
12
12
*/
13
13
private $ config ;
14
14
15
+ /**
16
+ * The config could be an array, string DSN or null. In case of null it will attempt to connect to Mongodb localhost with default credentials.
17
+ *
18
+ * $config = [
19
+ * 'uri' => 'mongodb://127.0.0.1/' - Mongodb connection string. see http://docs.mongodb.org/manual/reference/connection-string/
20
+ * 'dbname' => 'enqueue', - database name.
21
+ * 'collection_name' => 'enqueue' - collection name
22
+ * 'polling_interval' => '1000', - How often query for new messages (milliseconds)
23
+ * ]
24
+ *
25
+ * or
26
+ *
27
+ * mongodb://127.0.0.1:27017/dbname/collection_name?polling_interval=1000
28
+ *
29
+ * @param array|string|null $config
30
+ */
15
31
public function __construct ($ config = 'mongodb: ' )
16
32
{
17
33
if (empty ($ config )) {
@@ -24,45 +40,76 @@ public function __construct($config = 'mongodb:')
24
40
}
25
41
$ config = array_replace ([
26
42
'uri ' => 'mongodb://127.0.0.1/ ' ,
27
- 'uriOptions ' => [],
28
- 'driverOptions ' => [],
29
43
], $ config );
30
44
31
45
$ this ->config = $ config ;
32
46
}
33
47
34
48
public function createContext ()
35
49
{
36
- $ client = new Client ($ this ->config ['uri ' ], $ this -> config [ ' uriOptions ' ], $ this -> config [ ' driverOptions ' ] );
50
+ $ client = new Client ($ this ->config ['uri ' ]);
37
51
38
52
return new MongodbContext ($ client , $ this ->config );
39
53
}
40
54
41
- private function parseDsn ($ dsn )
55
+ public static function parseDsn ($ dsn )
42
56
{
43
- if (false === parse_url ($ dsn )) {
57
+ $ parsedUrl = parse_url ($ dsn );
58
+
59
+ if (false === $ parsedUrl ) {
44
60
throw new \LogicException (sprintf ('Failed to parse DSN "%s" ' , $ dsn ));
45
61
}
46
62
47
- $ schema = parse_url ($ dsn , PHP_URL_SCHEME );
48
- if (empty ($ schema )) {
63
+ if (empty ($ parsedUrl ['scheme ' ])) {
49
64
throw new \LogicException ('Schema is empty ' );
50
65
}
51
66
52
67
$ supported = [
53
68
'mongodb ' => true ,
54
69
];
55
70
56
- if (false == isset ($ supported [ $ schema ])) {
71
+ if (false == isset ($ parsedUrl [ ' scheme ' ])) {
57
72
throw new \LogicException (sprintf (
58
73
'The given DSN schema "%s" is not supported. There are supported schemes: "%s". ' ,
59
- $ schema ,
74
+ $ parsedUrl [ ' scheme ' ] ,
60
75
implode ('", " ' , array_keys ($ supported ))
61
76
));
62
77
}
63
78
64
- return [
65
- 'uri ' => $ schema .': ' === $ dsn ? $ schema .'://127.0.0.1/ ' : $ dsn ,
66
- ];
79
+ if ($ parsedUrl ['scheme ' ].': ' === $ dsn ) {
80
+ return [
81
+ 'uri ' => $ parsedUrl ['scheme ' ].'://127.0.0.1/ ' ,
82
+ ];
83
+ }
84
+
85
+ $ config ['uri ' ] = $ parsedUrl ['scheme ' ].':// ' .$ parsedUrl ['host ' ];
86
+
87
+ if (isset ($ parsedUrl ['path ' ]) && '/ ' !== $ parsedUrl ['path ' ]) {
88
+ $ pathArr = explode ('/ ' , $ parsedUrl ['path ' ]);
89
+
90
+ //DB name
91
+ if ($ pathArr [1 ]) {
92
+ $ config ['dbname ' ] = $ pathArr [1 ];
93
+ $ config ['uri ' ] .= '/ ' .$ pathArr [1 ];
94
+ }
95
+
96
+ //Collection name
97
+ if ($ pathArr [2 ]) {
98
+ $ config ['collection_name ' ] = $ pathArr [2 ];
99
+ }
100
+ }
101
+
102
+ if (isset ($ parsedUrl ['query ' ])) {
103
+ $ config ['uri ' ] .= '? ' .$ parsedUrl ['query ' ];
104
+ //get polling_interval value
105
+ if (false !== strpos ($ parsedUrl ['query ' ], 'polling_interval ' )) {
106
+ preg_match_all ('/polling_interval=([0-9]+)/ ' , $ parsedUrl ['query ' ], $ matches );
107
+ if (isset ($ matches [1 ][0 ])) {
108
+ $ config ['polling_interval ' ] = $ matches [1 ][0 ];
109
+ }
110
+ }
111
+ }
112
+
113
+ return $ config ;
67
114
}
68
115
}
0 commit comments