Skip to content

Commit 7dab675

Browse files
authored
Merge pull request #2506 from alcaeus/l10-compatibility
Laravel 10 compatibility
2 parents 0b03010 + 226a709 commit 7dab675

File tree

14 files changed

+145
-116
lines changed

14 files changed

+145
-116
lines changed

.github/workflows/build-ci.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ jobs:
1414
strategy:
1515
matrix:
1616
php:
17-
- '8.0'
17+
- '8.1'
1818
steps:
1919
- name: Checkout
20-
uses: actions/checkout@v2
20+
uses: actions/checkout@v3
2121
- name: Setup PHP
2222
uses: shivammathur/setup-php@v2
2323
with:
@@ -41,8 +41,8 @@ jobs:
4141
- '4.4'
4242
- '5.0'
4343
php:
44-
- '8.0'
4544
- '8.1'
45+
- '8.2'
4646
services:
4747
mysql:
4848
image: mysql:5.7
@@ -54,7 +54,7 @@ jobs:
5454
MYSQL_ROOT_PASSWORD:
5555

5656
steps:
57-
- uses: actions/checkout@v2
57+
- uses: actions/checkout@v3
5858
- name: Create MongoDB Replica Set
5959
run: |
6060
docker run --name mongodb -p 27017:27017 -e MONGO_INITDB_DATABASE=unittest --detach mongo:${{ matrix.mongodb }} mongod --replSet rs --setParameter transactionLifetimeLimitSeconds=5

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,6 @@ use Jenssegers\Mongodb\Eloquent\SoftDeletes;
256256
class User extends Model
257257
{
258258
use SoftDeletes;
259-
260-
protected $dates = ['deleted_at'];
261259
}
262260
```
263261

@@ -279,7 +277,7 @@ use Jenssegers\Mongodb\Eloquent\Model;
279277

280278
class User extends Model
281279
{
282-
protected $dates = ['birthday'];
280+
protected $casts = ['birthday' => 'datetime'];
283281
}
284282
```
285283

composer.json

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,16 @@
1919
],
2020
"license": "MIT",
2121
"require": {
22-
"illuminate/support": "^9.0",
23-
"illuminate/container": "^9.0",
24-
"illuminate/database": "^9.0",
25-
"illuminate/events": "^9.0",
26-
"mongodb/mongodb": "^1.11"
22+
"illuminate/support": "^10.0",
23+
"illuminate/container": "^10.0",
24+
"illuminate/database": "^10.0",
25+
"illuminate/events": "^10.0",
26+
"mongodb/mongodb": "^1.15"
2727
},
2828
"require-dev": {
29-
"phpunit/phpunit": "^9.5.8",
30-
"orchestra/testbench": "^7.0",
31-
"mockery/mockery": "^1.3.1",
32-
"doctrine/dbal": "^2.13.3|^3.1.4"
29+
"phpunit/phpunit": "^9.5.10",
30+
"orchestra/testbench": "^8.0",
31+
"mockery/mockery": "^1.4.4"
3332
},
3433
"autoload": {
3534
"psr-4": {
@@ -54,5 +53,6 @@
5453
"Jenssegers\\Mongodb\\MongodbQueueServiceProvider"
5554
]
5655
}
57-
}
56+
},
57+
"minimum-stability": "dev"
5858
}

src/Auth/PasswordBrokerManager.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ protected function createTokenRepository(array $config)
1616
$this->app['hash'],
1717
$config['table'],
1818
$this->app['config']['app.key'],
19-
$config['expire']
19+
$config['expire'],
20+
$config['throttle'] ?? 0
2021
);
2122
}
2223
}

src/Auth/PasswordResetServiceProvider.php

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,6 @@
66

