2019-12-10 00:44:56 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Friendica\Factory;
|
|
|
|
|
|
|
|
use Friendica\App;
|
|
|
|
use Friendica\Core\Cache\Cache;
|
|
|
|
use Friendica\Core\Cache\ICache;
|
|
|
|
use Friendica\Core\Config\Configuration;
|
2019-12-10 21:52:23 +01:00
|
|
|
use Friendica\Core\Session;
|
2019-12-10 00:44:56 +01:00
|
|
|
use Friendica\Core\System;
|
|
|
|
use Friendica\Database\Database;
|
|
|
|
use Friendica\Model\User\Cookie;
|
|
|
|
use Friendica\Util\Profiler;
|
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
|
2019-12-10 00:50:05 +01:00
|
|
|
/**
|
|
|
|
* Factory for creating a valid Session for this run
|
|
|
|
*/
|
2019-12-10 00:44:56 +01:00
|
|
|
class SessionFactory
|
|
|
|
{
|
|
|
|
/** @var string The plain, PHP internal session management */
|
|
|
|
const INTERNAL = 'native';
|
|
|
|
/** @var string Using the database for session management */
|
|
|
|
const DATABASE = 'database';
|
|
|
|
/** @var string Using the cache for session management */
|
|
|
|
const CACHE = 'cache';
|
|
|
|
/** @var string A temporary cached session */
|
|
|
|
const MEMORY = 'memory';
|
|
|
|
/** @var string The default type for Session management in case of no config */
|
|
|
|
const DEFAULT = self::DATABASE;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param App\Mode $mode
|
|
|
|
* @param Configuration $config
|
|
|
|
* @param Cookie $cookie
|
|
|
|
* @param Database $dba
|
|
|
|
* @param ICache $cache
|
|
|
|
* @param LoggerInterface $logger
|
|
|
|
* @param array $server
|
|
|
|
*
|
2019-12-10 21:52:23 +01:00
|
|
|
* @return Session\ISession
|
2019-12-10 00:44:56 +01:00
|
|
|
*/
|
|
|
|
public function createSession(App\Mode $mode, Configuration $config, Cookie $cookie, Database $dba, ICache $cache, LoggerInterface $logger, Profiler $profiler, array $server = [])
|
|
|
|
{
|
|
|
|
$stamp1 = microtime(true);
|
|
|
|
$session = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
if ($mode->isInstall() || $mode->isBackend()) {
|
2019-12-10 21:52:23 +01:00
|
|
|
$session = new Session\Memory();
|
2019-12-10 00:44:56 +01:00
|
|
|
} else {
|
|
|
|
$session_handler = $config->get('system', 'session_handler', self::DEFAULT);
|
|
|
|
|
|
|
|
switch ($session_handler) {
|
|
|
|
case self::INTERNAL:
|
2019-12-10 21:52:23 +01:00
|
|
|
$session = new Session\Native($config, $cookie);
|
2019-12-10 00:44:56 +01:00
|
|
|
break;
|
|
|
|
case self::DATABASE:
|
|
|
|
default:
|
2019-12-10 21:52:23 +01:00
|
|
|
$session = new Session\Database($config, $cookie, $dba, $logger, $server);
|
2019-12-10 00:44:56 +01:00
|
|
|
break;
|
|
|
|
case self::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') === Cache::TYPE_DATABASE) {
|
2019-12-10 21:52:23 +01:00
|
|
|
$session = new Session\Database($config, $cookie, $dba, $logger, $server);
|
2019-12-10 00:44:56 +01:00
|
|
|
} else {
|
2019-12-10 21:52:23 +01:00
|
|
|
$session = new Session\Cache($config, $cookie, $cache, $logger, $server);
|
2019-12-10 00:44:56 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
$profiler->saveTimestamp($stamp1, 'parser', System::callstack());
|
|
|
|
return $session;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|