]> git.mxchange.org Git - friendica.git/blobdiff - src/Core/StorageManager.php
Check that provided class implements IStorage in StorageManager::setBackend
[friendica.git] / src / Core / StorageManager.php
index 4c7b151bf7d1d49052dfa8e26b37426de7e1e946..3bb1cc4511161960dcbc47aec33634eba7c8895f 100644 (file)
@@ -3,9 +3,7 @@
 namespace Friendica\Core;
 
 use Friendica\Database\DBA;
-use Friendica\Core\Config;
-use Friendica\Core\Logger;
-
+use Friendica\Model\Storage\IStorage;
 
 
 /**
@@ -17,15 +15,15 @@ use Friendica\Core\Logger;
 class StorageManager
 {
        private static $default_backends = [
-               'Filesystem' => Friendica\Model\Storage\Filesystem::class,
-               'Database' => Friendica\Model\Storage\Database::class,
+               'Filesystem' => \Friendica\Model\Storage\Filesystem::class,
+               'Database' => \Friendica\Model\Storage\Database::class,
        ];
 
        private static $backends = [];
 
        private static function setup()
        {
-               if (count(self::$backends)==0) {
+               if (count(self::$backends) == 0) {
                        self::$backends = Config::get('storage', 'backends', self::$default_backends);
                }
        }
@@ -33,6 +31,7 @@ class StorageManager
        /**
         * @brief Return current storage backend class
         * @return string
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
        public static function getBackend()
        {
@@ -54,12 +53,19 @@ class StorageManager
        /**
         * @brief Set current storage backend class
         *
-        * @param string  $class  Backend class name
+        * @param string $class Backend class name
+        * @return bool
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
        public static function setBackend($class)
        {
-               /// @todo Check that $class implements IStorage
+               if (!in_array('Friendica\Model\Storage\IStorage', class_implements($class))) {
+                       return false;
+               }
+
                Config::set('storage', 'class', $class);
+
+               return true;
        }
 
        /**
@@ -74,12 +80,12 @@ class StorageManager
        }
 
 
-
        /**
         * @brief Register a storage backend class
         *
-        * @param string  $name   User readable backend name
-        * @param string  $class  Backend class name
+        * @param string $name  User readable backend name
+        * @param string $class Backend class name
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
        public static function register($name, $class)
        {
@@ -93,7 +99,8 @@ class StorageManager
        /**
         * @brief Unregister a storage backend class
         *
-        * @param string  $name   User readable backend name
+        * @param string $name User readable backend name
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
        public static function unregister($name)
        {
@@ -109,19 +116,20 @@ class StorageManager
         * Copy existing data to destination storage and delete from source.
         * This method cannot move to legacy in-table `data` field.
         *
-        * @param string  $dest    Destination storage class name
-        * @param array   $tables  Tables to look in for resources. Optional, defaults to ['photo']
+        * @param string     $dest    Destination storage class name
+        * @param array|null $tables  Tables to look in for resources. Optional, defaults to ['photo', 'attach']
         *
-        * @retur int Number of moved resources
+        * @throws \Exception
+        * @return int Number of moved resources
         */
        public static function move($dest, $tables = null)
        {
                if (is_null($dest) || empty($dest)) {
-                       throw Exception('Can\'t move to NULL storage backend');
+                       throw new \Exception('Can\'t move to NULL storage backend');
                }
                
                if (is_null($tables)) {
-                       $tables = ['photo'];
+                       $tables = ['photo', 'attach'];
                }
 
                $moved = 0;
@@ -134,9 +142,10 @@ class StorageManager
                        );
 
                        if (DBA::isResult($rr)) {
-                               while($r = $rr->fetch()) {
+                               while($r = DBA::fetch($rr)) {
                                        $id = $r['id'];
                                        $data = $r['data'];
+                                       /** @var IStorage $backendClass */
                                        $backendClass = $r['backend-class'];
                                        $backendRef = $r['backend-ref'];
                                        if (!is_null($backendClass) && $backendClass !== '') {
@@ -145,6 +154,7 @@ class StorageManager
                                        }
                                        
                                        Logger::log("save data to new backend " . $dest);
+                                       /** @var IStorage $dest */
                                        $ref = $dest::put($data);
                                        Logger::log("saved data as " . $ref);