]> git.mxchange.org Git - friendica.git/blob - src/Core/Console/Storage.php
9db2a123f2d335087ca910ea6a8b1a3c6a1fedb6
[friendica.git] / src / Core / Console / Storage.php
1 <?php
2
3 namespace Friendica\Core\Console;
4
5 use Asika\SimpleConsole\CommandArgsException;
6 use Friendica\Core\StorageManager;
7
8 /**
9  * @brief tool to manage storage backend and stored data from CLI
10  *
11  */
12 class Storage extends \Asika\SimpleConsole\Console
13 {
14         protected $helpOptions = ['h', 'help', '?'];
15
16         protected function getHelp()
17         {
18                 $help = <<<HELP
19 console storage - manage storage backend and stored data
20 Synopsis
21     bin/console storage [-h|--help|-?] [-v]
22     bin/console storage list
23     bin/console storage set <name>
24     bin/console storage move
25 HELP;
26                 return $help;
27         }
28
29         protected function doExecute()
30         {
31                 $a = \Friendica\BaseObject::getApp();
32
33                 if ($this->getOption('v')) {
34                         $this->out('Executable: ' . $this->executable);
35                         $this->out('Class: ' . __CLASS__);
36                         $this->out('Arguments: ' . var_export($this->args, true));
37                         $this->out('Options: ' . var_export($this->options, true));
38                 }
39
40                 if (count($this->args) == 0) {
41                         $this->out($this->getHelp());
42                         return -1;
43                 }
44
45                 switch($this->args[0]) {
46                 case 'list':
47                         return $this->do_list();
48                         break;
49                 case 'set':
50                         return $this->do_set();
51                         break;
52                 case 'move':
53                         return $this->do_move();
54                         break;
55                 }
56
57                 $this->out(sprintf('Invalid action "%s"', $this->args[0]));
58                 return -1;
59         }
60
61         protected function do_list()
62         {
63                 $rowfmt = ' %-3s | %-20s';
64                 $current = StorageManager::getBackend();
65                 $this->out(sprintf($rowfmt, 'Sel', 'Name'));
66                 $this->out('-----------------------');
67                 $isregisterd = false;
68                 foreach(StorageManager::listBackends() as $name => $class) {
69                         $issel = ' ';
70                         if ($current === $class) {
71                                 $issel = '*';
72                                 $isregisterd = true;
73                         };
74                         $this->out(sprintf($rowfmt, $issel , $name ));
75                 }
76
77                 if ($current === '') {
78                         $this->out();
79                         $this->out('This sistem is using legacy storage system');
80                 }
81                 if ($current !== '' && !$isregisterd) {
82                         $this->out();
83                         $this->out('The current storage class (' . $current . ') is not registered!');
84                 }
85                 return 0;
86         }
87
88         protected function do_set()
89         {
90                 if (count($this->args) !== 2) {
91                         throw new CommandArgsException('Invalid arguments');
92                 }
93
94                 $name = $this->args[1];
95                 $class = StorageManager::getByName($name);
96
97                 if ($class === "") {
98                         $this->out($name . ' is not a registered backend.');
99                         return -1;
100                 }
101
102                 StorageManager::setBackend($class);
103                 return 0;
104         }
105
106         protected function do_move()
107         {
108                 if (count($this->args) !== 1) {
109                         throw new CommandArgsException('Invalid arguments');
110                 }
111
112                 $current = StorageManager::getBackend();
113                 $r = StorageManager::move($current);
114                 $this->out(sprintf("Moved %d files", $r));
115         }
116 }