8000 use time() and default Binary type · symfony/symfony@d8a704c · GitHub
[go: up one dir, main page]

Skip to content

Commit d8a704c

Browse files
committed
use time() and default Binary type
1 parent 22f279d commit d8a704c

File tree

5 files changed

+30
-29
lines changed

5 files changed

+30
-29
lines changed

src/Symfony/Component/HttpFoundation/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ CHANGELOG
99
* Add `UriSigner` from the HttpKernel component
1010
* Add `partitioned` flag to `Cookie` (CHIPS Cookie)
1111
* Add argument `bool $flush = true` to `Response::send()`
12-
* `MongoDbSessionHandler` can be used with the mongodb extension only
12+
* Make `MongoDbSessionHandler` instantiable with the mongodb extension directly
1313

1414
6.3
1515
---

src/Symfony/Component/HttpFoundation/Session/Storage/Handler/MongoDbSessionHandler.php

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ protected function doWrite(#[\SensitiveParameter] string $sessionId, string $dat
123123
$fields = [
124124
$this->options['time_field'] => $this->getUTCDateTime(),
125125
$this->options['expiry_field'] => $expiry,
126-
$this->options['data_field'] => new Binary($data, Binary::TYPE_OLD_BINARY),
126+
$this->options['data_field'] => new Binary($data),
127127
];
128128

129129
$write = new BulkWrite();
@@ -160,30 +160,27 @@ public function updateTimestamp(#[\SensitiveParameter] string $sessionId, string
160160

161161
protected function doRead(#[\SensitiveParameter] string $sessionId): string
162162
{
163-
$dbData = $this->manager->executeQuery($this->namespace, new Query([
163+
$cursor = $this->manager->executeQuery($this->namespace, new Query([
164164
$this->options['id_field'] => $sessionId,
165165
$this->options['expiry_field'] => ['$gte' => $this->getUTCDateTime()],
166166
], [
167167
'projection' => [
168168
'_id' => false,
169169
$this->options['data_field'] => true,
170170
],
171+
'limit' => 1,
171172
]));
172-
$dbData->setTypeMap(['root' => 'object']);
173-
$dbData->rewind();
174-
$data = $dbData->current();
175173

176-
if (!$data) {
177-
return '';
174+
foreach ($cursor as $document) {
175+
return (string) $document->{$this->options['data_field']} ?? '';
178176
}
179177

180-
return (string) $data->{$this->options['data_field']};
178+
// Not found
179+
return '';
181180
}
182181

183182
private function getUTCDateTime(int $additionalSeconds = 0): UTCDateTime
184183
{
185-
$timestamp = new \DateTimeImmutable('now', new \DateTimeZone('UTC'));
186-
187-
return new UTCDateTime(($timestamp->getTimestamp() + $additionalSeconds) * 1000);
184+
return new UTCDateTime((time() + $additionalSeconds) * 1000);
188185
}
189186
}

src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/MongoDbSessionHandlerTest.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,15 @@
2828
* @author Markus Bachmann <markus.bachmann@bachi.biz>
2929
*
3030
* @group integration
31+
* @group time-sensitive
3132
*
3233
* @requires extension mongodb
3334
*/
3435
class MongoDbSessionHandlerTest extends TestCase
3536
{
37+
private const DABASE_NAME = 'sf-test';
38+
private const COLLECTION_NAME = 'session-test';
39+
3640
public array $options;
3741
private Manager $manager;
3842
private MongoDbSessionHandler $storage;
@@ -44,7 +48,7 @@ protected function setUp(): void
4448
$this->manager = new Manager('mongodb://'.getenv('MONGODB_HOST'));
4549

4650
try {
47-
$this->manager->executeCommand('sf-test', new Command(['ping' => 1]));
51+
$this->manager->executeCommand(self::DABASE_NAME, new Command(['ping' => 1]));
4852
} catch (ConnectionException $e) {
4953
$this->markTestSkipped(sprintf('MongoDB Server "%s" not running: %s', getenv('MONGODB_HOST'), $e->getMessage()));
5054
}
@@ -54,8 +58,8 @@ protected function setUp(): void
5458
'data_field' => 'data',
5559
'time_field' => 'time',
5660
'expiry_field' => 'expires_at',
57-
'database' => 'sf-test',
58-
'collection' => 'session-test',
61+
'database' => self::DABASE_NAME,
62+
'collection' => self::COLLECTION_NAME,
5963
];
6064

6165
$this->storage = new MongoDbSessionHandler($this->manager, $this->options);
@@ -76,7 +80,7 @@ public function testCreateFromClient()
7680

7781
protected function tearDown(): void
7882
{
79-
$this->manager->executeCommand('sf-test', new Command(['drop' => 'session-test']));
83+
$this->manager->executeCommand(self::DABASE_NAME, new Command(['drop' => self::COLLECTION_NAME]));
8084
}
8185

8286
/** @dataProvider provideInvalidOptions */
@@ -177,21 +181,21 @@ public function testGc()
177181
*/
178182
private function getSessions(): array
179183
{
180-
return $this->manager->executeQuery('sf-test.session-test', new Query([]))->toArray();
184+
return $this->manager->executeQuery(self::DABASE_NAME.'.'.self::COLLECTION_NAME, new Query([]))->toArray();
181185
}
182186

183187
private function insertSession(string $sessionId, string $data, int $timeDiff): void
184188
{
185-
$timestamp = (new \DateTimeImmutable())->getTimestamp() + $timeDiff;
189+
$time = time() + $timeDiff;
186190

187191
$write = new BulkWrite();
188192
$write->insert([
189193
'_id' => $sessionId,
190-
'data' => new Binary($data, Binary::TYPE_OLD_BINARY),
191-
'time' => new UTCDateTime($timestamp * 1000),
192-
'expires_at' => new UTCDateTime(($timestamp + (int) \ini_get('session.gc_maxlifetime')) * 1000),
194+
'data' => new Binary($data),
195+
'time' => new UTCDateTime($time * 1000),
196+
'expires_at' => new UTCDateTime(($time + (int) \ini_get('session.gc_maxlifetime')) * 1000),
193197
]);
194198

195-
$this->manager->executeBulkWrite('sf-test.session-test', $write);
199+
$this->manager->executeBulkWrite(self::DABASE_NAME.'.'.self::COLLECTION_NAME, $write);
196200
}
197201
}

src/Symfony/Component/Lock/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ CHANGELOG
44
6.4
55
---
66

7-
* `MongoDbStore` can be used with the mongodb extension only
7+
* Make `MongoDbStore` instantiable with the mongodb extension directly
88

99
6.3
1010
---

src/Symfony/Component/Lock/Store/MongoDbStore.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ public function delete(Key $key)
279279
{
280280
$write = new BulkWrite();
281281
$write->delete(
282-
[ // filter
282+
[
283283
'_id' => (string) $key,
284284
'token' => $this->getUniqueToken($key),
285285
],
@@ -292,14 +292,14 @@ public function delete(Key $key)
292292
public function exists(Key $key): bool
293293
{
294294
$cursor = $this->manager->executeQuery($this->namespace, new Query(
295-
[ // filter
295+
[
296296
'_id' => (string) $key,
297297
'token' => $this->getUniqueToken($key),
298298
'expires_at' => [
299299
'$gt' => $this->createMongoDateTime(microtime(true)),
300300
],
301301
],
302-
[ // options
302+
[
303303
'limit' => 1,
304304
'projection' => ['_id' => 1],
305305
]
@@ -320,7 +320,7 @@ private function upsert(Key $key, float $ttl): void
320320

321321
$write = new BulkWrite();
322322
$write->update(
323-
[ // filter
323+
[
324324
'_id' => (string) $key,
325325
'$or' => [
326326
[
@@ -333,14 +333,14 @@ private function upsert(Key $key, float $ttl): void
333333
],
334334
],
335335
],
336-
[ // update
336+
[
337337
'$set' => [
338338
'_id' => (string) $key,
339339
'token' => $token,
340340
'expires_at' => $this->createMongoDateTime($now + $ttl),
341341
],
342342
],
343-
[ // options
343+
[
344344
'upsert' => true,
345345
]
346346
);

0 commit comments

Comments
 (0)
0