Skip to content

Commit 20de677

Browse files
committed
Merge branch 'master' into fix_failed_job_exception
2 parents e29d918 + 04a344b commit 20de677

File tree

7 files changed

+52
-26
lines changed

7 files changed

+52
-26
lines changed

src/Jenssegers/Mongodb/Auth/DatabaseTokenRepository.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use DateTime;
66
use DateTimeZone;
77
use Illuminate\Auth\Passwords\DatabaseTokenRepository as BaseDatabaseTokenRepository;
8+
use Illuminate\Support\Facades\Date;
89
use MongoDB\BSON\UTCDateTime;
910

1011
class DatabaseTokenRepository extends BaseDatabaseTokenRepository
@@ -17,7 +18,7 @@ protected function getPayload($email, $token)
1718
return [
1819
'email' => $email,
1920
'token' => $this->hasher->make($token),
20-
'created_at' => new UTCDateTime(time() * 1000),
21+
'created_at' => new UTCDateTime(Date::now()->format('Uv')),
2122
];
2223
}
2324

@@ -37,7 +38,7 @@ protected function tokenExpired($createdAt)
3738
protected function tokenRecentlyCreated($createdAt)
3839
{
3940
$createdAt = $this->convertDateTime($createdAt);
40-
41+
4142
return parent::tokenRecentlyCreated($createdAt);
4243
}
4344

src/Jenssegers/Mongodb/Eloquent/Model.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
namespace Jenssegers\Mongodb\Eloquent;
44

5-
use Illuminate\Support\Carbon;
65
use DateTime;
76
use Illuminate\Contracts\Queue\QueueableCollection;
87
use Illuminate\Contracts\Queue\QueueableEntity;
98
use Illuminate\Database\Eloquent\Model as BaseModel;
109
use Illuminate\Database\Eloquent\Relations\Relation;
1110
use Illuminate\Support\Arr;
11+
use Illuminate\Support\Facades\Date;
1212
use Illuminate\Support\Str;
1313
use Jenssegers\Mongodb\Query\Builder as QueryBuilder;
1414
use MongoDB\BSON\Binary;
@@ -89,7 +89,7 @@ public function fromDateTime($value)
8989
$value = parent::asDateTime($value);
9090
}
9191

92-
return new UTCDateTime($value->getTimestamp() * 1000);
92+
return new UTCDateTime($value->format('Uv'));
9393
}
9494

9595
/**
@@ -99,7 +99,7 @@ protected function asDateTime($value)
9999
{
100100
// Convert UTCDateTime instances.
101101
if ($value instanceof UTCDateTime) {
102-
return Carbon::createFromTimestamp($value->toDateTime()->getTimestamp());
102+
return Date::createFromTimestampMs($value->toDateTime()->format('Uv'));
103103
}
104104

105105
return parent::asDateTime($value);
@@ -118,7 +118,7 @@ public function getDateFormat()
118118
*/
119119
public function freshTimestamp()
120120
{
121-
return new UTCDateTime(Carbon::now());
121+
return new UTCDateTime(Date::now()->format('Uv'));
122122
}
123123

124124
/**

src/Jenssegers/Mongodb/Query/Builder.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ public function getFresh($columns = [])
294294
}
295295
}
296296
}
297-
297+
298298
// The _id field is mandatory when using grouping.
299299
if ($group && empty($group['_id'])) {
300300
$group['_id'] = null;
@@ -930,18 +930,18 @@ protected function compileWheres()
930930
if (is_array($where['value'])) {
931931
array_walk_recursive($where['value'], function (&$item, $key) {
932932
if ($item instanceof DateTime) {
933-
$item = new UTCDateTime($item->getTimestamp() * 1000);
933+
$item = new UTCDateTime($item->format('Uv'));
934934
}
935935
});
936936
} else {
937937
if ($where['value'] instanceof DateTime) {
938-
$where['value'] = new UTCDateTime($where['value']->getTimestamp() * 1000);
938+
$where['value'] = new UTCDateTime($where['value']->format('Uv'));
939939
}
940940
}
941941
} elseif (isset($where['values'])) {
942942
array_walk_recursive($where['values'], function (&$item, $key) {
943943
if ($item instanceof DateTime) {
944-
$item = new UTCDateTime($item->getTimestamp() * 1000);
944+
$item = new UTCDateTime($item->format('Uv'));
945945
}
946946
});
947947
}

src/Jenssegers/Mongodb/Schema/Blueprint.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,16 +234,18 @@ public function expire($columns, $seconds)
234234
}
235235

236236
/**
237-
* @inheritdoc
237+
* Indicate that the collection needs to be created.
238+
* @param array $options
239+
* @return void
238240
*/
239-
public function create()
241+
public function create($options = [])
240242
{
241243
$collection = $this->collection->getCollectionName();
242244

243245
$db = $this->connection->getMongoDB();
244246

245247
// Ensure the collection is created.
246-
$db->createCollection($collection);
248+
$db->createCollection($collection, $options);
247249
}
248250

