setNewDatabase($configuration); static::$driver = $driver; } /** @throws SessionException */ public static function useCurrentDatabaseDriver(DB $databaseInstance): void { static::throwExceptionIfHasStarted(); $driver = new Database(); $driver->setCurrentDatabase($databaseInstance); static::$driver = $driver; } /** @throws SessionException */ public static function useNewDatabaseEncryptionDriver(array|Configurator $configuration, string $key, ?string $method = null): void { static::throwExceptionIfHasStarted(); $driver = new DatabaseEncryption(); $driver->setNewDatabase($configuration); static::setKeyAndMethod($driver, $key, $method); static::$driver = $driver; } /** @throws SessionException */ public static function useCurrentDatabaseEncryptionDriver(DB $databaseInstance, string $key, ?string $method = null): void { static::throwExceptionIfHasStarted(); $driver = new DatabaseEncryption(); $driver->setCurrentDatabase($databaseInstance); static::setKeyAndMethod($driver, $key, $method); static::$driver = $driver; } /** @throws SessionException */ public static function useNewRedisDriver(array|string $configuration): void { static::throwExceptionIfHasStarted(); $driver = new Redis(); $driver->setNewRedis($configuration); $driver->setLifetime(static::getLifetimeForRedis()); static::$driver = $driver; } /** @throws SessionException */ public static function useCurrentRedisDriver(PredisClient $redisInstance): void { static::throwExceptionIfHasStarted(); $driver = new Redis(); $driver->setCurrentRedis($redisInstance); $driver->setLifetime(static::getLifetimeForRedis()); static::$driver = $driver; } /** @throws SessionException */ public static function useNewRedisEncryptionDriver(array|string $configuration, string $key, ?string $method = null): void { static::throwExceptionIfHasStarted(); $driver = new RedisEncryption(); $driver->setNewRedis($configuration); static::setKeyAndMethod($driver, $key, $method); $driver->setLifetime(static::getLifetimeForRedis()); static::$driver = $driver; } /** @throws SessionException */ public static function useCurrentRedisEncryptionDriver(PredisClient $redisInstance, string $key, ?string $method = null): void { static::throwExceptionIfHasStarted(); $driver = new RedisEncryption(); $driver->setCurrentRedis($redisInstance); static::setKeyAndMethod($driver, $key, $method); $driver->setLifetime(static::getLifetimeForRedis()); static::$driver = $driver; } /** * @param Encryption $driver (use Encryption trait) * * @throws SessionException */ private static function setKeyAndMethod(mixed $driver, string $key, ?string $method): void { $driver->setKey($key); if ($method !== null) { $driver->setMethod($method); } } /** @throws SessionException */ public static function useCustomDriver(\SessionHandlerInterface $customDriver): void { static::throwExceptionIfHasStarted(); static::$driver = $customDriver; } public static function getDriver(): \SessionHandlerInterface { return static::$driver; } public static function setUserIdForDatabase(int $userId): void { if (\method_exists(static::$driver, 'setUserId')) { static::$driver->setUserId($userId); } } public static function setPrefixForFile(string $prefix): void { if (\method_exists(static::$driver, 'setPrefix')) { static::$driver->setPrefix($prefix); } } public static function deleteUserSessionsInDatabase(int $userID): void { if (\method_exists(static::$driver, 'deleteUserSessions')) { static::$driver->deleteUserSessions($userID); } } public static function deleteAnonymousSessionsInDatabase(): void { if (\method_exists(static::$driver, 'deleteAnonymousSessions')) { static::$driver->deleteAnonymousSessions(); } } }