2019-12-09 18:44:56 -05: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 15:52:23 -05:00
|
|
|
use Friendica\Core\Session;
|
2019-12-09 18:44:56 -05:00
|
|
|
use Friendica\Core\System;
|
|
|
|
use Friendica\Database\Database;
|
|
|
|
use Friendica\Util\Profiler;
|
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
|
2019-12-09 18:50:05 -05:00
|
|
|
/**
|
|
|
|
* Factory for creating a valid Session for this run
|
|
|
|
*/
|
2019-12-09 18:44:56 -05:00
|
|
|
class SessionFactory
|
|
|
|
{
|
|
|
|
/** @var string The plain, PHP internal session management */
|
2019-12-11 14:30:31 -05:00
|
|
|
const HANDLER_NATIVE = 'native';
|
2019-12-09 18:44:56 -05:00
|
|
|
/** @var string Using the database for session management */
|
2019-12-11 14:30:31 -05:00
|
|
|
const HANDLER_DATABASE = 'database';
|
2019-12-09 18:44:56 -05:00
|
|
|
/** @var string Using the cache for session management */
|
2019-12-11 14:30:31 -05:00
|
|
|
const HANDLER_CACHE = 'cache';
|
|
|
|
|
|
|
|
const HANDLER_DEFAULT = self::HANDLER_DATABASE;
|
2019-12-09 18:44:56 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param App\Mode $mode
|
2019-12-11 14:30:31 -05:00
|
|
|
* @param App\BaseURL $baseURL
|
2019-12-09 18:44:56 -05:00
|
|
|
* @param Configuration $config
|
|
|
|
* @param Database $dba
|
|
|
|
* @param ICache $cache
|
|
|
|
* @param LoggerInterface $logger
|
|
|
|
* @param array $server
|
|
|
|
*
|
2019-12-10 15:52:23 -05:00
|
|
|
* @return Session\ISession
|
2019-12-09 18:44:56 -05:00
|
|
|
*/
|
2019-12-28 18:03:58 -05:00
|
|
|
public function createSession(App\Mode $mode, App\BaseURL $baseURL, Configuration $config, Database $dba, ICache $cache, LoggerInterface $logger, Profiler $profiler, array $server = [])
|
2019-12-09 18:44:56 -05:00
|
|
|
{
|
|
|
|
$stamp1 = microtime(true);
|
|
|
|
$session = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
if ($mode->isInstall() || $mode->isBackend()) {
|
2019-12-28 18:03:58 -05:00
|
|
|
$session = new Session\Memory();
|
2019-12-09 18:44:56 -05:00
|
|
|
} else {
|
2019-12-11 14:30:31 -05:00
|
|
|
$session_handler = $config->get('system', 'session_handler', self::HANDLER_DEFAULT);
|
|
|
|
$handler = null;
|
2019-12-09 18:44:56 -05:00
|
|
|
|
|
|
|
switch ($session_handler) {
|
2019-12-11 14:30:31 -05:00
|
|
|
case self::HANDLER_DATABASE:
|
|
|
|
$handler = new Session\Handler\Database($dba, $logger, $server);
|
2019-12-09 18:44:56 -05:00
|
|
|
break;
|
2019-12-11 14:30:31 -05:00
|
|
|
case self::HANDLER_CACHE:
|
2019-12-09 18:44:56 -05:00
|
|
|
// 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-11 14:30:31 -05:00
|
|
|
$handler = new Session\Handler\Database($dba, $logger, $server);
|
2019-12-09 18:44:56 -05:00
|
|
|
} else {
|
2019-12-11 14:30:31 -05:00
|
|
|
$handler = new Session\Handler\Cache($cache, $logger, $server);
|
2019-12-09 18:44:56 -05:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2019-12-11 14:30:31 -05:00
|
|
|
|
2019-12-28 18:03:58 -05:00
|
|
|
$session = new Session\Native($baseURL, $handler);
|
2019-12-09 18:44:56 -05:00
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
$profiler->saveTimestamp($stamp1, 'parser', System::callstack());
|
|
|
|
return $session;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|