Merge pull request #8019 from nupplaphil/task/replace_getClass
[friendica.git/.git] / src / Core / Console.php
1 <?php
2
3 namespace Friendica\Core;
4
5 use Dice\Dice;
6 use Friendica;
7
8 /**
9  * Description of Console
10  *
11  * @author Hypolite Petovan <hypolite@mrpetovan.com>
12  */
13 class Console extends \Asika\SimpleConsole\Console
14 {
15         // Disables the default help handling
16         protected $helpOptions = [];
17         protected $customHelpOptions = ['h', 'help', '?'];
18
19         /**
20          * @var Dice The DI library
21          */
22         protected $dice;
23
24         protected function getHelp()
25         {
26                 $help = <<<HELP
27 Usage: bin/console [--version] [-h|--help|-?] <command> [<args>] [-v]
28
29 Commands:
30         cache                  Manage node cache
31         config                 Edit site config
32         createdoxygen          Generate Doxygen headers
33         dbstructure            Do database updates
34         docbloxerrorchecker    Check the file tree for DocBlox errors
35         extract                Generate translation string file for the Friendica project (deprecated)
36         globalcommunityblock   Block remote profile from interacting with this node
37         globalcommunitysilence Silence remote profile from global community page
38         archivecontact         Archive a contact when you know that it isn't existing anymore
39         help                   Show help about a command, e.g (bin/console help config)
40         autoinstall            Starts automatic installation of friendica based on values from htconfig.php
41         lock                   Edit site locks
42         maintenance            Set maintenance mode for this node
43         newpassword            Set a new password for a given user
44         php2po                 Generate a messages.po file from a strings.php file
45         po2php                 Generate a strings.php file from a messages.po file
46         typo                   Checks for parse errors in Friendica files
47         postupdate             Execute pending post update scripts (can last days)
48         serverblock            Manage blocked servers
49         storage                Manage storage backend
50
51 Options:
52         -h|--help|-? Show help information
53         -v           Show more debug information.
54 HELP;
55                 return $help;
56         }
57
58         protected $subConsoles = [
59                 'cache'                  => Friendica\Console\Cache::class,
60                 'config'                 => Friendica\Console\Config::class,
61                 'createdoxygen'          => Friendica\Console\CreateDoxygen::class,
62                 'docbloxerrorchecker'    => Friendica\Console\DocBloxErrorChecker::class,
63                 'dbstructure'            => Friendica\Console\DatabaseStructure::class,
64                 'extract'                => Friendica\Console\Extract::class,
65                 'globalcommunityblock'   => Friendica\Console\GlobalCommunityBlock::class,
66                 'globalcommunitysilence' => Friendica\Console\GlobalCommunitySilence::class,
67                 'archivecontact'         => Friendica\Console\ArchiveContact::class,
68                 'autoinstall'            => Friendica\Console\AutomaticInstallation::class,
69                 'lock'                   => Friendica\Console\Lock::class,
70                 'maintenance'            => Friendica\Console\Maintenance::class,
71                 'newpassword'            => Friendica\Console\NewPassword::class,
72                 'php2po'                 => Friendica\Console\PhpToPo::class,
73                 'po2php'                 => Friendica\Console\PoToPhp::class,
74                 'typo'                   => Friendica\Console\Typo::class,
75                 'postupdate'             => Friendica\Console\PostUpdate::class,
76                 'serverblock'            => Friendica\Console\ServerBlock::class,
77                 'storage'                => Friendica\Console\Storage::class,
78         ];
79
80         /**
81          * CliInput Friendica constructor.
82          *
83          * @param Dice $dice The DI library
84          * @param array $argv
85          */
86         public function __construct(Dice $dice, array $argv = null)
87         {
88                 parent::__construct($argv);
89
90                 $this->dice = $dice;
91         }
92
93         protected function doExecute()
94         {
95                 if ($this->getOption('v')) {
96                         $this->out('Executable: ' . $this->executable);
97                         $this->out('Arguments: ' . var_export($this->args, true));
98                         $this->out('Options: ' . var_export($this->options, true));
99                 }
100
101                 $subHelp = false;
102                 $command = null;
103
104                 if ($this->getOption('version')) {
105                         $this->out('Friendica Console version ' . FRIENDICA_VERSION);
106
107                         return 0;
108                 } elseif ((count($this->options) === 0 || $this->getOption($this->customHelpOptions) === true || $this->getOption($this->customHelpOptions) === 1) && count($this->args) === 0
109                 ) {
110                 } elseif (count($this->args) >= 2 && $this->getArgument(0) == 'help') {
111                         $command = $this->getArgument(1);
112                         $subHelp = true;
113                         array_shift($this->args);
114                         array_shift($this->args);
115                 } elseif (count($this->args) >= 1) {
116                         $command = $this->getArgument(0);
117                         array_shift($this->args);
118                 }
119
120                 if (is_null($command)) {
121                         $this->out($this->getHelp());
122                         return 0;
123                 }
124
125                 $console = $this->getSubConsole($command);
126
127                 if ($subHelp) {
128                         $console->setOption($this->customHelpOptions, true);
129                 }
130
131                 return $console->execute();
132         }
133
134         private function getSubConsole($command)
135         {
136                 if ($this->getOption('v')) {
137                         $this->out('Command: ' . $command);
138                 }
139
140                 if (!isset($this->subConsoles[$command])) {
141                         throw new \Asika\SimpleConsole\CommandArgsException('Command ' . $command . ' doesn\'t exist');
142                 }
143
144                 $subargs = $this->args;
145                 array_unshift($subargs, $this->executable);
146
147                 $className = $this->subConsoles[$command];
148
149                 Friendica\DI::init($this->dice);
150
151                 /** @var Console $subconsole */
152                 $subconsole = $this->dice->create($className, [$subargs]);
153
154                 foreach ($this->options as $name => $value) {
155                         $subconsole->setOption($name, $value);
156                 }
157
158                 return $subconsole;
159         }
160
161 }