Refactoring Core class structures ...

This commit is contained in:
Philipp 2021-10-26 21:44:29 +02:00
parent 57b4c008cb
commit b216317477
No known key found for this signature in database
GPG key ID: 24A7501396EB5432
130 changed files with 1625 additions and 1397 deletions

View file

@ -19,19 +19,19 @@
*
*/
namespace Friendica\Core\Session;
namespace Friendica\Core\Session\Capability;
/**
* Contains all global supported Session methods
*/
interface ISession
interface IHandleSessions
{
/**
* Start the current session
*
* @return self The own Session instance
*/
public function start();
public function start(): IHandleSessions;
/**
* Checks if the key exists in this session
@ -40,7 +40,7 @@ interface ISession
*
* @return boolean True, if it exists
*/
public function exists(string $name);
public function exists(string $name): bool;
/**
* Retrieves a key from the session super global or the defaults if the key is missing or the value is falsy.

View file

@ -22,10 +22,12 @@
namespace Friendica\Core\Session\Factory;
use Friendica\App;
use Friendica\Core\Cache\ICache;
use Friendica\Core\Cache\Enum\Type;
use Friendica\Core\Config\IConfig;
use Friendica\Core\Session;
use Friendica\Core\Cache\Capability\ICanCache;
use Friendica\Core\Cache\Enum;
use Friendica\Core\Config\Capability\IManageConfigValues;
use Friendica\Core\Session\Capability\IHandleSessions;
use Friendica\Core\Session\Type;
use Friendica\Core\Session\Handler;
use Friendica\Database\Database;
use Friendica\Util\Profiler;
use Psr\Log\LoggerInterface;
@ -33,7 +35,7 @@ use Psr\Log\LoggerInterface;
/**
* Factory for creating a valid Session for this run
*/
class SessionFactory
class Session
{
/** @var string The plain, PHP internal session management */
const HANDLER_NATIVE = 'native';
@ -45,43 +47,44 @@ class SessionFactory
const HANDLER_DEFAULT = self::HANDLER_DATABASE;
/**
* @param App\Mode $mode
* @param App\BaseURL $baseURL
* @param IConfig $config
* @param Database $dba
* @param ICache $cache
* @param LoggerInterface $logger
* @param array $server
* @param App\Mode $mode
* @param App\BaseURL $baseURL
* @param IManageConfigValues $config
* @param Database $dba
* @param ICanCache $cache
* @param LoggerInterface $logger
* @param Profiler $profiler
* @param array $server
*
* @return Session\ISession
* @return IHandleSessions
*/
public function createSession(App\Mode $mode, App\BaseURL $baseURL, IConfig $config, Database $dba, ICache $cache, LoggerInterface $logger, Profiler $profiler, array $server = [])
public function createSession(App\Mode $mode, App\BaseURL $baseURL, IManageConfigValues $config, Database $dba, ICanCache $cache, LoggerInterface $logger, Profiler $profiler, array $server = [])
{
$profiler->startRecording('session');
$session = null;
try {
if ($mode->isInstall() || $mode->isBackend()) {
$session = new Session\Type\Memory();
$session = new Type\Memory();
} else {
$session_handler = $config->get('system', 'session_handler', self::HANDLER_DEFAULT);
$handler = null;
$handler = null;
switch ($session_handler) {
case self::HANDLER_DATABASE:
$handler = new Session\Handler\Database($dba, $logger, $server);
$handler = new Handler\Database($dba, $logger, $server);
break;
case self::HANDLER_CACHE:
// In case we're using the db as cache driver, use the native db session, not the cache
if ($config->get('system', 'cache_driver') === Type::DATABASE) {
$handler = new Session\Handler\Database($dba, $logger, $server);
if ($config->get('system', 'cache_driver') === Enum\Type::DATABASE) {
$handler = new Handler\Database($dba, $logger, $server);
} else {
$handler = new Session\Handler\Cache($cache);
$handler = new Handler\Cache($cache, $logger);
}
break;
}
$session = new Session\Type\Native($baseURL, $handler);
$session = new Type\Native($baseURL, $handler);
}
} finally {
$profiler->stopRecording();

View file

@ -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;
}

View file

@ -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;
}
}
}

View file

@ -21,15 +21,17 @@
namespace Friendica\Core\Session\Type;
use Friendica\Core\Session\Capability\IHandleSessions;
/**
* Contains the base methods for $_SESSION interaction
*/
class AbstractSession
class AbstractSession implements IHandleSessions
{
/**
* {@inheritDoc}
*/
public function start()
public function start(): IHandleSessions
{
return $this;
}
@ -37,7 +39,7 @@ class AbstractSession
/**
* {@inheritDoc}}
*/
public function exists(string $name)
public function exists(string $name): bool
{
return isset($_SESSION[$name]);
}

View file

@ -21,14 +21,14 @@
namespace Friendica\Core\Session\Type;
use Friendica\Core\Session\ISession;
use Friendica\Core\Session\Capability\IHandleSessions;
/**
* Usable for backend processes (daemon/worker) and testing
*
* @todo after replacing the last direct $_SESSION call, use a internal array instead of the global variable
*/
class Memory extends AbstractSession implements ISession
class Memory extends AbstractSession implements IHandleSessions
{
public function __construct()
{

View file

@ -22,14 +22,14 @@
namespace Friendica\Core\Session\Type;
use Friendica\App;
use Friendica\Core\Session\ISession;
use Friendica\Core\Session\Capability\IHandleSessions;
use Friendica\Model\User\Cookie;
use SessionHandlerInterface;
/**
* The native Session class which uses the PHP internal Session functions
*/
class Native extends AbstractSession implements ISession
class Native extends AbstractSession implements IHandleSessions
{
public function __construct(App\BaseURL $baseURL, SessionHandlerInterface $handler = null)
{
@ -49,7 +49,7 @@ class Native extends AbstractSession implements ISession
/**
* {@inheritDoc}
*/
public function start()
public function start(): IHandleSessions
{
session_start();
return $this;