From: fabrixxm Date: Thu, 29 Nov 2018 11:57:57 +0000 (+0100) Subject: Add 'storage' console command X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=4d9d62bcdbcbccb72562f58332f230df70ead796;p=friendica.git Add 'storage' console command - list registered backend - set backend --- diff --git a/src/Core/Console.php b/src/Core/Console.php index 9edc12b64c..2cca4f15bb 100644 --- a/src/Core/Console.php +++ b/src/Core/Console.php @@ -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 index 0000000000..a03743f253 --- /dev/null +++ b/src/Core/Console/Storage.php @@ -0,0 +1,110 @@ + + 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() + { + + } +} diff --git a/src/Core/StorageManager.php b/src/Core/StorageManager.php index 6f3e98ae7e..5c14fdbf33 100644 --- a/src/Core/StorageManager.php +++ b/src/Core/StorageManager.php @@ -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