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
183 lines
3.8 KiB
11 months ago
|
<?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
|
||
|
*/
|
||
|
|
||
10 months ago
|
namespace Phacil\Framework\Session\Handlers;
|
||
11 months ago
|
|
||
|
use Cm\RedisSession\Handler\ConfigInterface;
|
||
|
use Cm\RedisSession\Handler\LoggerInterface;
|
||
|
use Cm\RedisSession\ConnectionFailedException;
|
||
|
use Cm\RedisSession\ConcurrentConnectionsExceededException;
|
||
|
use Phacil\Framework\Exception;
|
||
|
|
||
10 months ago
|
class Redis implements \Phacil\Framework\Session\Api\HandlerInterface {
|
||
11 months ago
|
|
||
|
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];
|
||
|
}
|
||
|
|
||
10 months ago
|
/** {@inheritdoc} */
|
||
11 months ago
|
function setName($name){
|
||
|
$this->name = $name;
|
||
10 months ago
|
return $this;
|
||
11 months ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Open session
|
||
|
*
|
||
|
* @param string $savePath ignored
|
||
|
* @param string $sessionName ignored
|
||
|
* @return bool
|
||
|
* @throws Exception
|
||
|
*/
|
||
11 months ago
|
#[\ReturnTypeWillChange]
|
||
11 months ago
|
public function open($savePath, $sessionName)
|
||
|
{
|
||
11 months ago
|
return (bool)$this->getConnection()->open($savePath, $sessionName);
|
||
11 months ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Fetch session data
|
||
|
*
|
||
|
* @param string $sessionId
|
||
|
* @return string
|
||
|
* @throws ConcurrentConnectionsExceededException
|
||
|
* @throws Exception
|
||
|
*/
|
||
11 months ago
|
#[\ReturnTypeWillChange]
|
||
11 months ago
|
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]
|
||
11 months ago
|
public function write($sessionId, $sessionData)
|
||
|
{
|
||
|
try {
|
||
|
//$this->getConnection()->setName($this->name);
|
||
|
$cha = $this->getConnection()->write($sessionId, $sessionData);
|
||
11 months ago
|
return (bool) $cha;
|
||
11 months ago
|
} catch (ConcurrentConnectionsExceededException $e) {
|
||
|
throw new Exception($e->getMessage(), 1);
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Destroy session
|
||
|
*
|
||
|
* @param string $sessionId
|
||
|
* @return boolean
|
||
|
* @throws Exception
|
||
|
*/
|
||
11 months ago
|
#[\ReturnTypeWillChange]
|
||
11 months ago
|
public function destroy($sessionId)
|
||
|
{
|
||
11 months ago
|
return (bool) $this->getConnection()->destroy($sessionId);
|
||
11 months ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Overridden to prevent calling getLifeTime at shutdown
|
||
|
*
|
||
|
* @return bool
|
||
|
* @throws Exception
|
||
|
*/
|
||
11 months ago
|
#[\ReturnTypeWillChange]
|
||
11 months ago
|
public function close()
|
||
|
{
|
||
11 months ago
|
return (bool)$this->getConnection()->close();
|
||
11 months ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Garbage collection
|
||
|
*
|
||
|
* @param int $maxLifeTime ignored
|
||
|
* @return boolean
|
||
|
* @throws Exception
|
||
|
* @SuppressWarnings(PHPMD.ShortMethodName)
|
||
|
*/
|
||
11 months ago
|
#[\ReturnTypeWillChange]
|
||
11 months ago
|
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();
|
||
|
}
|
||
|
}
|