Skip to content

Commit 4e08f27

Browse files
committed
Merge pull request mongodb#1 from jenssegers/master
Merge back master
2 parents 90274a3 + 1e78631 commit 4e08f27

39 files changed

+579
-278
lines changed

Diff for: .travis.yml

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
language: php
22

33
php:
4-
- 5.6
54
- 7
65
- 7.1
76

Diff for: README.md

+23-1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ composer require jenssegers/mongodb
4141
5.2.x | 2.3.x or 3.0.x
4242
5.3.x | 3.1.x or 3.2.x
4343
5.4.x | 3.2.x
44+
5.5.x | 3.3.x
4445

4546
And add the service provider in `config/app.php`:
4647

@@ -102,6 +103,15 @@ Embedded relations now return an `Illuminate\Database\Eloquent\Collection` rathe
102103
$books = $user->books()->sortBy('title');
103104
```
104105

106+
Testing
107+
-------
108+
109+
To run the test for this package, run:
110+
111+
```
112+
docker-compose up
113+
```
114+
105115
Configuration
106116
-------------
107117

@@ -143,6 +153,18 @@ You can connect to multiple servers or replica sets with the following configura
143153
],
144154
```
145155

156+
Alternatively, you can use MongoDB connection string:
157+
158+
```php
159+
'mongodb' => [
160+
'driver' => 'mongodb',
161+
'dsn' => env('DB_DSN'),
162+
'database' => env('DB_DATABASE'),
163+
],
164+
```
165+
166+
Please refer to MongoDB official docs for its URI format: https://docs.mongodb.com/manual/reference/connection-string/
167+
146168
Eloquent
147169
--------
148170

