]> git.mxchange.org Git - friendica.git/blob - src/Factory/SessionFactory.php
double use
[friendica.git] / src / Factory / SessionFactory.php
1 <?php
2
3 namespace Friendica\Factory;
4
5 use Friendica\App;
6 use Friendica\Core\Cache\Cache;
7 use Friendica\Core\Cache\ICache;
8 use Friendica\Core\Config\Configuration;
9 use Friendica\Core\Session\ISession;
10 use Friendica\Core\Session\Memory;
11 use Friendica\Core\Session\Native;
12 use Friendica\Core\System;
13 use Friendica\Database\Database;
14 use Friendica\Model\User\Cookie;
15 use Friendica\Util\Profiler;
16 use Psr\Log\LoggerInterface;
17
18 /**
19  * Factory for creating a valid Session for this run
20  */
21 class SessionFactory
22 {
23         /** @var string The plain, PHP internal session management */
24         const INTERNAL = 'native';
25         /** @var string Using the database for session management */
26         const DATABASE = 'database';
27         /** @var string Using the cache for session management */
28         const CACHE = 'cache';
29         /** @var string A temporary cached session */
30         const MEMORY  = 'memory';
31         /** @var string The default type for Session management in case of no config */
32         const DEFAULT = self::DATABASE;
33
34         /**
35          * @param App\Mode        $mode
36          * @param Configuration   $config
37          * @param Cookie          $cookie
38          * @param Database        $dba
39          * @param ICache          $cache
40          * @param LoggerInterface $logger
41          * @param array           $server
42          *
43          * @return ISession
44          */
45         public function createSession(App\Mode $mode, Configuration $config, Cookie $cookie, Database $dba, ICache $cache, LoggerInterface $logger, Profiler $profiler, array $server = [])
46         {
47                 $stamp1  = microtime(true);
48                 $session = null;
49
50                 try {
51                         if ($mode->isInstall() || $mode->isBackend()) {
52                                 $session = new Memory();
53                         } else {
54                                 $session_handler = $config->get('system', 'session_handler', self::DEFAULT);
55
56                                 switch ($session_handler) {
57                                         case self::INTERNAL:
58                                                 $session = new Native($config, $cookie);
59                                                 break;
60                                         case self::DATABASE:
61                                         default:
62                                                 $session = new Database($config, $cookie, $dba, $logger, $server);
63                                                 break;
64                                         case self::CACHE:
65                                                 // In case we're using the db as cache driver, use the native db session, not the cache
66                                                 if ($config->get('system', 'cache_driver') === Cache::TYPE_DATABASE) {
67                                                         $session = new Database($config, $cookie, $dba, $logger, $server);
68                                                 } else {
69                                                         $session = new Cache($config, $cookie, $cache, $logger, $server);
70                                                 }
71                                                 break;
72                                 }
73                         }
74                 } finally {
75                         $profiler->saveTimestamp($stamp1, 'parser', System::callstack());
76                         return $session;
77                 }
78         }
79 }