77
class PasswordResetServiceProvider extends BasePasswordResetServiceProvider
88
{
9-
/**
10-
* Register the token repository implementation.
11-
*
12-
* @return void
13-
*/
14-
protected function registerTokenRepository()
15-
{
16-
$this->app->singleton('auth.password.tokens', function ($app) {
17-
$connection = $app['db']->connection();
18-
19-
// The database token repository is an implementation of the token repository
20-
// interface, and is responsible for the actual storing of auth tokens and
21-
// their e-mail addresses. We will inject this table and hash key to it.
22-
$table = $app['config']['auth.password.table'];
23-
24-
$key = $app['config']['app.key'];
25-
26-
$expire = $app['config']->get('auth.password.expire', 60);
27-
28-
return new DatabaseTokenRepository($connection, $table, $key, $expire);
29-
});
30-
}
31-
329
/**
3310
* @inheritdoc
3411
*/

src/Eloquent/Model.php

Lines changed: 67 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,23 @@
22

33
namespace Jenssegers\Mongodb\Eloquent;
44

5+
use function array_key_exists;
56
use DateTimeInterface;
7+
use function explode;
68
use Illuminate\Contracts\Queue\QueueableCollection;
79
use Illuminate\Contracts\Queue\QueueableEntity;
10+
use Illuminate\Contracts\Support\Arrayable;
811
use Illuminate\Database\Eloquent\Model as BaseModel;
912
use Illuminate\Database\Eloquent\Relations\Relation;
1013
use Illuminate\Support\Arr;
1114
use Illuminate\Support\Facades\Date;
1215
use Illuminate\Support\Str;
16+
use function in_array;
1317
use Jenssegers\Mongodb\Query\Builder as QueryBuilder;
1418
use MongoDB\BSON\Binary;
1519
use MongoDB\BSON\ObjectID;
1620
use MongoDB\BSON\UTCDateTime;
21+
use function uniqid;
1722

1823
abstract class Model extends BaseModel
1924
{
@@ -94,7 +99,7 @@ public function fromDateTime($value)
9499
$value = parent::asDateTime($value);
95100
}
96101

97-
return new UTCDateTime($value->format('Uv'));
102+
return new UTCDateTime($value);
98103
}
99104

100105
/**
@@ -182,7 +187,7 @@ protected function getAttributeFromArray($key)
182187
/**
183188
* @inheritdoc
184189
*/
185-
public function setAttribute($key, $value)
190+
public function setAttribute($key, $value): static
186191
{
187192
// Convert _id to ObjectID.
188193
if ($key == '_id' && is_string($value)) {
@@ -191,13 +196,14 @@ public function setAttribute($key, $value)
191196
$value = $builder->convertKey($value);
192197
} // Support keys in dot notation.
193198
elseif (Str::contains($key, '.')) {
194-
if (in_array($key, $this->getDates()) && $value) {
195-
$value = $this->fromDateTime($value);
196-
}
199+
// Store to a temporary key, then move data to the actual key
200+
$uniqueKey = uniqid($key);
201+
parent::setAttribute($uniqueKey, $value);
197202

198-
Arr::set($this->attributes, $key, $value);
203+
Arr::set($this->attributes, $key, $this->attributes[$uniqueKey] ?? null);
204+
unset($this->attributes[$uniqueKey]);
199205

200-
return;
206+
return $this;
201207
}
202208

203209
return parent::setAttribute($key, $value);
@@ -222,13 +228,6 @@ public function attributesToArray()
222228
}
223229
}
224230

225-
// Convert dot-notation dates.
226-
foreach ($this->getDates() as $key) {
227-
if (Str::contains($key, '.') && Arr::has($attributes, $key)) {
228-
Arr::set($attributes, $key, (string) $this->asDateTime(Arr::get($attributes, $key)));
229-
}
230-
}
231-
232231
return $attributes;
233232
}
234233

@@ -515,4 +514,58 @@ public function __call($method, $parameters)
515514

516515
return parent::__call($method, $parameters);
517516
}
517+
518+
/**
519+
* @inheritdoc
520+
*/
521+
protected function addCastAttributesToArray(array $attributes, array $mutatedAttributes): array
522+
{
523+
foreach ($this->getCasts() as $key => $castType) {
524+
if (! Arr::has($attributes, $key) || Arr::has($mutatedAttributes, $key)) {
525+
continue;
526+
}
527+
528+
$originalValue = Arr::get($attributes, $key);
529+
530+
// Here we will cast the attribute. Then, if the cast is a date or datetime cast
531+
// then we will serialize the date for the array. This will convert the dates
532+
// to strings based on the date format specified for these Eloquent models.
533+
$castValue = $this->castAttribute(
534+
$key, $originalValue
535+
);
536+
537+
// If the attribute cast was a date or a datetime, we will serialize the date as
538+
// a string. This allows the developers to customize how dates are serialized
539+
// into an array without affecting how they are persisted into the storage.
540+
if ($castValue !== null && in_array($castType, ['date', 'datetime', 'immutable_date', 'immutable_datetime'])) {
541+
$castValue = $this->serializeDate($castValue);
542+
}
543+
544+
if ($castValue !== null && ($this->isCustomDateTimeCast($castType) ||
545+
$this->isImmutableCustomDateTimeCast($castType))) {
546+
$castValue = $castValue->format(explode(':', $castType, 2)[1]);
547+
}
548+
549+
if ($castValue instanceof DateTimeInterface &&
550+
$this->isClassCastable($key)) {
551+
$castValue = $this->serializeDate($castValue);
552+
}
553+
554+
if ($castValue !== null && $this->isClassSerializable($key)) {
555+
$castValue = $this->serializeClassCastableAttribute($key, $castValue);
556+
}
557+
558+
if ($this->isEnumCastable($key) && (! $castValue instanceof Arrayable)) {
559+
$castValue = $castValue !== null ? $this->getStorableEnumValue($attributes[$key]) : null;
560+
}
561+
562+
if ($castValue instanceof Arrayable) {
563+
$castValue = $castValue->toArray();
564+
}
565+
566+
Arr::set($attributes, $key, $castValue);
567+
}
568+
569+
return $attributes;
570+
}
518571
}

