1
1
<?php namespace Jenssegers \Mongodb ;
2
2
3
- use Jenssegers \Mongodb \Collection ;
4
- use Jenssegers \Mongodb \Query \Builder as QueryBuilder ;
5
3
use MongoClient ;
6
4
7
5
class Connection extends \Illuminate \Database \Connection {
@@ -34,13 +32,25 @@ public function __construct(array $config)
34
32
$ dsn = $ this ->getDsn ($ config );
35
33
36
34
// You can pass options directly to the MongoClient constructor
37
- $ options = array_get ($ config , 'options ' , array () );
35
+ $ options = array_get ($ config , 'options ' , [] );
38
36
39
37
// Create the connection
40
38
$ this ->connection = $ this ->createConnection ($ dsn , $ config , $ options );
41
39
42
40
// Select database
43
41
$ this ->db = $ this ->connection ->{$ config ['database ' ]};
42
+
43
+ $ this ->useDefaultPostProcessor ();
44
+ }
45
+
46
+ /**
47
+ * Get the default post processor instance.
48
+ *
49
+ * @return Query\Processor
50
+ */
51
+ protected function getDefaultPostProcessor ()
52
+ {
53
+ return new Query \Processor ;
44
54
}
45
55
46
56
/**
@@ -51,7 +61,9 @@ public function __construct(array $config)
51
61
*/
52
62
public function collection ($ collection )
53
63
{
54
- $ query = new QueryBuilder ($ this );
64
+ $ processor = $ this ->getPostProcessor ();
65
+
66
+ $ query = new Query \Builder ($ this , $ processor );
55
67
56
68
return $ query ->from ($ collection );
57
69
}
@@ -120,12 +132,12 @@ protected function createConnection($dsn, array $config, array $options)
120
132
{
121
133
// Add credentials as options, this makes sure the connection will not fail if
122
134
// the username or password contains strange characters.
123
- if (isset ( $ config [ ' username ' ]) && $ config ['username ' ])
135
+ if ( ! empty ( $ config ['username ' ]) )
124
136
{
125
137
$ options ['username ' ] = $ config ['username ' ];
126
138
}
127
139
128
- if (isset ( $ config [ ' password ' ]) && $ config ['password ' ])
140
+ if ( ! empty ( $ config ['password ' ]) )
129
141
{
130
142
$ options ['password ' ] = $ config ['password ' ];
131
143
}
@@ -156,13 +168,19 @@ protected function getDsn(array $config)
156
168
// need to establish the MongoClient and return them back for use.
157
169
extract ($ config );
158
170
171
+ // Check if the user passed a complete dsn to the configuration.
172
+ if ( ! empty ($ dsn ))
173
+ {
174
+ return $ dsn ;
175
+ }
176
+
159
177
// Treat host option as array of hosts
160
- $ hosts = is_array ($ config [ ' host ' ] ) ? $ config [ ' host ' ] : array ( $ config [ ' host ' ]) ;
178
+ $ hosts = is_array ($ host ) ? $ host : [ $ host] ;
161
179
162
- // Add ports to hosts
163
180
foreach ($ hosts as &$ host )
164
181
{
165
- if (isset ($ config ['port ' ]))
182
+ // Check if we need to add a port to the host
183
+ if (strpos ($ host , ': ' ) === false and isset ($ port ))
166
184
{
167
185
$ host = "{$ host }: {$ port }" ;
168
186
}
0 commit comments