|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace MongoDB\Laravel\Session; |
| 13 | + |
| 14 | +use Illuminate\Session\DatabaseSessionHandler; |
| 15 | +use MongoDB\BSON\Binary; |
| 16 | +use MongoDB\BSON\UTCDateTime; |
| 17 | +use MongoDB\Collection; |
| 18 | + |
| 19 | +use function tap; |
| 20 | +use function time; |
| 21 | + |
| 22 | +/** |
| 23 | + * Session handler using the MongoDB driver extension. |
| 24 | + */ |
| 25 | +final class MongoDbSessionHandler extends DatabaseSessionHandler |
| 26 | +{ |
| 27 | + private Collection $collection; |
| 28 | + |
| 29 | + public function close(): bool |
| 30 | + { |
| 31 | + return true; |
| 32 | + } |
| 33 | + |
| 34 | + public function gc($lifetime): int |
| 35 | + { |
| 36 | + $result = $this->getCollection()->deleteMany(['last_activity' => ['$lt' => $this->getUTCDateTime(-$lifetime)]]); |
| 37 | + |
| 38 | + return $result->getDeletedCount() ?? 0; |
| 39 | + } |
| 40 | + |
| 41 | + public function destroy($sessionId): bool |
| 42 | + { |
| 43 | + $this->getCollection()->deleteOne(['_id' => (string) $sessionId]); |
| 44 | + |
| 45 | + return true; |
| 46 | + } |
| 47 | + |
| 48 | + public function read($sessionId): string|false |
| 49 | + { |
| 50 | + $result = $this->getCollection()->findOne( |
| 51 | + ['_id' => (string) $sessionId, 'expires_at' => ['$gte' => $this->getUTCDateTime()]], |
| 52 | + [ |
| 53 | + 'projection' => ['_id' => false, 'payload' => true], |
| 54 | + 'typeMap' => ['root' => 'bson'], |
| 55 | + ], |
| 56 | + ); |
| 57 | + |
| 58 | + return $result ? (string) $result->payload : false; |
| 59 | + } |
| 60 | + |
| 61 | + public function write($sessionId, $data): bool |
| 62 | + { |
| 63 | + $payload = $this->getDefaultPayload($data); |
| 64 | + |
| 65 | + $this->getCollection()->replaceOne( |
| 66 | + ['_id' => (string) $sessionId], |
| 67 | + $payload, |
| 68 | + ['upsert' => true], |
| 69 | + ); |
| 70 | + |
| 71 | + return true; |
| 72 | + } |
| 73 | + |
| 74 | + /** Creates a TTL index that automatically deletes expired objects. */ |
| 75 | + public function createTTLIndex(): void |
| 76 | + { |
| 77 | + $this->collection->createIndex( |
| 78 | + // UTCDateTime field that holds the expiration date |
| 79 | + ['expires_at' => 1], |
| 80 | + // Delay to remove items after expiration |
| 81 | + ['expireAfterSeconds' => 0], |
| 82 | + ); |
| 83 | + } |
| 84 | + |
| 85 | + protected function getDefaultPayload($data): array |
| 86 | + { |
| 87 | + $payload = [ |
| 88 | + 'payload' => new Binary($data), |
| 89 | + 'last_activity' => $this->getUTCDateTime(), |
| 90 | + 'expires_at' => $this->getUTCDateTime($this->minutes * 60), |
| 91 | + ]; |
| 92 | + |
| 93 | + if (! $this->container) { |
| 94 | + return $payload; |
| 95 | + } |
| 96 | + |
| 97 | + return tap($payload, function (&$payload) { |
| 98 | + $this->addUserInformation($payload) |
| 99 | + ->addRequestInformation($payload); |
| 100 | + }); |
| 101 | + } |
| 102 | + |
| 103 | + private function getCollection(): Collection |
| 104 | + { |
| 105 | + return $this->collection ??= $this->connection->getCollection($this->table); |
| 106 | + } |
| 107 | + |
| 108 | + private function getUTCDateTime(int $additionalSeconds = 0): UTCDateTime |
| 109 | + { |
| 110 | + return new UTCDateTime((time() + $additionalSeconds * 60) * 1000); |
| 111 | + } |
| 112 | +} |
0 commit comments