249251
/**

src/Jenssegers/Mongodb/Schema/Builder.php

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,20 @@ public function hasColumns($table, array $columns)
3333

3434
/**
3535
* Determine if the given collection exists.
36-
* @param string $collection
36+
* @param string $name
3737
* @return bool
3838
*/
39-
public function hasCollection($collection)
39+
public function hasCollection($name)
4040
{
4141
$db = $this->connection->getMongoDB();
4242

43-
foreach ($db->listCollections() as $collectionFromMongo) {
44-
if ($collectionFromMongo->getName() == $collection) {
45-
return true;
46-
}
47-
}
43+
$collections = iterator_to_array($db->listCollections([
44+
'filter' => [
45+
'name' => $name,
46+
],
47+
]), false);
4848

49-
return false;
49+
return count($collections) ? true : false;
5050
}
5151

5252
/**
@@ -134,6 +134,24 @@ protected function createBlueprint($collection, Closure $callback = null)
134134
return new Blueprint($this->connection, $collection);
135135
}
136136

137+
/**
138+
* Get collection.
139+
* @param string $name
140+
* @return bool|\MongoDB\Model\CollectionInfo
141+
*/
142+
public function getCollection($name)
143+
{
144+
$db = $this->connection->getMongoDB();
145+
146+
$collections = iterator_to_array($db->listCollections([
147+
'filter' => [
148+
'name' => $name,
149+
],
150+
]), false);
151+
152+
return count($collections) ? current($collections) : false;
153+
}
154+
137155
/**
138156
* Get all of the collections names for the database.
139157
* @return array

tests/QueryBuilderTest.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
declare(strict_types=1);
33

4+
use Illuminate\Support\Facades\Date;
45
use Illuminate\Support\Facades\DB;
56
use Jenssegers\Mongodb\Collection;
67
use Jenssegers\Mongodb\Query\Builder;
@@ -545,14 +546,14 @@ public function testUpdateSubdocument()
545546
public function testDates()
546547
{
547548
DB::collection('users')->insert([
548-
['name' => 'John Doe', 'birthday' => new UTCDateTime(1000 * strtotime("1980-01-01 00:00:00"))],
549-
['name' => 'Jane Doe', 'birthday' => new UTCDateTime(1000 * strtotime("1981-01-01 00:00:00"))],
550-
['name' => 'Robert Roe', 'birthday' => new UTCDateTime(1000 * strtotime("1982-01-01 00:00:00"))],
551-
['name' => 'Mark Moe', 'birthday' => new UTCDateTime(1000 * strtotime("1983-01-01 00:00:00"))],
549+
['name' => 'John Doe', 'birthday' => new UTCDateTime(Date::parse("1980-01-01 00:00:00")->format('Uv'))],
550+
['name' => 'Jane Doe', 'birthday' => new UTCDateTime(Date::parse("1981-01-01 00:00:00")->format('Uv'))],
551+
['name' => 'Robert Roe', 'birthday' => new UTCDateTime(Date::parse("1982-01-01 00:00:00")->format('Uv'))],
552+
['name' => 'Mark Moe', 'birthday' => new UTCDateTime(Date::parse("1983-01-01 00:00:00")->format('Uv'))],
552553
]);
553554

554555
$user = DB::collection('users')
555-
->where('birthday', new UTCDateTime(1000 * strtotime("1980-01-01 00:00:00")))
556+
->where('birthday', new UTCDateTime(Date::parse("1980-01-01 00:00:00")->format('Uv')))
556557
->first();
557558
$this->assertEquals('John Doe', $user['name']);
558559

tests/SchemaTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ public function testCreateWithOptions(): void
3434
Schema::create('newcollection_two', null, ['capped' => true, 'size' => 1024]);
3535
$this->assertTrue(Schema::hasCollection('newcollection_two'));
3636
$this->assertTrue(Schema::hasTable('newcollection_two'));
37+
38+
$collection = Schema::getCollection('newcollection_two');
39+
$this->assertTrue($collection['options']['capped']);
40+
$this->assertEquals(1024, $collection['options']['size']);
3741
}
3842

3943
public function testDrop(): void

0 commit comments

Comments
 (0)