Add 'storage' console command

- list registered backend
- set backend
This commit is contained in:
fabrixxm 2018-11-29 12:57:57 +01:00 committed by Hypolite Petovan
parent 6a0ed7c298
commit 4d9d62bcdb
3 changed files with 121 additions and 9 deletions

View File

@ -30,6 +30,7 @@ class Console extends \Asika\SimpleConsole\Console
'po2php' => __NAMESPACE__ . '\Console\PoToPhp', 'po2php' => __NAMESPACE__ . '\Console\PoToPhp',
'typo' => __NAMESPACE__ . '\Console\Typo', 'typo' => __NAMESPACE__ . '\Console\Typo',
'postupdate' => __NAMESPACE__ . '\Console\PostUpdate', 'postupdate' => __NAMESPACE__ . '\Console\PostUpdate',
'storage' => __NAMESPACE__ . '\Console\Storage',
]; ];
protected function getHelp() protected function getHelp()
@ -55,6 +56,7 @@ Commands:
po2php Generate a strings.php file from a messages.po file po2php Generate a strings.php file from a messages.po file
typo Checks for parse errors in Friendica files typo Checks for parse errors in Friendica files
postupdate Execute pending post update scripts (can last days) postupdate Execute pending post update scripts (can last days)
storage Manage storage backend
Options: Options:
-h|--help|-? Show help information -h|--help|-? Show help information

View File

@ -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()
{
}
}

View File

@ -14,17 +14,17 @@ use Friendica\Core\Config;
*/ */
class StorageManager class StorageManager
{ {
private static $default_storages = [ private static $default_backends = [
'Filesystem' => \Friendica\Model\Storage\Filesystem::class, 'Filesystem' => \Friendica\Model\Storage\Filesystem::class,
'Database' => \Friendica\Model\Storage\Database::class, 'Database' => \Friendica\Model\Storage\Database::class,
]; ];
private static $storages = []; private static $backends = [];
private static function setup() private static function setup()
{ {
if (count(self::$storages)==0) { if (count(self::$backends)==0) {
self::$storage = Config::get('storage', 'backends', self::$default_storages); self::$backends = Config::get('storage', 'backends', self::$default_backends);
} }
} }
@ -46,7 +46,7 @@ class StorageManager
public static function getByName($name) public static function getByName($name)
{ {
self::setup(); self::setup();
return defaults(self::$storages, $name, ''); return defaults(self::$backends, $name, '');
} }
/** /**
@ -83,8 +83,8 @@ class StorageManager
{ {
/// @todo Check that $class implements IStorage /// @todo Check that $class implements IStorage
self::setup(); self::setup();
self::$storages[$name] = $class; self::$backends[$name] = $class;
Config::set('storage', 'backends', self::$storages); Config::set('storage', 'backends', self::$backends);
} }
@ -96,7 +96,7 @@ class StorageManager
public static function unregister($class) public static function unregister($class)
{ {
self::setup(); self::setup();
unset(self::$storages[$name]); unset(self::$backends[$name]);
Config::set('storage', 'backends', self::$storages); Config::set('storage', 'backends', self::$backends);
} }
} }