]> git.mxchange.org Git - friendica.git/commitdiff
Add storage backend manager class
authorfabrixxm <fabrix.xm@gmail.com>
Thu, 29 Nov 2018 08:27:04 +0000 (09:27 +0100)
committerHypolite Petovan <hypolite@mrpetovan.com>
Mon, 21 Jan 2019 14:11:34 +0000 (09:11 -0500)
src/Core/StorageManager.php [new file with mode: 0644]
src/Model/Photo.php

diff --git a/src/Core/StorageManager.php b/src/Core/StorageManager.php
new file mode 100644 (file)
index 0000000..6f3e98a
--- /dev/null
@@ -0,0 +1,102 @@
+<?php
+
+namespace Friendica\Core;
+
+use Friendica\Core\Config;
+
+
+
+/**
+ * @brief Manage storage backends
+ *
+ * Core code uses this class to get and set current storage backend class.
+ * Addons use this class to register and unregister additional backends.
+ */
+class StorageManager
+{
+       private static $default_storages = [
+               'Filesystem' => \Friendica\Model\Storage\Filesystem::class,
+               'Database' => \Friendica\Model\Storage\Database::class,
+       ];
+
+       private static $storages = [];
+
+       private static function setup()
+       {
+               if (count(self::$storages)==0) {
+                       self::$storage = Config::get('storage', 'backends', self::$default_storages);
+               }
+       }
+
+       /**
+        * @brief Return current storage backend class
+        * @return string
+        */
+       public static function getBackend()
+       {
+               return Config::get('storage', 'class', '');
+       }
+
+       /**
+        * @brief Return storage backend class by registered name
+        *
+        * @param string  $name  Backend name
+        * @return string Empty if no backend registered at $name exists
+        */
+       public static function getByName($name)
+       {
+               self::setup();
+               return defaults(self::$storages, $name, '');
+       }
+
+       /**
+        * @brief Set current storage backend class
+        *
+        * @param string  $class  Backend class name
+        */
+       public static function setBackend($class)
+       {
+               /// @todo Check that $class implements IStorage
+               Config::set('storage', 'class', $class);
+       }
+
+       /**
+        * @brief Get registered backends
+        *
+        * @return array
+        */
+       public static function listBackends()
+       {
+               self::setup();
+               return self::$backends;
+       }
+
+
+
+       /**
+        * @brief Register a storage backend class
+        *
+        * @param string  $name   User readable backend name
+        * @param string  $class  Backend class name
+        */
+       public static function register($name, $class)
+       {
+               /// @todo Check that $class implements IStorage
+               self::setup();
+               self::$storages[$name] = $class;
+               Config::set('storage', 'backends', self::$storages);
+       }
+
+
+       /**
+        * @brief Unregister a storage backend class
+        *
+        * @param string  $name   User readable backend name
+        */
+       public static function unregister($class)
+       {
+               self::setup();
+               unset(self::$storages[$name]);
+               Config::set('storage', 'backends', self::$storages);
+       }
+}
\ No newline at end of file
index 0156b5d32e351522f0bcb836b0f5d11679ed9169..4942381a4c15fbd2a434750d0d2dbd27d4e6dd17 100644 (file)
@@ -11,6 +11,7 @@ use Friendica\Core\Cache;
 use Friendica\Core\Config;
 use Friendica\Core\L10n;
 use Friendica\Core\System;
+use Friendica\Core\StorageManager;
 use Friendica\Database\DBA;
 use Friendica\Database\DBStructure;
 use Friendica\Object\Image;
@@ -263,7 +264,7 @@ class Photo extends BaseObject
                        $backend_ref = (string)$existing_photo["backend-ref"];
                        $backend_class = (string)$existing_photo["backend-class"];
                } else {
-                       $backend_class = Config::get("storage", "class", "");
+                       $backend_class = StorageManager::getBackend();
                }
                if ($backend_class === "") {
                        $data = $Image->asString();