@@ -942,7 +964,7 @@ $cursor = DB::collection('users')->raw(function($collection)
942964
Optional: if you don't pass a closure to the raw method, the internal MongoCollection object will be accessible:
943965

944966
```php
945-
$model = User::raw()->findOne(['age' => array('$lt' => 18]));
967+
$model = User::raw()->findOne(['age' => array('$lt' => 18)]);
946968
```
947969

948970
The internal MongoClient and MongoDB objects can be accessed like this:

Diff for: composer.json

+8-7
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,18 @@
1111
],
1212
"license" : "MIT",
1313
"require": {
14-
"illuminate/support": "^5.1",
15-
"illuminate/container": "^5.1",
16-
"illuminate/database": "^5.1",
17-
"illuminate/events": "^5.1",
14+
"illuminate/support": "^5.5",
15+
"illuminate/container": "^5.5",
16+
"illuminate/database": "^5.5",
17+
"illuminate/events": "^5.5",
1818
"mongodb/mongodb": "^1.0.0"
1919
},
2020
"require-dev": {
21-
"phpunit/phpunit": "^5.0|^6.0",
21+
"phpunit/phpunit": "^6.0",
2222
"orchestra/testbench": "^3.1",
23-
"mockery/mockery": "^0.9",
24-
"satooshi/php-coveralls": "^1.0"
23+
"mockery/mockery": "^1.0",
24+
"satooshi/php-coveralls": "^2.0",
25+
"doctrine/dbal": "^2.5"
2526
},
2627
"autoload": {
2728
"psr-0": {

Diff for: docker-compose.yml

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
version: '3'
2+
3+
services:
4+
5+
php:
6+
build:
7+
context: .
8+
dockerfile: docker/Dockerfile
9+
volumes:
10+
- .:/code
11+
working_dir: /code
12+
command: docker/entrypoint.sh
13+
depends_on:
14+
- mysql
15+
- mongodb
16+
17+
mysql:
18+
image: mysql
19+
environment:
20+
MYSQL_ROOT_PASSWORD:
21+
MYSQL_DATABASE: unittest
22+
MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
23+
logging:
24+
driver: none
25+
26+
mongodb:
27+
image: mongo
28+
logging:
29+
driver: none

Diff for: docker/Dockerfile

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
FROM php:7.1-cli
2+
3+
RUN apt-get update && \
4+
apt-get install -y autoconf pkg-config libssl-dev && \
5+
pecl install mongodb && docker-php-ext-enable mongodb && \
6+
docker-php-ext-install -j$(nproc) pdo pdo_mysql

Diff for: docker/entrypoint.sh

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env bash
2+
3+
sleep 3 && php ./vendor/bin/phpunit

Diff for: src/Jenssegers/Mongodb/Auth/DatabaseTokenRepository.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ protected function tokenExpired($token)
2727
$date = $token['created_at']->toDateTime();
2828
$date->setTimezone(new DateTimeZone(date_default_timezone_get()));
2929
$token['created_at'] = $date->format('Y-m-d H:i:s');
30-
} elseif (is_array($token['created_at']) and isset($token['created_at']['date'])) {
30+
} elseif (is_array($token['created_at']) && isset($token['created_at']['date'])) {
3131
$date = new DateTime($token['created_at']['date'], new DateTimeZone(isset($token['created_at']['timezone']) ? $token['created_at']['timezone'] : 'UTC'));
3232
$date->setTimezone(new DateTimeZone(date_default_timezone_get()));
3333
$token['created_at'] = $date->format('Y-m-d H:i:s');

Diff for: src/Jenssegers/Mongodb/Auth/User.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
99
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
1010
use Illuminate\Foundation\Auth\Access\Authorizable;
11-
use Jenssegers\Mongodb\Eloquent\Model as Model;
11+
use Jenssegers\Mongodb\Eloquent\Model;
1212

1313
class User extends Model implements
1414
AuthenticatableContract,

Diff for: src/Jenssegers/Mongodb/Connection.php

+47-7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace Jenssegers\Mongodb;
44

55
use Illuminate\Database\Connection as BaseConnection;
6+
use Illuminate\Support\Arr;
7+
use Illuminate\Support\Str;
68
use MongoDB\Client;
79

810
class Connection extends BaseConnection
@@ -34,7 +36,7 @@ public function __construct(array $config)
3436
$dsn = $this->getDsn($config);
3537

3638
// You can pass options directly to the MongoDB constructor
37-
$options = array_get($config, 'options', []);
39+
$options = Arr::get($config, 'options', []);
3840

3941
// Create the connection
4042
$this->connection = $this->createConnection($dsn, $config, $options);
@@ -149,18 +151,43 @@ public function disconnect()
149151
}
150152

151153
/**
152-
* Create a DSN string from a configuration.
154+
* Determine if the given configuration array has a UNIX socket value.
153155
*
154-
* @param array $config
156+
* @param array $config
157+
* @return bool
158+
*/
159+
protected function hasDsnString(array $config)
160+
{
161+
return isset($config['dsn']) && ! empty($config['dsn']);
162+
}
163+
164+
/**
165+
* Get the DSN string for a socket configuration.
166+
*
167+
* @param array $config
155168
* @return string
156169
*/
157-
protected function getDsn(array $config)
170+
protected function getDsnString(array $config)
158171
{
159-
// Check if the user passed a complete dsn to the configuration.
160-
if (!empty($config['dsn'])) {
161-
return $config['dsn'];
172+
$dsn_string = $config['dsn'];
173+
174+
if (Str::contains($dsn_string, 'mongodb://')) {
175+
$dsn_string = Str::replaceFirst('mongodb://', '', $dsn_string);
162176
}
163177

178+
$dsn_string = rawurlencode($dsn_string);
179+
180+
return "mongodb://{$dsn_string}";
181+
}
182+
183+
/**
184+
* Get the DSN string for a host / port configuration.
185+
*
186+
* @param array $config
187+
* @return string
188+
*/
189+
protected function getHostDsn(array $config)
190+
{
164191
// Treat host option as array of hosts
165192
$hosts = is_array($config['host']) ? $config['host'] : [$config['host']];
166193

@@ -177,6 +204,19 @@ protected function getDsn(array $config)
177204
return 'mongodb://' . implode(',', $hosts) . ($auth_database ? '/' . $auth_database : '');
178205
}
179206

207+
/**
208+
* Create a DSN string from a configuration.
209+
*
210+
* @param array $config
211+
* @return string
212+
*/
213+
protected function getDsn(array $config)
214+
{
215+
return $this->hasDsnString($config)
216+
? $this->getDsnString($config)
217+
: $this->getHostDsn($config);
218+
}
219+
180220
/**
181221
* @inheritdoc
182222
*/

Diff for: src/Jenssegers/Mongodb/Eloquent/Builder.php

+17-1
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,14 @@ public function decrement($column, $amount = 1, array $extra = [])
143143
return parent::decrement($column, $amount, $extra);
144144
}
145145

146+
/**
147+
* @inheritdoc
148+
*/
149+
public function chunkById($count, callable $callback, $column = '_id', $alias = null)
150+
{
151+
return parent::chunkById($count, $callback, $column, $alias);
152+
}
153+
146154
/**
147155
* @inheritdoc
148156
*/
@@ -162,10 +170,18 @@ public function raw($expression = null)
162170

163171
return $this->model->newFromBuilder((array) $results);
164172
} // The result is a single object.
165-
elseif (is_array($results) and array_key_exists('_id', $results)) {
173+
elseif (is_array($results) && array_key_exists('_id', $results)) {
166174
return $this->model->newFromBuilder((array) $results);
167175
}
168176

169177
return $results;
170178
}
179+
180+
/**
181+
* @return \Illuminate\Database\ConnectionInterface
182+
*/
183+
public function getConnection()
184+
{
185+
return $this->query->getConnection();
186+
}
171187
}

