]> git.mxchange.org Git - friendica.git/commitdiff
Add 'storage' console command
authorfabrixxm <fabrix.xm@gmail.com>
Thu, 29 Nov 2018 11:57:57 +0000 (12:57 +0100)
committerHypolite Petovan <hypolite@mrpetovan.com>
Mon, 21 Jan 2019 14:11:34 +0000 (09:11 -0500)
- list registered backend
- set backend

src/Core/Console.php
src/Core/Console/Storage.php [new file with mode: 0644]
src/Core/StorageManager.php

index 9edc12b64c93b690646f75786589f52a16c0e136..2cca4f15bb790d914b6ca6f6d26663a8444e48c5 100644 (file)
@@ -30,6 +30,7 @@ class Console extends \Asika\SimpleConsole\Console
                'po2php'                 => __NAMESPACE__ . '\Console\PoToPhp',
                'typo'                   => __NAMESPACE__ . '\Console\Typo',
                'postupdate'             => __NAMESPACE__ . '\Console\PostUpdate',
+               'storage'                => __NAMESPACE__ . '\Console\Storage',
        ];
 
        protected function getHelp()
@@ -55,6 +56,7 @@ Commands:
        po2php                 Generate a strings.php file from a messages.po file
        typo                   Checks for parse errors in Friendica files
        postupdate             Execute pending post update scripts (can last days)
+       storage                Manage storage backend
 
 Options:
        -h|--help|-? Show help information
diff --git a/src/Core/Console/Storage.php b/src/Core/Console/Storage.php
new file mode 100644 (file)
index 0000000..a03743f
--- /dev/null
@@ -0,0 +1,110 @@
+<?php
+
+namespace Friendica\Core\Console;
+
+use Asika\SimpleConsole\CommandArgsException;
+use Friendica\Core\StorageManager;
+
+/**
+ * @brief tool to manage storage backend and stored data from CLI
+ *
+ */
+class Storage extends \Asika\SimpleConsole\Console
+{
+       protected $helpOptions = ['h', 'help', '?'];
+
+       protected function getHelp()
+       {
+               $help = <<<HELP
+console storage - manage storage backend and stored data
+Synopsis
+    bin/console storage [-h|--help|-?] [-v]
+    bin/console storage list
+    bin/console storage set <name>
+    bin/console storage move
+HELP;
+               return $help;
+       }
+
+       protected function doExecute()
+       {
+               $a = \Friendica\BaseObject::getApp();
+
+               if ($this->getOption('v')) {
+                       $this->out('Executable: ' . $this->executable);
+                       $this->out('Class: ' . __CLASS__);
+                       $this->out('Arguments: ' . var_export($this->args, true));
+                       $this->out('Options: ' . var_export($this->options, true));
+               }
+
+               if (count($this->args) == 0) {
+                       $this->out($this->getHelp());
+                       return -1;
+               }
+
+               switch($this->args[0]) {
+               case 'list':
+                       return $this->do_list();
+                       break;
+               case 'set':
+                       return $this->do_set();
+                       break;
+               case 'move':
+                       return $this->do_move();
+                       break;
+               }
+
+               $this->out(sprintf('Invalid action "%s"', $this->args[0]));
+               return -1;
+       }
+
+       protected function do_list()
+       {
+               $rowfmt = ' %-3s | %-20s';
+               $current = StorageManager::getBackend();
+               $this->out(sprintf($rowfmt, 'Sel', 'Name'));
+               $this->out('-----------------------');
+               $isregisterd = false;
+               foreach(StorageManager::listBackends() as $name => $class) {
+                       $issel = ' ';
+                       if ($current === $class) {
+                               $issel = '*';
+                               $isregisterd = true;
+                       };
+                       $this->out(sprintf($rowfmt, $issel , $name ));
+               }
+
+               if ($current === '') {
+                       $this->out();
+                       $this->out('This sistem is using legacy storage system');
+               }
+               if ($current !== '' && !$isregisterd) {
+                       $this->out();
+                       $this->out('The current storage class (' . $current . ') is not registered!');
+               }
+               return 0;
+       }
+
+       protected function do_set()
+       {
+               if (count($this->args) !== 2) {
+                       throw new CommandArgsException('Invalid arguments');
+               }
+
+               $name = $this->args[1];
+               $class = StorageManager::getByName($name);
+
+               if ($class === "") {
+                       $this->out($name . ' is not a registered backend.');
+                       return -1;
+               }
+
+               StorageManager::setBackend($class);
+               return 0;
+       }
+
+       protected function do_move()
+       {
+
+       }
+}
index 6f3e98ae7e2e57d77004b858ae3015764878e462..5c14fdbf3388646c954441b30d8aadce96ebe753 100644 (file)
@@ -14,17 +14,17 @@ use Friendica\Core\Config;
  */
 class StorageManager
 {
-       private static $default_storages = [
+       private static $default_backends = [
                'Filesystem' => \Friendica\Model\Storage\Filesystem::class,
                'Database' => \Friendica\Model\Storage\Database::class,
        ];
 
-       private static $storages = [];
+       private static $backends = [];
 
        private static function setup()
        {
-               if (count(self::$storages)==0) {
-                       self::$storage = Config::get('storage', 'backends', self::$default_storages);
+               if (count(self::$backends)==0) {
+                       self::$backends = Config::get('storage', 'backends', self::$default_backends);
                }
        }
 
@@ -46,7 +46,7 @@ class StorageManager
        public static function getByName($name)
        {
                self::setup();
-               return defaults(self::$storages, $name, '');
+               return defaults(self::$backends, $name, '');
        }
 
        /**
@@ -83,8 +83,8 @@ class StorageManager
        {
                /// @todo Check that $class implements IStorage
                self::setup();
-               self::$storages[$name] = $class;
-               Config::set('storage', 'backends', self::$storages);
+               self::$backends[$name] = $class;
+               Config::set('storage', 'backends', self::$backends);
        }
 
 
@@ -96,7 +96,7 @@ class StorageManager
        public static function unregister($class)
        {
                self::setup();
-               unset(self::$storages[$name]);
-               Config::set('storage', 'backends', self::$storages);
+               unset(self::$backends[$name]);
+               Config::set('storage', 'backends', self::$backends);
        }
 }
\ No newline at end of file