3 * @copyright Copyright (C) 2010-2022, 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\Core\Session\Factory;
25 use Friendica\Core\Cache\Enum;
26 use Friendica\Core\Cache\Factory\Cache;
27 use Friendica\Core\Config\Capability\IManageConfigValues;
28 use Friendica\Core\Session\Capability\IHandleSessions;
29 use Friendica\Core\Session\Type;
30 use Friendica\Core\Session\Handler;
31 use Friendica\Database\Database;
32 use Friendica\Util\Profiler;
33 use Psr\Log\LoggerInterface;
36 * Factory for creating a valid Session for this run
40 /** @var string The plain, PHP internal session management */
41 const HANDLER_NATIVE = 'native';
42 /** @var string Using the database for session management */
43 const HANDLER_DATABASE = 'database';
44 /** @var string Using the cache for session management */
45 const HANDLER_CACHE = 'cache';
47 const HANDLER_DEFAULT = self::HANDLER_DATABASE;
50 * @param App\Mode $mode
51 * @param App\BaseURL $baseURL
52 * @param IManageConfigValues $config
53 * @param Database $dba
54 * @param Cache $cacheFactory
55 * @param LoggerInterface $logger
56 * @param Profiler $profiler
57 * @param array $server
59 public function createSession(App\Mode $mode, App\BaseURL $baseURL, IManageConfigValues $config, Database $dba, Cache $cacheFactory, LoggerInterface $logger, Profiler $profiler, array $server = []): IHandleSessions
61 $profiler->startRecording('session');
65 if ($mode->isInstall() || $mode->isBackend()) {
66 $session = new Type\Memory();
68 $session_handler = $config->get('system', 'session_handler', self::HANDLER_DEFAULT);
71 switch ($session_handler) {
72 case self::HANDLER_DATABASE:
73 $handler = new Handler\Database($dba, $logger, $server);
75 case self::HANDLER_CACHE:
76 $cache = $cacheFactory->createDistributed();
78 // In case we're using the db as cache driver, use the native db session, not the cache
79 if ($config->get('system', 'cache_driver') === Enum\Type::DATABASE) {
80 $handler = new Handler\Database($dba, $logger, $server);
82 $handler = new Handler\Cache($cache, $logger);
87 $session = new Type\Native($baseURL, $handler);
90 $profiler->stopRecording();