Refactor Session factory

This commit is contained in:
Art4 2024-11-19 07:33:07 +00:00
commit 2b81529844

View file

@ -13,11 +13,14 @@ use Friendica\Core\Cache\Factory\Cache;
use Friendica\Core\Cache\Type\DatabaseCache; use Friendica\Core\Cache\Type\DatabaseCache;
use Friendica\Core\Config\Capability\IManageConfigValues; use Friendica\Core\Config\Capability\IManageConfigValues;
use Friendica\Core\Session\Capability\IHandleSessions; use Friendica\Core\Session\Capability\IHandleSessions;
use Friendica\Core\Session\Type; use Friendica\Core\Session\Handler\Cache as CacheHandler;
use Friendica\Core\Session\Handler; use Friendica\Core\Session\Handler\Database as DatabaseHandler;
use Friendica\Core\Session\Type\Memory;
use Friendica\Core\Session\Type\Native;
use Friendica\Database\Database; use Friendica\Database\Database;
use Friendica\Util\Profiler; use Friendica\Util\Profiler;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Throwable;
/** /**
* Factory for creating a valid Session for this run * Factory for creating a valid Session for this run
@ -49,33 +52,36 @@ class Session
$profiler->startRecording('session'); $profiler->startRecording('session');
$session_handler = $config->get('system', 'session_handler', self::HANDLER_DEFAULT); $session_handler = $config->get('system', 'session_handler', self::HANDLER_DEFAULT);
try {
if ($mode->isInstall() || $mode->isBackend()) { if ($mode->isInstall() || $mode->isBackend()) {
$session = new Type\Memory(); $session = new Memory();
} else { $profiler->stopRecording();
return $session;
}
try {
switch ($session_handler) { switch ($session_handler) {
case self::HANDLER_DATABASE: case self::HANDLER_DATABASE:
$handler = new Handler\Database($dba, $logger, $server); $handler = new DatabaseHandler($dba, $logger, $server);
break; break;
case self::HANDLER_CACHE: case self::HANDLER_CACHE:
$cache = $cacheFactory->createDistributed(); $cache = $cacheFactory->createDistributed();
// In case we're using the db as cache driver, use the native db session, not the 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') === DatabaseCache::NAME) { if ($config->get('system', 'cache_driver') === DatabaseCache::NAME) {
$handler = new Handler\Database($dba, $logger, $server); $handler = new DatabaseHandler($dba, $logger, $server);
} else { } else {
$handler = new Handler\Cache($cache, $logger); $handler = new CacheHandler($cache, $logger);
} }
break; break;
default: default:
$handler = null; $handler = null;
} }
$session = new Type\Native($baseURL, $handler); $session = new Native($baseURL, $handler);
}
} catch (\Throwable $e) { } catch (Throwable $e) {
$logger->notice('Unable to create session', ['mode' => $mode, 'session_handler' => $session_handler, 'exception' => $e]); $logger->notice('Unable to create session', ['mode' => $mode, 'session_handler' => $session_handler, 'exception' => $e]);
$session = new Type\Memory(); $session = new Memory();
} finally { } finally {
$profiler->stopRecording(); $profiler->stopRecording();
return $session; return $session;