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