Skip to content

Commit 97dbdfc

Browse files
committed
Connection tweaks, fixes #413
1 parent ca1a929 commit 97dbdfc

File tree

6 files changed

+58
-26
lines changed

6 files changed

+58
-26
lines changed

src/Jenssegers/Eloquent/Model.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public function belongsTo($related, $foreignKey = null, $otherKey = null, $relat
143143

144144
$relation = $caller['function'];
145145
}
146-
146+
147147
// Check if it is a relation with an original model.
148148
if (!is_subclass_of($related, 'Jenssegers\Mongodb\Model'))
149149
{
@@ -234,7 +234,7 @@ public function belongsToMany($related, $collection = null, $foreignKey = null,
234234
{
235235
$relation = $this->getBelongsToManyCaller();
236236
}
237-
237+
238238
// Check if it is a relation with an original model.
239239
if (!is_subclass_of($related, 'Jenssegers\Mongodb\Model'))
240240
{
@@ -278,7 +278,7 @@ protected function newBaseQueryBuilder()
278278
// Check the connection type
279279
if ($connection instanceof \Jenssegers\Mongodb\Connection)
280280
{
281-
return new QueryBuilder($connection);
281+
return new QueryBuilder($connection, $connection->getPostProcessor());
282282
}
283283

284284
return parent::newBaseQueryBuilder();

src/Jenssegers/Mongodb/Collection.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?php namespace Jenssegers\Mongodb;
22

3-
use Exception;
4-
use MongoCollection;
3+
use Exception, MongoCollection;
54
use Jenssegers\Mongodb\Connection;
65

76
class Collection {

src/Jenssegers/Mongodb/Connection.php

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
<?php namespace Jenssegers\Mongodb;
22

3-
use Jenssegers\Mongodb\Collection;
4-
use Jenssegers\Mongodb\Query\Builder as QueryBuilder;
53
use MongoClient;
64

75
class 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
}

src/Jenssegers/Mongodb/Query/Builder.php

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
<?php namespace Jenssegers\Mongodb\Query;
22

3-
use MongoId;
4-
use MongoRegex;
5-
use MongoDate;
6-
use DateTime;
7-
use Closure;
8-
9-
use Illuminate\Database\Query\Builder as QueryBuilder;
3+
use MongoId, MongoRegex, MongoDate, DateTime, Closure;
4+
use Illuminate\Database\Query\Builder as BaseBuilder;
105
use Illuminate\Database\Query\Expression;
116
use Jenssegers\Mongodb\Connection;
127

13-
class Builder extends QueryBuilder {
8+
class Builder extends BaseBuilder {
149

1510
/**
1611
* The database collection
@@ -69,9 +64,10 @@ class Builder extends QueryBuilder {
6964
* @param Connection $connection
7065
* @return void
7166
*/
72-
public function __construct(Connection $connection)
67+
public function __construct(Connection $connection, Processor $processor)
7368
{
7469
$this->connection = $connection;
70+
$this->processor = $processor;
7571
}
7672

7773
/**
@@ -656,7 +652,7 @@ public function drop($columns)
656652
*/
657653
public function newQuery()
658654
{
659-
return new Builder($this->connection);
655+
return new Builder($this->connection, $this->processor);
660656
}
661657

662658
/**
@@ -716,12 +712,12 @@ public function convertKey($id)
716712
public function where($column, $operator = null, $value = null, $boolean = 'and')
717713
{
718714
$params = func_get_args();
719-
715+
720716
// Remove the leading $ from operators.
721717
if (func_num_args() == 3)
722718
{
723719
$operator = &$params[1];
724-
720+
725721
if (starts_with($operator, '$'))
726722
{
727723
$operator = substr($operator, 1);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php namespace Jenssegers\Mongodb\Query;
2+
3+
use Illuminate\Database\Query\Processors\Processor as BaseProcessor;
4+
5+
class Processor extends BaseProcessor {
6+
7+
8+
}

tests/ConnectionTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,15 @@ public function testCustomPort()
119119
$connection = DB::connection('mongodb');
120120
}
121121

122+
public function testHostWithPorts()
123+
{
124+
$hosts = ['localhost:27001', 'localhost:27002'];
125+
Config::set('database.connections.mongodb.port', 27000);
126+
Config::set('database.connections.mongodb.host', ['localhost:27001', 'localhost:27002']);
127+
$database = Config::get('database.connections.mongodb.database');
128+
129+
$this->setExpectedException('MongoConnectionException', "Failed to connect to: " . $hosts[0] . ": Connection refused; Failed to connect to: " . $hosts[1] . ": Connection refused");
130+
$connection = DB::connection('mongodb');
131+
}
132+
122133
}

0 commit comments

Comments
 (0)