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