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