Initializing SessionHandlers with Dependency Injection

This commit is contained in:
Philipp Holzer 2019-12-09 23:09:18 +01:00
parent 94a8a60841
commit 009a8bb939
No known key found for this signature in database
GPG Key ID: D8365C3D36B77D90
4 changed files with 80 additions and 35 deletions

View File

@ -7,12 +7,15 @@ namespace Friendica\Core;
use Friendica\App;
use Friendica\BaseObject;
use Friendica\Core\Cache\ICache;
use Friendica\Core\Session\CacheSessionHandler;
use Friendica\Core\Session\DatabaseSessionHandler;
use Friendica\Database\Database;
use Friendica\Database\DBA;
use Friendica\Model\Contact;
use Friendica\Model\User;
use Friendica\Util\Strings;
use Psr\Log\LoggerInterface;
/**
* High-level Session service class
@ -37,9 +40,17 @@ class Session
$session_handler = Config::get('system', 'session_handler', 'database');
if ($session_handler != 'native') {
if ($session_handler == 'cache' && Config::get('system', 'cache_driver', 'database') != 'database') {
$SessionHandler = new CacheSessionHandler();
$SessionHandler = new CacheSessionHandler(
BaseObject::getClass(ICache::class),
BaseObject::getClass(LoggerInterface::class),
$_SERVER
);
} else {
$SessionHandler = new DatabaseSessionHandler();
$SessionHandler = new DatabaseSessionHandler(
BaseObject::getClass(Database::class),
BaseObject::getClass(LoggerInterface::class),
$_SERVER
);
}
session_set_save_handler($SessionHandler);

View File

@ -2,10 +2,9 @@
namespace Friendica\Core\Session;
use Friendica\BaseObject;
use Friendica\Core\Cache;
use Friendica\Core\Logger;
use Friendica\Core\Cache\ICache;
use Friendica\Core\Session;
use Psr\Log\LoggerInterface;
use SessionHandlerInterface;
/**
@ -13,8 +12,29 @@ use SessionHandlerInterface;
*
* @author Hypolite Petovan <hypolite@mrpetovan.com>
*/
class CacheSessionHandler extends BaseObject implements SessionHandlerInterface
class CacheSessionHandler implements SessionHandlerInterface
{
/** @var ICache */
private $cache;
/** @var LoggerInterface */
private $logger;
/** @var array The $_SERVER array */
private $server;
/**
* CacheSessionHandler constructor.
*
* @param ICache $cache
* @param LoggerInterface $logger
* @param array $server
*/
public function __construct(ICache $cache, LoggerInterface $logger, array $server)
{
$this->cache = $cache;
$this->logger = $logger;
$this->server = $server;
}
public function open($save_path, $session_name)
{
return true;
@ -26,13 +46,13 @@ class CacheSessionHandler extends BaseObject implements SessionHandlerInterface
return '';
}
$data = Cache::get('session:' . $session_id);
$data = $this->cache->get('session:' . $session_id);
if (!empty($data)) {
Session::$exists = true;
return $data;
}
Logger::notice('no data for session', ['session_id' => $session_id, 'uri' => $_SERVER['REQUEST_URI']]);
$this->logger->notice('no data for session', ['session_id' => $session_id, 'uri' => $this->server['REQUEST_URI'] ?? '']);
return '';
}
@ -59,9 +79,7 @@ class CacheSessionHandler extends BaseObject implements SessionHandlerInterface
return true;
}
$return = Cache::set('session:' . $session_id, $session_data, Session::$expire);
return $return;
return $this->cache->set('session:' . $session_id, $session_data, Session::$expire);
}
public function close()
@ -71,9 +89,7 @@ class CacheSessionHandler extends BaseObject implements SessionHandlerInterface
public function destroy($id)
{
$return = Cache::delete('session:' . $id);
return $return;
return $this->cache->delete('session:' . $id);
}
public function gc($maxlifetime)

View File