Diff for: src/Jenssegers/Mongodb/Eloquent/EmbedsRelations.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Jenssegers\Mongodb\Eloquent;
44

5+
use Illuminate\Support\Str;
56
use Jenssegers\Mongodb\Relations\EmbedsMany;
67
use Jenssegers\Mongodb\Relations\EmbedsOne;
78

@@ -32,7 +33,7 @@ protected function embedsMany($related, $localKey = null, $foreignKey = null, $r
3233
}
3334

3435
if (is_null($foreignKey)) {
35-
$foreignKey = snake_case(class_basename($this));
36+
$foreignKey = Str::snake(class_basename($this));
3637
}
3738

3839
$query = $this->newQuery();
@@ -67,7 +68,7 @@ protected function embedsOne($related, $localKey = null, $foreignKey = null, $re
6768
}
6869

6970
if (is_null($foreignKey)) {
70-
$foreignKey = snake_case(class_basename($this));
71+
$foreignKey = Str::snake(class_basename($this));
7172
}
7273

7374
$query = $this->newQuery();

Diff for: src/Jenssegers/Mongodb/Eloquent/HybridRelations.php

+35-5
Original file line numberDiff line numberDiff line change
@@ -214,11 +214,20 @@ public function morphTo($name = null, $type = null, $id = null)
214214
* @param string $collection
215215
* @param string $foreignKey
216216
* @param string $otherKey
217+
* @param string $parentKey
218+
* @param string $relatedKey
217219
* @param string $relation
218220
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
219221
*/
220-
public function belongsToMany($related, $collection = null, $foreignKey = null, $otherKey = null, $relation = null)
221-
{
222+
public function belongsToMany(
223+
$related,
224+
$collection = null,
225+
$foreignKey = null,
226+
$otherKey = null,
227+
$parentKey = null,
228+
$relatedKey = null,
229+
$relation = null
230+
) {
222231
// If no relationship name was passed, we will pull backtraces to get the
223232
// name of the calling function. We will use that function name as the
224233
// title of this relation since that is a great convention to apply.
@@ -228,7 +237,15 @@ public function belongsToMany($related, $collection = null, $foreignKey = null,
228237

229238
// Check if it is a relation with an original model.
230239
if (!is_subclass_of($related, \Jenssegers\Mongodb\Eloquent\Model::class)) {
231-
return parent::belongsToMany($related, $collection, $foreignKey, $otherKey, $relation);
240+
return parent::belongsToMany(
241+
$related,
242+
$collection,
243+
$foreignKey,
244+
$otherKey,
245+
$parentKey,
246+
$relatedKey,
247+
$relation
248+
);
232249
}
233250

234251
// First, we'll need to determine the foreign key and "other key" for the
@@ -252,7 +269,16 @@ public function belongsToMany($related, $collection = null, $foreignKey = null,
252269
// appropriate query constraint and entirely manages the hydrations.
253270
$query = $instance->newQuery();
254271

255-
return new BelongsToMany($query, $this, $collection, $foreignKey, $otherKey, $relation);
272+
return new BelongsToMany(
273+
$query,
274+
$this,
275+
$collection,
276+
$foreignKey,
277+
$otherKey,
278+
$parentKey ?: $this->getKeyName(),
279+
$relatedKey ?: $instance->getKeyName(),
280+
$relation
281+
);
256282
}
257283

258284
/**
@@ -274,6 +300,10 @@ protected function guessBelongsToManyRelation()
274300
*/
275301
public function newEloquentBuilder($query)
276302
{
277-
return new EloquentBuilder($query);
303+
if (is_subclass_of($this, \Jenssegers\Mongodb\Eloquent\Model::class)) {
304+
return new Builder($query);
305+
} else {
306+
return new EloquentBuilder($query);
307+
}
278308
}
279309
}

0 commit comments

Comments
 (0)