]> git.mxchange.org Git - friendica.git/blob - src/Core/Session/Factory/Session.php
Merge pull request #12021 from nupplaphil/feat/session_util
[friendica.git] / src / Core / Session / Factory / Session.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Core\Session\Factory;
23
24 use Friendica\App;
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;
34
35 /**
36  * Factory for creating a valid Session for this run
37  */
38 class Session
39 {
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';
46
47         const HANDLER_DEFAULT = self::HANDLER_DATABASE;
48
49         /**
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
58          *
59          * @return IHandleSessions
60          */
61         public function createSession(App\Mode $mode, App\BaseURL $baseURL, IManageConfigValues $config, Database $dba, Cache $cacheFactory, LoggerInterface $logger, Profiler $profiler, array $server = [])
62         {
63                 $profiler->startRecording('session');
64                 $session = null;
65
66                 try {
67                         if ($mode->isInstall() || $mode->isBackend()) {
68                                 $session = new Type\Memory();
69                         } else {
70                                 $session_handler = $config->get('system', 'session_handler', self::HANDLER_DEFAULT);
71                                 $handler         = null;
72
73                                 switch ($session_handler) {
74                                         case self::HANDLER_DATABASE:
75                                                 $handler = new Handler\Database($dba, $logger, $server);
76                                                 break;
77                                         case self::HANDLER_CACHE:
78                                                 $cache = $cacheFactory->createDistributed();
79
80                                                 // In case we're using the db as cache driver, use the native db session, not the cache
81                                                 if ($config->get('system', 'cache_driver') === Enum\Type::DATABASE) {
82                                                         $handler = new Handler\Database($dba, $logger, $server);
83                                                 } else {
84                                                         $handler = new Handler\Cache($cache, $logger);
85                                                 }
86                                                 break;
87                                 }
88
89                                 $session = new Type\Native($baseURL, $handler);
90                         }
91                 } finally {
92                         $profiler->stopRecording();
93                         return $session;
94                 }
95         }
96 }