]> git.mxchange.org Git - friendica.git/commitdiff
Initializing SessionHandlers with Dependency Injection
authornupplaPhil <admin@philipp.info>
Mon, 9 Dec 2019 22:09:18 +0000 (23:09 +0100)
committernupplaPhil <admin@philipp.info>
Mon, 9 Dec 2019 22:09:18 +0000 (23:09 +0100)
src/Core/Session.php
src/Core/Session/CacheSessionHandler.php
src/Core/Session/DatabaseSessionHandler.php
src/Model/User/Cookie.php

index 542307a5ca4a2d27b6acb60f2f2d727f8e47737b..140781d1c644e659107803f1e71e4f0754eb6755 100644 (file)
@@ -7,12 +7,15 @@ namespace Friendica\Core;
 
 use Friendica\App;
 use Friendica\BaseObject;
+use Friendica\Core\Cache\ICache;
 use Friendica\Core\Session\CacheSessionHandler;
 use Friendica\Core\Session\DatabaseSessionHandler;
+use Friendica\Database\Database;
 use Friendica\Database\DBA;
 use Friendica\Model\Contact;
 use Friendica\Model\User;
 use Friendica\Util\Strings;
+use Psr\Log\LoggerInterface;
 
 /**
  * High-level Session service class
@@ -37,9 +40,17 @@ class Session
                $session_handler = Config::get('system', 'session_handler', 'database');
                if ($session_handler != 'native') {
                        if ($session_handler == 'cache' && Config::get('system', 'cache_driver', 'database') != 'database') {
-                               $SessionHandler = new CacheSessionHandler();
+                               $SessionHandler = new CacheSessionHandler(
+                                       BaseObject::getClass(ICache::class),
+                                       BaseObject::getClass(LoggerInterface::class),
+                                       $_SERVER
+                               );
                        } else {
-                               $SessionHandler = new DatabaseSessionHandler();
+                               $SessionHandler = new DatabaseSessionHandler(
+                                       BaseObject::getClass(Database::class),
+                                       BaseObject::getClass(LoggerInterface::class),
+                                       $_SERVER
+                               );
                        }
 
                        session_set_save_handler($SessionHandler);
index 6a1b32bfb1686c84b6e0d1954b4d3808cc273662..218ec1440fa8f97caf504f99c795ccecca76af85 100644 (file)
@@ -2,10 +2,9 @@
 
 namespace Friendica\Core\Session;
 
-use Friendica\BaseObject;
-use Friendica\Core\Cache;
-use Friendica\Core\Logger;
+use Friendica\Core\Cache\ICache;
 use Friendica\Core\Session;
+use Psr\Log\LoggerInterface;
 use SessionHandlerInterface;
 
 /**
@@ -13,8 +12,29 @@ use SessionHandlerInterface;
  *
  * @author Hypolite Petovan <hypolite@mrpetovan.com>
  */
-class CacheSessionHandler extends BaseObject implements SessionHandlerInterface
+class CacheSessionHandler implements SessionHandlerInterface
 {
+       /** @var ICache */
+       private $cache;
+       /** @var LoggerInterface */
+       private $logger;
+       /** @var array The $_SERVER array */
+       private $server;
+
+       /**
+        * CacheSessionHandler constructor.
+        *
+        * @param ICache          $cache
+        * @param LoggerInterface $logger
+        * @param array           $server
+        */
+       public function __construct(ICache $cache, LoggerInterface $logger, array $server)
+       {
+               $this->cache  = $cache;
+               $this->logger = $logger;
+               $this->server = $server;
+       }
+
        public function open($save_path, $session_name)
        {
                return true;
@@ -26,13 +46,13 @@ class CacheSessionHandler extends BaseObject implements SessionHandlerInterface
                        return '';
                }
 
-               $data = Cache::get('session:' . $session_id);
+               $data = $this->cache->get('session:' . $session_id);
                if (!empty($data)) {
                        Session::$exists = true;
                        return $data;
                }
 
-               Logger::notice('no data for session', ['session_id' => $session_id, 'uri' => $_SERVER['REQUEST_URI']]);
+               $this->logger->notice('no data for session', ['session_id' => $session_id, 'uri' => $this->server['REQUEST_URI'] ?? '']);
 
                return '';
        }
@@ -59,9 +79,7 @@ class CacheSessionHandler extends BaseObject implements SessionHandlerInterface
                        return true;
                }
 
-               $return = Cache::set('session:' . $session_id, $session_data, Session::$expire);
-
-               return $return;
+               return $this->cache->set('session:' . $session_id, $session_data, Session::$expire);
        }
 
        public function close()
@@ -71,9 +89,7 @@ class CacheSessionHandler extends BaseObject implements SessionHandlerInterface
 
        public function destroy($id)
        {
-               $return = Cache::delete('session:' . $id);
-
-               return $return;
+               return $this->cache->delete('session:' . $id);
        }
 
        public function gc($maxlifetime)
