A super easy PHP Framework for web development! https://github.com/exacti/phacil-framework
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

183 lines
3.8 KiB

<?php
/**
* Copyright © 2024 ExacTI Technology Solutions. All rights reserved.
* GPLv3 General License.
* https://exacti.com.br
* Phacil PHP Framework - https://github.com/exacti/phacil-framework
*/
namespace Phacil\Framework\Session\Handlers;
use Cm\RedisSession\Handler\ConfigInterface;
use Cm\RedisSession\Handler\LoggerInterface;
use Cm\RedisSession\ConnectionFailedException;
use Cm\RedisSession\ConcurrentConnectionsExceededException;
use Phacil\Framework\Exception;
class Redis implements \Phacil\Framework\Session\Api\HandlerInterface {
private $savePath;
/**
*
* @var \Cm\RedisSession\Handler[]
*/
private $connection;
/**
*
* @var \Cm\RedisSession\Handler\ConfigInterface
*/
private $config;
/**
*
* @var \Cm\RedisSession\Handler\LoggerInterface
*/
private $logger;
private $name;
/**
* @param ConfigInterface $config
* @param LoggerInterface $logger
* @param Filesystem $filesystem
* @throws Exception
*/
public function __construct(ConfigInterface $config, LoggerInterface $logger)
{
$this->config = $config;
$this->logger = $logger;
}
/**
* Get connection
*
* @return \Cm\RedisSession\Handler
* @throws Exception
*/
private function getConnection()
{
$pid = getmypid();
if (!isset($this->connection[$pid])) {
try {
//$this->connection[$pid] = new \Cm\RedisSession\Handler($this->config, $this->logger);
$this->connection[$pid] = new \Phacil\Framework\Session\Redis\AbstractCM\Handler($this->config, $this->logger);
} catch (ConnectionFailedException $e) {
throw new Exception(($e->getMessage()));
}
}
return $this->connection[$pid];
}
/** {@inheritdoc} */
function setName($name){
$this->name = $name;
return $this;
}
/**
* Open session
*
* @param string $savePath ignored
* @param string $sessionName ignored
* @return bool
* @throws Exception
*/
11 months ago
#[\ReturnTypeWillChange]
public function open($savePath, $sessionName)
{
11 months ago
return (bool)$this->getConnection()->open($savePath, $sessionName);
}
/**
* Fetch session data
*
* @param string $sessionId
* @return string
* @throws ConcurrentConnectionsExceededException
* @throws Exception
*/
11 months ago
#[\ReturnTypeWillChange]
public function read($sessionId)
{
try {
return $this->getConnection()->read($sessionId);
} catch (ConcurrentConnectionsExceededException $e) {
throw new Exception($e->getMessage(), 1);
}
}
/**
* Update session
*
* @param string $sessionId
* @param string $sessionData
* @return boolean
* @throws Exception
*/
11 months ago
#[\ReturnTypeWillChange]
public function write($sessionId, $sessionData)
{
try {
//$this->getConnection()->setName($this->name);
$cha = $this->getConnection()->write($sessionId, $sessionData);
11 months ago
return (bool) $cha;
} catch (ConcurrentConnectionsExceededException $e) {
throw new Exception($e->getMessage(), 1);
}
}
/**
* Destroy session
*
* @param string $sessionId
* @return boolean
* @throws Exception
*/
11 months ago
#[\ReturnTypeWillChange]
public function destroy($sessionId)
{
11 months ago
return (bool) $this->getConnection()->destroy($sessionId);
}
/**
* Overridden to prevent calling getLifeTime at shutdown
*
* @return bool
* @throws Exception
*/
11 months ago
#[\ReturnTypeWillChange]
public function close()
{
11 months ago
return (bool)$this->getConnection()->close();
}
/**
* Garbage collection
*
* @param int $maxLifeTime ignored
* @return boolean
* @throws Exception
* @SuppressWarnings(PHPMD.ShortMethodName)
*/
11 months ago
#[\ReturnTypeWillChange]
public function gc($maxLifeTime)
{
return $this->getConnection()->gc($maxLifeTime);
}
/**
* Get the number of failed lock attempts
*
* @return int
* @throws Exception
*/
public function getFailedLockAttempts()
{
return $this->getConnection()->getFailedLockAttempts();
}
}