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