]> git.mxchange.org Git - friendica.git/commitdiff
Restructure Session to follow new paradigm
authorPhilipp <admin@philipp.info>
Sat, 23 Oct 2021 09:39:29 +0000 (11:39 +0200)
committerPhilipp <admin@philipp.info>
Tue, 26 Oct 2021 20:11:07 +0000 (22:11 +0200)
src/Core/Session/AbstractSession.php [deleted file]
src/Core/Session/Factory/SessionFactory.php [new file with mode: 0644]
src/Core/Session/Memory.php [deleted file]
src/Core/Session/Native.php [deleted file]
src/Core/Session/Type/AbstractSession.php [new file with mode: 0644]
src/Core/Session/Type/Memory.php [new file with mode: 0644]
src/Core/Session/Type/Native.php [new file with mode: 0644]
src/Factory/SessionFactory.php [deleted file]
static/dependencies.config.php
tests/FixtureTest.php
tests/src/Core/StorageManagerTest.php

diff --git a/src/Core/Session/AbstractSession.php b/src/Core/Session/AbstractSession.php
deleted file mode 100644 (file)
index c185f1b..0000000
+++ /dev/null
@@ -1,84 +0,0 @@
-<?php
-/**
- * @copyright Copyright (C) 2010-2021, the Friendica project
- *
- * @license GNU AGPL version 3 or any later version
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program.  If not, see <https://www.gnu.org/licenses/>.
- *
- */
-
-namespace Friendica\Core\Session;
-
-/**
- * Contains the base methods for $_SESSION interaction
- */
-class AbstractSession
-{
-       /**
-        * {@inheritDoc}
-        */
-       public function start()
-       {
-               return $this;
-       }
-
-       /**
-        * {@inheritDoc}}
-        */
-       public function exists(string $name)
-       {
-               return isset($_SESSION[$name]);
-       }
-
-       /**
-        * {@inheritDoc}
-        */
-       public function get(string $name, $defaults = null)
-       {
-               return $_SESSION[$name] ?? $defaults;
-       }
-
-       /**
-        * {@inheritDoc}
-        */
-       public function set(string $name, $value)
-       {
-               $_SESSION[$name] = $value;
-       }
-
-       /**
-        * {@inheritDoc}
-        */
-       public function setMultiple(array $values)
-       {
-               $_SESSION = $values + $_SESSION;
-       }
-
-       /**
-        * {@inheritDoc}
-        */
-       public function remove(string $name)
-       {
-               unset($_SESSION[$name]);
-       }
-
-       /**
-        * {@inheritDoc}
-        */
-       public function clear()
-       {
-               $_SESSION = [];
-       }
-}
diff --git a/src/Core/Session/Factory/SessionFactory.php b/src/Core/Session/Factory/SessionFactory.php
new file mode 100644 (file)
index 0000000..510ff08
--- /dev/null
@@ -0,0 +1,91 @@
+<?php
+/**
+ * @copyright Copyright (C) 2010-2021, the Friendica project
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace Friendica\Core\Session\Factory;
+
+use Friendica\App;
+use Friendica\Core\Cache\ICache;
+use Friendica\Core\Cache\Enum\Type;
+use Friendica\Core\Config\IConfig;
+use Friendica\Core\Session;
+use Friendica\Database\Database;
+use Friendica\Util\Profiler;
+use Psr\Log\LoggerInterface;
+
+/**
+ * Factory for creating a valid Session for this run
+ */
+class SessionFactory
+{
+       /** @var string The plain, PHP internal session management */
+       const HANDLER_NATIVE = 'native';
+       /** @var string Using the database for session management */
+       const HANDLER_DATABASE = 'database';
+       /** @var string Using the cache for session management */
+       const HANDLER_CACHE = 'cache';
+
+       const HANDLER_DEFAULT = self::HANDLER_DATABASE;
+
+       /**
+        * @param App\Mode        $mode
+        * @param App\BaseURL     $baseURL
+        * @param IConfig         $config
+        * @param Database        $dba
+        * @param ICache          $cache
+        * @param LoggerInterface $logger
+        * @param array           $server
+        *
+        * @return Session\ISession
+        */
+       public function createSession(App\Mode $mode, App\BaseURL $baseURL, IConfig $config, Database $dba, ICache $cache, LoggerInterface $logger, Profiler $profiler, array $server = [])
+       {
+               $profiler->startRecording('session');
+               $session = null;
+
+               try {
+                       if ($mode->isInstall() || $mode->isBackend()) {
+                               $session = new Session\Type\Memory();
+                       } else {
+                               $session_handler = $config->get('system', 'session_handler', self::HANDLER_DEFAULT);
+                               $handler = null;
+
+                               switch ($session_handler) {
+                                       case self::HANDLER_DATABASE:
+                                               $handler = new Session\Handler\Database($dba, $logger, $server);
+                                               break;
+                                       case self::HANDLER_CACHE:
+                                               // In case we're using the db as cache driver, use the native db session, not the cache
+                                               if ($config->get('system', 'cache_driver') === Type::DATABASE) {
+                                                       $handler = new Session\Handler\Database($dba, $logger, $server);
+                                               } else {
+                                                       $handler = new Session\Handler\Cache($cache);
+                                               }
+                                               break;
+                               }
+
+                               $session = new Session\Type\Native($baseURL, $handler);
+                       }
+               } finally {
+                       $profiler->stopRecording();
+                       return $session;
+               }
+       }
+}
diff --git a/src/Core/Session/Memory.php b/src/Core/Session/Memory.php
deleted file mode 100644 (file)
index 7cf9fd6..0000000
+++ /dev/null
@@ -1,37 +0,0 @@
-<?php
-/**
- * @copyright Copyright (C) 2010-2021, the Friendica project
- *
- * @license GNU AGPL version 3 or any later version
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program.  If not, see <https://www.gnu.org/licenses/>.
- *
- */
-
-namespace Friendica\Core\Session;
-
-/**
- * Usable for backend processes (daemon/worker) and testing
- *
- * @todo after replacing the last direct $_SESSION call, use a internal array instead of the global variable
- */
-class Memory extends AbstractSession implements ISession
-{
-       public function __construct()
-       {
-               // Backward compatibility until all Session variables are replaced
-               // with the Session class
-               $_SESSION = [];
-       }
-}
diff --git a/src/Core/Session/Native.php b/src/Core/Session/Native.php
deleted file mode 100644 (file)
index 23d02af..0000000
+++ /dev/null
@@ -1,56 +0,0 @@
-<?php
-/**
- * @copyright Copyright (C) 2010-2021, the Friendica project
- *
- * @license GNU AGPL version 3 or any later version
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program.  If not, see <https://www.gnu.org/licenses/>.
- *
- */
-
-namespace Friendica\Core\Session;
-
-use Friendica\App;
-use Friendica\Model\User\Cookie;
-use SessionHandlerInterface;
-
-/**
- * The native Session class which uses the PHP internal Session functions
- */
-class Native extends AbstractSession implements ISession
-{
-       public function __construct(App\BaseURL $baseURL, SessionHandlerInterface $handler = null)
-       {
-               ini_set('session.gc_probability', 50);
-               ini_set('session.use_only_cookies', 1);
-               ini_set('session.cookie_httponly', (int)Cookie::HTTPONLY);
-
-               if ($baseURL->getSSLPolicy() == App\BaseURL::SSL_POLICY_FULL) {
-                       ini_set('session.cookie_secure', 1);
-               }
-
-               if (isset($handler)) {
-                       session_set_save_handler($handler);
-               }
-       }
-
-       /**
-        * {@inheritDoc}
-        */
-       public function start()
-       {
-               session_start();
-               return $this;
-       }
-}
diff --git a/src/Core/Session/Type/AbstractSession.php b/src/Core/Session/Type/AbstractSession.php
new file mode 100644 (file)
index 0000000..8126804
--- /dev/null
@@ -0,0 +1,84 @@
+<?php
+/**
+ * @copyright Copyright (C) 2010-2021, the Friendica project
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace Friendica\Core\Session\Type;
+
+/**
+ * Contains the base methods for $_SESSION interaction
+ */
+class AbstractSession
+{
+       /**
+        * {@inheritDoc}
+        */
+       public function start()
+       {
+               return $this;
+       }
+
+       /**
+        * {@inheritDoc}}
+        */
+       public function exists(string $name)
+       {
+               return isset($_SESSION[$name]);
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public function get(string $name, $defaults = null)
+       {
+               return $_SESSION[$name] ?? $defaults;
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public function set(string $name, $value)
+       {
+               $_SESSION[$name] = $value;
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public function setMultiple(array $values)
+       {
+               $_SESSION = $values + $_SESSION;
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public function remove(string $name)
+       {
+               unset($_SESSION[$name]);
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public function clear()
+       {
+               $_SESSION = [];
+       }
+}
diff --git a/src/Core/Session/Type/Memory.php b/src/Core/Session/Type/Memory.php
new file mode 100644 (file)
index 0000000..635d880
--- /dev/null
@@ -0,0 +1,39 @@
+<?php
+/**
+ * @copyright Copyright (C) 2010-2021, the Friendica project
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace Friendica\Core\Session\Type;
+
+use Friendica\Core\Session\ISession;
+
+/**
+ * Usable for backend processes (daemon/worker) and testing
+ *
+ * @todo after replacing the last direct $_SESSION call, use a internal array instead of the global variable
+ */
+class Memory extends AbstractSession implements ISession
+{
+       public function __construct()
+       {
+               // Backward compatibility until all Session variables are replaced
+               // with the Session class
+               $_SESSION = [];
+       }
+}
diff --git a/src/Core/Session/Type/Native.php b/src/Core/Session/Type/Native.php
new file mode 100644 (file)
index 0000000..4edd9a9
--- /dev/null
@@ -0,0 +1,57 @@
+<?php
+/**
+ * @copyright Copyright (C) 2010-2021, the Friendica project
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace Friendica\Core\Session\Type;
+
+use Friendica\App;
+use Friendica\Core\Session\ISession;
+use Friendica\Model\User\Cookie;
+use SessionHandlerInterface;
+
+/**
+ * The native Session class which uses the PHP internal Session functions
+ */
+class Native extends AbstractSession implements ISession
+{
+       public function __construct(App\BaseURL $baseURL, SessionHandlerInterface $handler = null)
+       {
+               ini_set('session.gc_probability', 50);
+               ini_set('session.use_only_cookies', 1);
+               ini_set('session.cookie_httponly', (int)Cookie::HTTPONLY);
+
+               if ($baseURL->getSSLPolicy() == App\BaseURL::SSL_POLICY_FULL) {
+                       ini_set('session.cookie_secure', 1);
+               }
+
+               if (isset($handler)) {
+                       session_set_save_handler($handler);
+               }
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public function start()
+       {
+               session_start();
+               return $this;
+       }
+}
diff --git a/src/Factory/SessionFactory.php b/src/Factory/SessionFactory.php
deleted file mode 100644 (file)
index 82161fb..0000000
+++ /dev/null
@@ -1,92 +0,0 @@
-<?php
-/**
- * @copyright Copyright (C) 2010-2021, the Friendica project
- *
- * @license GNU AGPL version 3 or any later version
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program.  If not, see <https://www.gnu.org/licenses/>.
- *
- */
-
-namespace Friendica\Factory;
-
-use Friendica\App;
-use Friendica\Core\Cache\ICache;
-use Friendica\Core\Cache\Enum\Type;
-use Friendica\Core\Config\IConfig;
-use Friendica\Core\Session;
-use Friendica\Core\System;
-use Friendica\Database\Database;
-use Friendica\Util\Profiler;
-use Psr\Log\LoggerInterface;
-
-/**
- * Factory for creating a valid Session for this run
- */
-class SessionFactory
-{
-       /** @var string The plain, PHP internal session management */
-       const HANDLER_NATIVE = 'native';
-       /** @var string Using the database for session management */
-       const HANDLER_DATABASE = 'database';
-       /** @var string Using the cache for session management */
-       const HANDLER_CACHE = 'cache';
-
-       const HANDLER_DEFAULT = self::HANDLER_DATABASE;
-
-       /**
-        * @param App\Mode        $mode
-        * @param App\BaseURL     $baseURL
-        * @param IConfig         $config
-        * @param Database        $dba
-        * @param ICache          $cache
-        * @param LoggerInterface $logger
-        * @param array           $server
-        *
-        * @return Session\ISession
-        */
-       public function createSession(App\Mode $mode, App\BaseURL $baseURL, IConfig $config, Database $dba, ICache $cache, LoggerInterface $logger, Profiler $profiler, array $server = [])
-       {
-               $profiler->startRecording('session');
-               $session = null;
-
-               try {
-                       if ($mode->isInstall() || $mode->isBackend()) {
-                               $session = new Session\Memory();
-                       } else {
-                               $session_handler = $config->get('system', 'session_handler', self::HANDLER_DEFAULT);
-                               $handler = null;
-
-                               switch ($session_handler) {
-                                       case self::HANDLER_DATABASE:
-                                               $handler = new Session\Handler\Database($dba, $logger, $server);
-                                               break;
-                                       case self::HANDLER_CACHE:
-                                               // In case we're using the db as cache driver, use the native db session, not the cache
-                                               if ($config->get('system', 'cache_driver') === Type::DATABASE) {
-                                                       $handler = new Session\Handler\Database($dba, $logger, $server);
-                                               } else {
-                                                       $handler = new Session\Handler\Cache($cache);
-                                               }
-                                               break;
-                               }
-
-                               $session = new Session\Native($baseURL, $handler);
-                       }
-               } finally {
-                       $profiler->stopRecording();
-                       return $session;
-               }
-       }
-}
index 48ed997f337a62d095e58065a2d2f741e4a38c60..4591f4761cabc42f638909436a2bcad12546244b 100644 (file)
@@ -207,7 +207,7 @@ return [
                ],
        ],
        ISession::class => [
-               'instanceOf' => Factory\SessionFactory::class,
+               'instanceOf' => \Friendica\Core\Session\Factory\SessionFactory::class,
                'call' => [
                        ['createSession', [$_SERVER], Dice::CHAIN_CALL],
                        ['start', [], Dice::CHAIN_CALL],
index fdeda1febc22c83c5b3bd6e9809c569bdaaeb800..c09f874fa06ff33a713cd554b192ec2b06ecff61 100644 (file)
@@ -33,7 +33,7 @@ abstract class FixtureTest extends DatabaseTest
                $this->dice = (new Dice())
                        ->addRules(include __DIR__ . '/../static/dependencies.config.php')
                        ->addRule(Database::class, ['instanceOf' => StaticDatabase::class, 'shared' => true])
-                       ->addRule(ISession::class, ['instanceOf' => Session\Memory::class, 'shared' => true, 'call' => null]);
+                       ->addRule(ISession::class, ['instanceOf' => Session\Type\Memory::class, 'shared' => true, 'call' => null]);
                DI::init($this->dice);
 
                /** @var IConfig $config */
index acab50ce42390f6f58f2de05a54c919f536d3835..b7e5715edc0f0e3d9588c7916bbef8045d4d6fea 100644 (file)
@@ -253,7 +253,7 @@ class StorageManagerTest extends DatabaseTest
                $dice = (new Dice())
                        ->addRules(include __DIR__ . '/../../../static/dependencies.config.php')
                        ->addRule(Database::class, ['instanceOf' => StaticDatabase::class, 'shared' => true])
-                       ->addRule(ISession::class, ['instanceOf' => Session\Memory::class, 'shared' => true, 'call' => null]);
+                       ->addRule(ISession::class, ['instanceOf' => Session\Type\Memory::class, 'shared' => true, 'call' => null]);
                DI::init($dice);
 
                $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n);
@@ -281,7 +281,7 @@ class StorageManagerTest extends DatabaseTest
                $dice = (new Dice())
                        ->addRules(include __DIR__ . '/../../../static/dependencies.config.php')
                        ->addRule(Database::class, ['instanceOf' => StaticDatabase::class, 'shared' => true])
-                       ->addRule(ISession::class, ['instanceOf' => Session\Memory::class, 'shared' => true, 'call' => null]);
+                       ->addRule(ISession::class, ['instanceOf' => Session\Type\Memory::class, 'shared' => true, 'call' => null]);
                DI::init($dice);
 
                $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n);