redis = new Predis($configuration); } /** * @param Predis $redis */ public function setCurrentRedis($redis): void { $this->redis = $redis; } /** * @param int $lifetime */ public function setLifetime(int $lifetime): void { $this->lifetime = $lifetime; } /** * @param string $savePath * @param string $sessionName * * @return bool */ public function open($savePath, $sessionName): bool { return true; } /** * @return bool */ public function close(): bool { return true; } /** * @param string $sessionId * * @return string */ public function read($sessionId): string { return (string) $this->redis->get($sessionId); } /** * @param string $sessionId * @param string $data * * @return bool */ public function write($sessionId, $data): bool { $this->redis->set($sessionId, $data); $this->redis->expireat($sessionId, \time() + $this->lifetime); return true; } /** * @param string $sessionId * * @return bool */ public function destroy($sessionId): bool { $this->redis->del([$sessionId]); return true; } /** * @param int $lifetime * * @return bool */ public function gc($lifetime): bool { return true; } /** * Checks format and id exists, if not session_id will be regenerate. * * @param string $key * * @return bool */ public function validateId($key): bool { if (\preg_match('/^[a-zA-Z0-9-]{127}+$/', $key) !== 1) { return false; } $exist = $this->redis->exists($key); return $exist === 1; } /** * Updates the timestamp of a session when its data didn't change. * * @param string $sessionId * @param string $sessionData * * @return bool */ public function updateTimestamp($sessionId, $sessionData): bool { return $this->write($sessionId, $sessionData); } /** * @throws \Exception * * @return string */ public function create_sid(): string { $string = ''; $caracters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-'; $countCaracters = 62; for ($i = 0; $i < 127; ++$i) { $string .= $caracters[\random_int(0, $countCaracters)]; } $exist = $this->redis->exists($string); if ($exist !== 0) { return $this->create_sid(); } return $string; } }