* @package generis */ class common_session_php_KeyValueSessionHandler extends ConfigurableService implements common_session_php_SessionHandler { public const OPTION_PERSISTENCE = 'persistence'; public const OPTION_SESSION_TTL = 'session_ttl'; public const KEY_NAMESPACE = 'generis:session:'; /** @var common_persistence_KeyValuePersistence */ private $sessionPersistence; protected function getPersistence() { if (is_null($this->sessionPersistence)) { $this->sessionPersistence = common_persistence_KeyValuePersistence::getPersistence($this->getOption(self::OPTION_PERSISTENCE)); } return $this->sessionPersistence; } /** * (non-PHPdoc) * @see common_session_storage_SessionStorage::open() */ public function open($savePath, $sessionName) { return true; } /** * (non-PHPdoc) * @see common_session_storage_SessionStorage::close() */ public function close() { return true; } /** * (non-PHPdoc) * @see common_session_storage_SessionStorage::read() */ public function read($id) { $session = $this->getPersistence()->get(self::KEY_NAMESPACE . $id); return is_string($session) ? $session : ''; } /** * (non-PHPdoc) * @see common_session_storage_SessionStorage::write() */ public function write($id, $data) { return $this->getPersistence()->set(self::KEY_NAMESPACE . $id, $data, $this->getSessionTtl()); } /** * (non-PHPdoc) * @see common_session_storage_SessionStorage::destroy() */ public function destroy($id) { $this->getPersistence()->del(self::KEY_NAMESPACE . $id); return true; } /** * (non-PHPdoc) * @see common_session_storage_SessionStorage::gc() */ public function gc($maxlifetime) { // //problem here either // solution 1 : do two explicit handlers for each specific persistence (Redis, SQL) // solution 2 : Check if the eprsistence is capable of autonomous garbage // return true; } private function getSessionTtl(): int { return $this->getOption(self::OPTION_SESSION_TTL) ?? (int)ini_get('session.gc_maxlifetime'); } }