mirror of
https://github.com/friendica/friendica
synced 2025-04-26 13:50:11 +00:00
Refactoring Core class structures ...
This commit is contained in:
parent
57b4c008cb
commit
b216317477
130 changed files with 1625 additions and 1397 deletions
|
@ -21,8 +21,10 @@
|
|||
|
||||
namespace Friendica\Core\Session\Handler;
|
||||
|
||||
use Friendica\Core\Cache\ICache;
|
||||
use Friendica\Core\Cache\Capability\ICanCache;
|
||||
use Friendica\Core\Cache\Exception\CachePersistenceException;
|
||||
use Friendica\Core\Session;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use SessionHandlerInterface;
|
||||
|
||||
/**
|
||||
|
@ -30,29 +32,37 @@ use SessionHandlerInterface;
|
|||
*/
|
||||
class Cache implements SessionHandlerInterface
|
||||
{
|
||||
/** @var ICache */
|
||||
/** @var ICanCache */
|
||||
private $cache;
|
||||
/** @var LoggerInterface */
|
||||
private $logger;
|
||||
|
||||
public function __construct(ICache $cache)
|
||||
public function __construct(ICanCache $cache, LoggerInterface $logger)
|
||||
{
|
||||
$this->cache = $cache;
|
||||
$this->cache = $cache;
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
public function open($save_path, $session_name)
|
||||
public function open($path, $name): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function read($session_id)
|
||||
public function read($id)
|
||||
{
|
||||
if (empty($session_id)) {
|
||||
if (empty($id)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$data = $this->cache->get('session:' . $session_id);
|
||||
if (!empty($data)) {
|
||||
Session::$exists = true;
|
||||
return $data;
|
||||
try {
|
||||
$data = $this->cache->get('session:' . $id);
|
||||
if (!empty($data)) {
|
||||
Session::$exists = true;
|
||||
return $data;
|
||||
}
|
||||
} catch (CachePersistenceException $exception) {
|
||||
$this->logger->warning('Cannot read session.'. ['id' => $id, 'exception' => $exception]);
|
||||
return '';
|
||||
}
|
||||
|
||||
return '';
|
||||
|
@ -65,36 +75,45 @@ class Cache implements SessionHandlerInterface
|
|||
* on the case. Uses the Session::expire for existing session, 5 minutes
|
||||
* for newly created session.
|
||||
*
|
||||
* @param string $session_id Session ID with format: [a-z0-9]{26}
|
||||
* @param string $session_data Serialized session data
|
||||
* @param string $id Session ID with format: [a-z0-9]{26}
|
||||
* @param string $data Serialized session data
|
||||
*
|
||||
* @return boolean Returns false if parameters are missing, true otherwise
|
||||
* @throws \Exception
|
||||
* @return bool Returns false if parameters are missing, true otherwise
|
||||
*/
|
||||
public function write($session_id, $session_data)
|
||||
public function write($id, $data): bool
|
||||
{
|
||||
if (!$session_id) {
|
||||
if (!$id) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!$session_data) {
|
||||
return $this->destroy($session_id);
|
||||
if (!$data) {
|
||||
return $this->destroy($id);
|
||||
}
|
||||
|
||||
return $this->cache->set('session:' . $session_id, $session_data, Session::$expire);
|
||||
try {
|
||||
return $this->cache->set('session:' . $id, $data, Session::$expire);
|
||||
} catch (CachePersistenceException $exception) {
|
||||
$this->logger->warning('Cannot write session', ['id' => $id, 'exception' => $exception]);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public function close()
|
||||
public function close(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function destroy($id)
|
||||
public function destroy($id): bool
|
||||
{
|
||||
return $this->cache->delete('session:' . $id);
|
||||
try {
|
||||
return $this->cache->delete('session:' . $id);
|
||||
} catch (CachePersistenceException $exception) {
|
||||
$this->logger->warning('Cannot destroy session', ['id' => $id, 'exception' => $exception]);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public function gc($maxlifetime)
|
||||
public function gc($max_lifetime): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -52,24 +52,29 @@ class Database implements SessionHandlerInterface
|
|||
$this->server = $server;
|
||||
}
|
||||
|
||||
public function open($save_path, $session_name)
|
||||
public function open($path, $name): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function read($session_id)
|
||||
public function read($id)
|
||||
{
|
||||
if (empty($session_id)) {
|
||||
if (empty($id)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$session = $this->dba->selectFirst('session', ['data'], ['sid' => $session_id]);
|
||||
if ($this->dba->isResult($session)) {
|
||||
Session::$exists = true;
|
||||
return $session['data'];
|
||||
try {
|
||||
$session = $this->dba->selectFirst('session', ['data'], ['sid' => $id]);
|
||||
if ($this->dba->isResult($session)) {
|
||||
Session::$exists = true;
|
||||
return $session['data'];
|
||||
}
|
||||
} catch (\Exception $exception) {
|
||||
$this->logger->warning('Cannot read session.'. ['id' => $id, 'exception' => $exception]);
|
||||
return '';
|
||||
}
|
||||
|
||||
$this->logger->notice('no data for session', ['session_id' => $session_id, 'uri' => $this->server['REQUEST_URI'] ?? '']);
|
||||
$this->logger->notice('no data for session', ['session_id' => $id, 'uri' => $this->server['REQUEST_URI'] ?? '']);
|
||||
|
||||
return '';
|
||||
}
|
||||
|
@ -81,49 +86,63 @@ class Database implements SessionHandlerInterface
|
|||
* on the case. Uses the Session::expire global for existing session, 5 minutes
|
||||
* for newly created session.
|
||||
*
|
||||
* @param string $session_id Session ID with format: [a-z0-9]{26}
|
||||
* @param string $session_data Serialized session data
|
||||
* @param string $id Session ID with format: [a-z0-9]{26}
|
||||
* @param string $data Serialized session data
|
||||
*
|
||||
* @return boolean Returns false if parameters are missing, true otherwise
|
||||
* @throws \Exception
|
||||
* @return bool Returns false if parameters are missing, true otherwise
|
||||
*/
|
||||
public function write($session_id, $session_data)
|
||||
public function write($id, $data): bool
|
||||
{
|
||||
if (!$session_id) {
|
||||
if (!$id) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!$session_data) {
|
||||
return $this->destroy($session_id);
|
||||
if (!$data) {
|
||||
return $this->destroy($id);
|
||||
}
|
||||
|
||||
$expire = time() + Session::$expire;
|
||||
$default_expire = time() + 300;
|
||||
|
||||
if (Session::$exists) {
|
||||
$fields = ['data' => $session_data, 'expire' => $expire];
|
||||
$condition = ["`sid` = ? AND (`data` != ? OR `expire` != ?)", $session_id, $session_data, $expire];
|
||||
$this->dba->update('session', $fields, $condition);
|
||||
} else {
|
||||
$fields = ['sid' => $session_id, 'expire' => $default_expire, 'data' => $session_data];
|
||||
$this->dba->insert('session', $fields);
|
||||
try {
|
||||
if (Session::$exists) {
|
||||
$fields = ['data' => $data, 'expire' => $expire];
|
||||
$condition = ["`sid` = ? AND (`data` != ? OR `expire` != ?)", $id, $data, $expire];
|
||||
$this->dba->update('session', $fields, $condition);
|
||||
} else {
|
||||
$fields = ['sid' => $id, 'expire' => $default_expire, 'data' => $data];
|
||||
$this->dba->insert('session', $fields);
|
||||
}
|
||||
} catch (\Exception $exception) {
|
||||
$this->logger->warning('Cannot write session.'. ['id' => $id, 'exception' => $exception]);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function close()
|
||||
public function close(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function destroy($id)
|
||||
public function destroy($id): bool
|
||||
{
|
||||
return $this->dba->delete('session', ['sid' => $id]);
|
||||
try {
|
||||
return $this->dba->delete('session', ['sid' => $id]);
|
||||
} catch (\Exception $exception) {
|
||||
$this->logger->warning('Cannot destroy session.'. ['id' => $id, 'exception' => $exception]);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public function gc($maxlifetime)
|
||||
public function gc($max_lifetime): bool
|
||||
{
|
||||
return $this->dba->delete('session', ["`expire` < ?", time()]);
|
||||
try {
|
||||
return $this->dba->delete('session', ["`expire` < ?", time()]);
|
||||
} catch (\Exception $exception) {
|
||||
$this->logger->warning('Cannot use garbage collector.'. ['exception' => $exception]);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue