]> git.mxchange.org Git - friendica.git/blob - src/Factory/SessionFactory.php
Merge pull request #7995 from annando/probe-hubzilla
[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 HANDLER_NATIVE = 'native';
23         /** @var string Using the database for session management */
24         const HANDLER_DATABASE = 'database';
25         /** @var string Using the cache for session management */
26         const HANDLER_CACHE = 'cache';
27
28         const HANDLER_DEFAULT = self::HANDLER_DATABASE;
29
30         /**
31          * @param App\Mode        $mode
32          * @param App\BaseURL     $baseURL
33          * @param Configuration   $config
34          * @param Cookie          $cookie
35          * @param Database        $dba
36          * @param ICache          $cache
37          * @param LoggerInterface $logger
38          * @param array           $server
39          *
40          * @return Session\ISession
41          */
42         public function createSession(App\Mode $mode, App\BaseURL $baseURL, Configuration $config, Cookie $cookie, Database $dba, ICache $cache, LoggerInterface $logger, Profiler $profiler, array $server = [])
43         {
44                 $stamp1  = microtime(true);
45                 $session = null;
46
47                 try {
48                         if ($mode->isInstall() || $mode->isBackend()) {
49                                 $session = new Session\Memory($cookie);
50                         } else {
51                                 $session_handler = $config->get('system', 'session_handler', self::HANDLER_DEFAULT);
52                                 $handler = null;
53
54                                 switch ($session_handler) {
55                                         case self::HANDLER_DATABASE:
56                                                 $handler = new Session\Handler\Database($dba, $logger, $server);
57                                                 break;
58                                         case self::HANDLER_CACHE:
59                                                 // In case we're using the db as cache driver, use the native db session, not the cache
60                                                 if ($config->get('system', 'cache_driver') === Cache::TYPE_DATABASE) {
61                                                         $handler = new Session\Handler\Database($dba, $logger, $server);
62                                                 } else {
63                                                         $handler = new Session\Handler\Cache($cache, $logger, $server);
64                                                 }
65                                                 break;
66                                 }
67
68                                 $session = new Session\Native($baseURL, $cookie, $handler);
69                         }
70                 } finally {
71                         $profiler->saveTimestamp($stamp1, 'parser', System::callstack());
72                         return $session;
73                 }
74         }
75 }