package and depencies
This commit is contained in:
@@ -26,17 +26,10 @@ use MongoDB\Collection;
|
||||
*/
|
||||
class MongoDbSessionHandler extends AbstractSessionHandler
|
||||
{
|
||||
private $mongo;
|
||||
|
||||
/**
|
||||
* @var Collection
|
||||
*/
|
||||
private $collection;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $options;
|
||||
private Client $mongo;
|
||||
private Collection $collection;
|
||||
private array $options;
|
||||
private int|\Closure|null $ttl;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
@@ -47,7 +40,8 @@ class MongoDbSessionHandler extends AbstractSessionHandler
|
||||
* * id_field: The field name for storing the session id [default: _id]
|
||||
* * data_field: The field name for storing the session data [default: data]
|
||||
* * time_field: The field name for storing the timestamp [default: time]
|
||||
* * expiry_field: The field name for storing the expiry-timestamp [default: expires_at].
|
||||
* * expiry_field: The field name for storing the expiry-timestamp [default: expires_at]
|
||||
* * ttl: The time to live in seconds.
|
||||
*
|
||||
* It is strongly recommended to put an index on the `expiry_field` for
|
||||
* garbage-collection. Alternatively it's possible to automatically expire
|
||||
@@ -82,21 +76,15 @@ class MongoDbSessionHandler extends AbstractSessionHandler
|
||||
'time_field' => 'time',
|
||||
'expiry_field' => 'expires_at',
|
||||
], $options);
|
||||
$this->ttl = $this->options['ttl'] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function close()
|
||||
public function close(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function doDestroy(string $sessionId)
|
||||
protected function doDestroy(string $sessionId): bool
|
||||
{
|
||||
$this->getCollection()->deleteOne([
|
||||
$this->options['id_field'] => $sessionId,
|
||||
@@ -105,23 +93,17 @@ class MongoDbSessionHandler extends AbstractSessionHandler
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int|false
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function gc($maxlifetime)
|
||||
public function gc(int $maxlifetime): int|false
|
||||
{
|
||||
return $this->getCollection()->deleteMany([
|
||||
$this->options['expiry_field'] => ['$lt' => new UTCDateTime()],
|
||||
])->getDeletedCount();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function doWrite(string $sessionId, string $data)
|
||||
protected function doWrite(string $sessionId, string $data): bool
|
||||
{
|
||||
$expiry = new UTCDateTime((time() + (int) \ini_get('session.gc_maxlifetime')) * 1000);
|
||||
$ttl = ($this->ttl instanceof \Closure ? ($this->ttl)() : $this->ttl) ?? \ini_get('session.gc_maxlifetime');
|
||||
$expiry = new UTCDateTime((time() + (int) $ttl) * 1000);
|
||||
|
||||
$fields = [
|
||||
$this->options['time_field'] => new UTCDateTime(),
|
||||
@@ -138,13 +120,10 @@ class MongoDbSessionHandler extends AbstractSessionHandler
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function updateTimestamp($sessionId, $data)
|
||||
public function updateTimestamp(string $sessionId, string $data): bool
|
||||
{
|
||||
$expiry = new UTCDateTime((time() + (int) \ini_get('session.gc_maxlifetime')) * 1000);
|
||||
$ttl = ($this->ttl instanceof \Closure ? ($this->ttl)() : $this->ttl) ?? \ini_get('session.gc_maxlifetime');
|
||||
$expiry = new UTCDateTime((time() + (int) $ttl) * 1000);
|
||||
|
||||
$this->getCollection()->updateOne(
|
||||
[$this->options['id_field'] => $sessionId],
|
||||
@@ -157,10 +136,7 @@ class MongoDbSessionHandler extends AbstractSessionHandler
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function doRead(string $sessionId)
|
||||
protected function doRead(string $sessionId): string
|
||||
{
|
||||
$dbData = $this->getCollection()->findOne([
|
||||
$this->options['id_field'] => $sessionId,
|
||||
@@ -176,17 +152,10 @@ class MongoDbSessionHandler extends AbstractSessionHandler
|
||||
|
||||
private function getCollection(): Collection
|
||||
{
|
||||
if (null === $this->collection) {
|
||||
$this->collection = $this->mongo->selectCollection($this->options['database'], $this->options['collection']);
|
||||
}
|
||||
|
||||
return $this->collection;
|
||||
return $this->collection ??= $this->mongo->selectCollection($this->options['database'], $this->options['collection']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Client
|
||||
*/
|
||||
protected function getMongo()
|
||||
protected function getMongo(): Client
|
||||
{
|
||||
return $this->mongo;
|
||||
}
|
||||
|
Reference in New Issue
Block a user