11<?php namespace Jenssegers \Mongodb ;
22
3- use Jenssegers \Mongodb \Collection ;
4- use Jenssegers \Mongodb \Query \Builder as QueryBuilder ;
53use MongoClient ;
64
75class Connection extends \Illuminate \Database \Connection {
@@ -34,13 +32,25 @@ public function __construct(array $config)
3432 $ dsn = $ this ->getDsn ($ config );
3533
3634 // You can pass options directly to the MongoClient constructor
37- $ options = array_get ($ config , 'options ' , array () );
35+ $ options = array_get ($ config , 'options ' , [] );
3836
3937 // Create the connection
4038 $ this ->connection = $ this ->createConnection ($ dsn , $ config , $ options );
4139
4240 // Select database
4341 $ 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 ;
4454 }
4555
4656 /**
@@ -51,7 +61,9 @@ public function __construct(array $config)
5161 */
5262 public function collection ($ collection )
5363 {
54- $ query = new QueryBuilder ($ this );
64+ $ processor = $ this ->getPostProcessor ();
65+
66+ $ query = new Query \Builder ($ this , $ processor );
5567
5668 return $ query ->from ($ collection );
5769 }
@@ -120,12 +132,12 @@ protected function createConnection($dsn, array $config, array $options)
120132 {
121133 // Add credentials as options, this makes sure the connection will not fail if
122134 // the username or password contains strange characters.
123- if (isset ( $ config [ ' username ' ]) && $ config ['username ' ])
135+ if ( ! empty ( $ config ['username ' ]) )
124136 {
125137 $ options ['username ' ] = $ config ['username ' ];
126138 }
127139
128- if (isset ( $ config [ ' password ' ]) && $ config ['password ' ])
140+ if ( ! empty ( $ config ['password ' ]) )
129141 {
130142 $ options ['password ' ] = $ config ['password ' ];
131143 }
@@ -156,13 +168,19 @@ protected function getDsn(array $config)
156168 // need to establish the MongoClient and return them back for use.
157169 extract ($ config );
158170
171+ // Check if the user passed a complete dsn to the configuration.
172+ if ( ! empty ($ dsn ))
173+ {
174+ return $ dsn ;
175+ }
176+
159177 // 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] ;
161179
162- // Add ports to hosts
163180 foreach ($ hosts as &$ host )
164181 {
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 ))
166184 {
167185 $ host = "{$ host }: {$ port }" ;
168186 }
0 commit comments