index e3e95f9ed12a5d07faec60cbed65f834a1be827e..5d8441e354e6b0ba37f04d7ab0e1794c07b4cc00 100644 (file)
@@ -2,10 +2,9 @@
 
 namespace Friendica\Core\Session;
 
-use Friendica\BaseObject;
-use Friendica\Core\Logger;
 use Friendica\Core\Session;
-use Friendica\Database\DBA;
+use Friendica\Database\Database;
+use Psr\Log\LoggerInterface;
 use SessionHandlerInterface;
 
 /**
@@ -13,8 +12,29 @@ use SessionHandlerInterface;
  *
  * @author Hypolite Petovan <hypolite@mrpetovan.com>
  */
-class DatabaseSessionHandler extends BaseObject implements SessionHandlerInterface
+class DatabaseSessionHandler implements SessionHandlerInterface
 {
+       /** @var Database */
+       private $dba;
+       /** @var LoggerInterface */
+       private $logger;
+       /** @var array The $_SERVER variable */
+       private $server;
+
+       /**
+        * DatabaseSessionHandler constructor.
+        *
+        * @param Database        $dba
+        * @param LoggerInterface $logger
+        * @param array           $server
+        */
+       public function __construct(Database $dba, LoggerInterface $logger, array $server)
+       {
+               $this->dba    = $dba;
+               $this->logger = $logger;
+               $this->server = $server;
+       }
+
        public function open($save_path, $session_name)
        {
                return true;
@@ -26,13 +46,13 @@ class DatabaseSessionHandler extends BaseObject implements SessionHandlerInterfa
                        return '';
                }
 
-               $session = DBA::selectFirst('session', ['data'], ['sid' => $session_id]);
-               if (DBA::isResult($session)) {
+               $session = $this->dba->selectFirst('session', ['data'], ['sid' => $session_id]);
+               if ($this->dba->isResult($session)) {
                        Session::$exists = true;
                        return $session['data'];
                }
 
-               Logger::notice('no data for session', ['session_id' => $session_id, 'uri' => $_SERVER['REQUEST_URI']]);
+               $this->logger->notice('no data for session', ['session_id' => $session_id, 'uri' => $this->server['REQUEST_URI'] ?? '']);
 
                return '';
        }
@@ -65,10 +85,10 @@ class DatabaseSessionHandler extends BaseObject implements SessionHandlerInterfa
                if (Session::$exists) {
                        $fields = ['data' => $session_data, 'expire' => $expire];
                        $condition = ["`sid` = ? AND (`data` != ? OR `expire` != ?)", $session_id, $session_data, $expire];
-                       DBA::update('session', $fields, $condition);
+                       $this->dba->update('session', $fields, $condition);
                } else {
                        $fields = ['sid' => $session_id, 'expire' => $default_expire, 'data' => $session_data];
-                       DBA::insert('session', $fields);
+                       $this->dba->insert('session', $fields);
                }
 
                return true;
@@ -81,13 +101,11 @@ class DatabaseSessionHandler extends BaseObject implements SessionHandlerInterfa
 
        public function destroy($id)
        {
-               DBA::delete('session', ['sid' => $id]);
-               return true;
+               return $this->dba->delete('session', ['sid' => $id]);
        }
 
        public function gc($maxlifetime)
        {
-               DBA::delete('session', ["`expire` < ?", time()]);
-               return true;
+               return $this->dba->delete('session', ["`expire` < ?", time()]);
        }
 }
index 79882d6415eaa27f77567c60febf3ed3ed348b6c..f85d818686352f34519fda5639d4fd0acf3126d9 100644 (file)
@@ -18,7 +18,7 @@ class Cookie
        const PATH = '/';
        /** @var string The domain name of the Friendica cookie */
        const DOMAIN = '';
-       /** @var bool True, if the cookie should only be accessable through HTTP */
+       /** @var bool True, if the cookie should only be accessible through HTTP */
        const HTTPONLY = true;
 
        /** @var string The remote address of this node */
@@ -68,10 +68,10 @@ class Cookie
        /**
         * Set the Friendica cookie for a user
         *
-        * @param int    $uid        The user id
-        * @param string $password   The user password
-        * @param string $privateKey The user private key
-        * @param int|null    $seconds    optional the seconds
+        * @param int      $uid        The user id
+        * @param string   $password   The user password
+        * @param string   $privateKey The user private key
+        * @param int|null $seconds    optional the seconds
         *
         * @return bool
         */
@@ -142,9 +142,9 @@ class Cookie
         * @link  https://php.net/manual/en/function.setcookie.php
         *
         * @param string $name
-        * @param string $value    [optional]
-        * @param int    $expire   [optional]
-        * @param bool   $secure   [optional]
+        * @param string $value  [optional]
+        * @param int    $expire [optional]
+        * @param bool   $secure [optional]
         *
         * @return bool If output exists prior to calling this function,
         *