]> git.mxchange.org Git - friendica.git/blob - src/Factory/SessionFactory.php
Inverted condition
[friendica.git] / src / Factory / SessionFactory.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, 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\Factory;
23
24 use Friendica\App;
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;
33
34 /**
35  * Factory for creating a valid Session for this run
36  */
37 class SessionFactory
38 {
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';
45
46         const HANDLER_DEFAULT = self::HANDLER_DATABASE;
47
48         /**
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
56          *
57          * @return Session\ISession
58          */
59         public function createSession(App\Mode $mode, App\BaseURL $baseURL, IConfig $config, Database $dba, ICache $cache, LoggerInterface $logger, Profiler $profiler, array $server = [])
60         {
61                 $stamp1  = microtime(true);
62                 $session = null;
63
64                 try {
65                         if ($mode->isInstall() || $mode->isBackend()) {
66                                 $session = new Session\Memory();
67                         } else {
68                                 $session_handler = $config->get('system', 'session_handler', self::HANDLER_DEFAULT);
69                                 $handler = null;
70
71                                 switch ($session_handler) {
72                                         case self::HANDLER_DATABASE:
73                                                 $handler = new Session\Handler\Database($dba, $logger, $server);
74                                                 break;
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);
79                                                 } else {
80                                                         $handler = new Session\Handler\Cache($cache, $logger, $server);
81                                                 }
82                                                 break;
83                                 }
84
85                                 $session = new Session\Native($baseURL, $handler);
86                         }
87                 } finally {
88                         $profiler->saveTimestamp($stamp1, 'parser');
89                         return $session;
90                 }
91         }
92 }