2019-12-09 23:44:56 +00:00
|
|
|
<?php
|
2020-02-09 14:45:36 +00:00
|
|
|
/**
|
2021-03-29 06:40:20 +00:00
|
|
|
* @copyright Copyright (C) 2010-2021, the Friendica project
|
2020-02-09 14:45:36 +00:00
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
2019-12-09 23:44:56 +00:00
|
|
|
|
|
|
|
namespace Friendica\Factory;
|
|
|
|
|
|
|
|
use Friendica\App;
|
|
|
|
use Friendica\Core\Cache\ICache;
|
2020-01-18 14:41:19 +00:00
|
|
|
use Friendica\Core\Cache\Type;
|
2020-01-19 20:29:36 +00:00
|
|
|
use Friendica\Core\Config\IConfig;
|
2019-12-10 20:52:23 +00:00
|
|
|
use Friendica\Core\Session;
|
2019-12-09 23:44:56 +00:00
|
|
|
use Friendica\Core\System;
|
|
|
|
use Friendica\Database\Database;
|
|
|
|
use Friendica\Util\Profiler;
|
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
|
2019-12-09 23:50:05 +00:00
|
|
|
/**
|
|
|
|
* Factory for creating a valid Session for this run
|
|
|
|
*/
|
2019-12-09 23:44:56 +00:00
|
|
|
class SessionFactory
|
|
|
|
{
|
|
|
|
/** @var string The plain, PHP internal session management */
|
2019-12-11 19:30:31 +00:00
|
|
|
const HANDLER_NATIVE = 'native';
|
2019-12-09 23:44:56 +00:00
|
|
|
/** @var string Using the database for session management */
|
2019-12-11 19:30:31 +00:00
|
|
|
const HANDLER_DATABASE = 'database';
|
2019-12-09 23:44:56 +00:00
|
|
|
/** @var string Using the cache for session management */
|
2019-12-11 19:30:31 +00:00
|
|
|
const HANDLER_CACHE = 'cache';
|
|
|
|
|
|
|
|
const HANDLER_DEFAULT = self::HANDLER_DATABASE;
|
2019-12-09 23:44:56 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param App\Mode $mode
|
2019-12-11 19:30:31 +00:00
|
|
|
* @param App\BaseURL $baseURL
|
2020-01-19 20:29:36 +00:00
|
|
|
* @param IConfig $config
|
2019-12-09 23:44:56 +00:00
|
|
|
* @param Database $dba
|
|
|
|
* @param ICache $cache
|
|
|
|
* @param LoggerInterface $logger
|
|
|
|
* @param array $server
|
|
|
|
*
|
2019-12-10 20:52:23 +00:00
|
|
|
* @return Session\ISession
|
2019-12-09 23:44:56 +00:00
|
|
|
*/
|
2020-01-19 20:29:36 +00:00
|
|
|
public function createSession(App\Mode $mode, App\BaseURL $baseURL, IConfig $config, Database $dba, ICache $cache, LoggerInterface $logger, Profiler $profiler, array $server = [])
|
2019-12-09 23:44:56 +00:00
|
|
|
{
|
2021-07-27 04:57:29 +00:00
|
|
|
$profiler->startRecording('parser');
|
2019-12-09 23:44:56 +00:00
|
|
|
$session = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
if ($mode->isInstall() || $mode->isBackend()) {
|
2019-12-28 23:03:58 +00:00
|
|
|
$session = new Session\Memory();
|
2019-12-09 23:44:56 +00:00
|
|
|
} else {
|
2019-12-11 19:30:31 +00:00
|
|
|
$session_handler = $config->get('system', 'session_handler', self::HANDLER_DEFAULT);
|
|
|
|
$handler = null;
|
2019-12-09 23:44:56 +00:00
|
|
|
|
|
|
|
switch ($session_handler) {
|
2019-12-11 19:30:31 +00:00
|
|
|
case self::HANDLER_DATABASE:
|
|
|
|
$handler = new Session\Handler\Database($dba, $logger, $server);
|
2019-12-09 23:44:56 +00:00
|
|
|
break;
|
2019-12-11 19:30:31 +00:00
|
|
|
case self::HANDLER_CACHE:
|
2019-12-09 23:44:56 +00:00
|
|
|
// In case we're using the db as cache driver, use the native db session, not the cache
|
2020-01-18 14:41:19 +00:00
|
|
|
if ($config->get('system', 'cache_driver') === Type::DATABASE) {
|
2019-12-11 19:30:31 +00:00
|
|
|
$handler = new Session\Handler\Database($dba, $logger, $server);
|
2019-12-09 23:44:56 +00:00
|
|
|
} else {
|
2019-12-11 19:30:31 +00:00
|
|
|
$handler = new Session\Handler\Cache($cache, $logger, $server);
|
2019-12-09 23:44:56 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2019-12-11 19:30:31 +00:00
|
|
|
|
2019-12-28 23:03:58 +00:00
|
|
|
$session = new Session\Native($baseURL, $handler);
|
2019-12-09 23:44:56 +00:00
|
|
|
}
|
|
|
|
} finally {
|
2021-07-27 04:57:29 +00:00
|
|
|
$profiler->stopRecording();
|
2019-12-09 23:44:56 +00:00
|
|
|
return $session;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|