src/Relations/EmbedsMany.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function performInsert(Model $model)
5252
}
5353

5454
// Push the new model to the database.
55-
$result = $this->getBaseQuery()->push($this->localKey, $model->getAttributes(), true);
55+
$result = $this->toBase()->push($this->localKey, $model->getAttributes(), true);
5656

5757
// Attach the model to its parent.
5858
if ($result) {
@@ -83,7 +83,7 @@ public function performUpdate(Model $model)
8383
$values = $this->getUpdateValues($model->getDirty(), $this->localKey.'.$.');
8484

8585
// Update document in database.
86-
$result = $this->getBaseQuery()->where($this->localKey.'.'.$model->getKeyName(), $foreignKey)
86+
$result = $this->toBase()->where($this->localKey.'.'.$model->getKeyName(), $foreignKey)
8787
->update($values);
8888

8989
// Attach the model to its parent.
@@ -112,7 +112,7 @@ public function performDelete(Model $model)
112112
// Get the correct foreign key value.
113113
$foreignKey = $this->getForeignKeyValue($model);
114114

115-
$result = $this->getBaseQuery()->pull($this->localKey, [$model->getKeyName() => $foreignKey]);
115+
$result = $this->toBase()->pull($this->localKey, [$model->getKeyName() => $foreignKey]);
116116

117117
if ($result) {
118118
$this->dissociate($model);

src/Relations/EmbedsOne.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function getEager()
3333
/**
3434
* Save a new model and attach it to the parent model.
3535
*
36-
* @param Model $model
36+
* @param Model $model
3737
* @return Model|bool
3838
*/
3939
public function performInsert(Model $model)
@@ -50,7 +50,7 @@ public function performInsert(Model $model)
5050
return $this->parent->save() ? $model : false;
5151
}
5252

53-
$result = $this->getBaseQuery()->update([$this->localKey => $model->getAttributes()]);
53+
$result = $this->toBase()->update([$this->localKey => $model->getAttributes()]);
5454

5555
// Attach the model to its parent.
5656
if ($result) {
@@ -63,7 +63,7 @@ public function performInsert(Model $model)
6363
/**
6464
* Save an existing model and attach it to the parent model.
6565
*
66-
* @param Model $model
66+
* @param Model $model
6767
* @return Model|bool
6868
*/
6969
public function performUpdate(Model $model)
@@ -76,7 +76,7 @@ public function performUpdate(Model $model)
7676

7777
$values = $this->getUpdateValues($model->getDirty(), $this->localKey.'.');
7878

79-
$result = $this->getBaseQuery()->update($values);
79+
$result = $this->toBase()->update($values);
8080

8181
// Attach the model to its parent.
8282
if ($result) {
@@ -101,7 +101,7 @@ public function performDelete()
101101
}
102102

103103
// Overwrite the local key with an empty array.
104-
$result = $this->getBaseQuery()->update([$this->localKey => null]);
104+
$result = $this->toBase()->update([$this->localKey => null]);
105105

106106
// Detach the model from its parent.
107107
if ($result) {
@@ -114,7 +114,7 @@ public function performDelete()
114114
/**
115115
* Attach the model to its parent.
116116
*
117-
* @param Model $model
117+
* @param Model $model
118118
* @return Model
119119
*/
120120
public function associate(Model $model)
@@ -145,8 +145,8 @@ public function delete()
145145
/**
146146
* Get the name of the "where in" method for eager loading.
147147
*
148-
* @param \Illuminate\Database\Eloquent\Model $model
149-
* @param string $key
148+
* @param \Illuminate\Database\Eloquent\Model $model
149+
* @param string $key
150150
* @return string
151151
*/
152152
protected function whereInMethod(EloquentModel $model, $key)

0 commit comments

Comments
 (0)