@ -2,10 +2,9 @@
namespace Friendica\Core\Session;
use Friendica\BaseObject;
use Friendica\Core\Logger;
use Friendica\Core\Session;
use Friendica\Database\DBA;
use Friendica\Database\Database;
use Psr\Log\LoggerInterface;
use SessionHandlerInterface;
/**
@ -13,8 +12,29 @@ use SessionHandlerInterface;
*
* @author Hypolite Petovan <hypolite@mrpetovan.com>
*/
class DatabaseSessionHandler extends BaseObject implements SessionHandlerInterface
class DatabaseSessionHandler implements SessionHandlerInterface
{
/** @var Database */
private $dba;
/** @var LoggerInterface */
private $logger;
/** @var array The $_SERVER variable */
private $server;
/**
* DatabaseSessionHandler constructor.
*
* @param Database $dba
* @param LoggerInterface $logger
* @param array $server
*/
public function __construct(Database $dba, LoggerInterface $logger, array $server)
{
$this->dba = $dba;
$this->logger = $logger;
$this->server = $server;
}
public function open($save_path, $session_name)
{
return true;
@ -26,13 +46,13 @@ class DatabaseSessionHandler extends BaseObject implements SessionHandlerInterfa
return '';
}
$session = DBA::selectFirst('session', ['data'], ['sid' => $session_id]);
if (DBA::isResult($session)) {
$session = $this->dba->selectFirst('session', ['data'], ['sid' => $session_id]);
if ($this->dba->isResult($session)) {
Session::$exists = true;
return $session['data'];
}
Logger::notice('no data for session', ['session_id' => $session_id, 'uri' => $_SERVER['REQUEST_URI']]);
$this->logger->notice('no data for session', ['session_id' => $session_id, 'uri' => $this->server['REQUEST_URI'] ?? '']);
return '';
}
@ -65,10 +85,10 @@ class DatabaseSessionHandler extends BaseObject implements SessionHandlerInterfa
if (Session::$exists) {
$fields = ['data' => $session_data, 'expire' => $expire];
$condition = ["`sid` = ? AND (`data` != ? OR `expire` != ?)", $session_id, $session_data, $expire];
DBA::update('session', $fields, $condition);
$this->dba->update('session', $fields, $condition);
} else {
$fields = ['sid' => $session_id, 'expire' => $default_expire, 'data' => $session_data];
DBA::insert('session', $fields);
$this->dba->insert('session', $fields);
}
return true;
@ -81,13 +101,11 @@ class DatabaseSessionHandler extends BaseObject implements SessionHandlerInterfa
public function destroy($id)
{
DBA::delete('session', ['sid' => $id]);
return true;
return $this->dba->delete('session', ['sid' => $id]);
}
public function gc($maxlifetime)
{
DBA::delete('session', ["`expire` < ?", time()]);
return true;
return $this->dba->delete('session', ["`expire` < ?", time()]);
}
}

View File

@ -18,7 +18,7 @@ class Cookie
const PATH = '/';
/** @var string The domain name of the Friendica cookie */
const DOMAIN = '';
/** @var bool True, if the cookie should only be accessable through HTTP */
/** @var bool True, if the cookie should only be accessible through HTTP */
const HTTPONLY = true;
/** @var string The remote address of this node */
@ -68,10 +68,10 @@ class Cookie
/**
* Set the Friendica cookie for a user
*
* @param int $uid The user id
* @param string $password The user password
* @param string $privateKey The user private key
* @param int|null $seconds optional the seconds
* @param int $uid The user id
* @param string $password The user password
* @param string $privateKey The user private key
* @param int|null $seconds optional the seconds
*
* @return bool
*/
@ -142,9 +142,9 @@ class Cookie
* @link https://php.net/manual/en/function.setcookie.php
*
* @param string $name
* @param string $value [optional]
* @param int $expire [optional]
* @param bool $secure [optional]
* @param string $value [optional]
* @param int $expire [optional]
* @param bool $secure [optional]
*
* @return bool If output exists prior to calling this function,
*