3 * @copyright Copyright (C) 2010-2021, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Factory;
25 use Friendica\Core\Cache\ICache;
26 use Friendica\Core\Cache\Type;
27 use Friendica\Core\Config\IConfig;
28 use Friendica\Core\Session;
29 use Friendica\Core\System;
30 use Friendica\Database\Database;
31 use Friendica\Util\Profiler;
32 use Psr\Log\LoggerInterface;
35 * Factory for creating a valid Session for this run
39 /** @var string The plain, PHP internal session management */
40 const HANDLER_NATIVE = 'native';
41 /** @var string Using the database for session management */
42 const HANDLER_DATABASE = 'database';
43 /** @var string Using the cache for session management */
44 const HANDLER_CACHE = 'cache';
46 const HANDLER_DEFAULT = self::HANDLER_DATABASE;
49 * @param App\Mode $mode
50 * @param App\BaseURL $baseURL
51 * @param IConfig $config
52 * @param Database $dba
53 * @param ICache $cache
54 * @param LoggerInterface $logger
55 * @param array $server
57 * @return Session\ISession
59 public function createSession(App\Mode $mode, App\BaseURL $baseURL, IConfig $config, Database $dba, ICache $cache, LoggerInterface $logger, Profiler $profiler, array $server = [])
61 $stamp1 = microtime(true);
65 if ($mode->isInstall() || $mode->isBackend()) {
66 $session = new Session\Memory();
68 $session_handler = $config->get('system', 'session_handler', self::HANDLER_DEFAULT);
71 switch ($session_handler) {
72 case self::HANDLER_DATABASE:
73 $handler = new Session\Handler\Database($dba, $logger, $server);
75 case self::HANDLER_CACHE:
76 // In case we're using the db as cache driver, use the native db session, not the cache
77 if ($config->get('system', 'cache_driver') === Type::DATABASE) {
78 $handler = new Session\Handler\Database($dba, $logger, $server);
80 $handler = new Session\Handler\Cache($cache, $logger, $server);
85 $session = new Session\Native($baseURL, $handler);
88 $profiler->saveTimestamp($stamp1, 'parser');