]> git.mxchange.org Git - friendica.git/blob - src/Core/Console.php
Merge pull request #4674 from annando/acl-fix
[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                 'maintenance'            => __NAMESPACE__ . '\Console\Maintenance',
25                 'php2po'                 => __NAMESPACE__ . '\Console\PhpToPo',
26                 'po2php'                 => __NAMESPACE__ . '\Console\PoToPhp',
27                 'typo'                   => __NAMESPACE__ . '\Console\Typo',
28         ];
29
30         protected function getHelp()
31         {
32                 $help = <<<HELP
33 Usage: bin/console [--version] [-h|--help|-?] <command> [<args>] [-v]
34
35 Commands:
36         config                 Edit site config
37         createdoxygen          Generate Doxygen headers
38         dbstructure            Do database updates
39         docbloxerrorchecker    Check the file tree for DocBlox errors
40         extract                Generate translation string file for the Friendica project (deprecated)
41         globalcommunityblock   Block remote profile from interacting with this node
42         globalcommunitysilence Silence remote profile from global community page
43         help                   Show help about a command, e.g (bin/console help config)
44         maintenance            Set maintenance mode for this node
45         php2po                 Generate a messages.po file from a strings.php file
46         po2php                 Generate a strings.php file from a messages.po file
47         typo                   Checks for parse errors in Friendica files
48
49 Options:
50         -h|--help|-? Show help information
51         -v           Show more debug information.
52 HELP;
53                 return $help;
54         }
55
56         protected function doExecute()
57         {
58                 if ($this->getOption('v')) {
59                         $this->out('Executable: ' . $this->executable);
60                         $this->out('Arguments: ' . var_export($this->args, true));
61                         $this->out('Options: ' . var_export($this->options, true));
62                 }
63
64                 $showHelp = false;
65                 $subHelp = false;
66                 $command = null;
67
68                 if ($this->getOption('version')) {
69                         $this->out('Friendica Console version ' . FRIENDICA_VERSION);
70
71                         return 0;
72                 } elseif ((count($this->options) === 0 || $this->getOption($this->customHelpOptions) === true || $this->getOption($this->customHelpOptions) === 1) && count($this->args) === 0
73                 ) {
74                         $showHelp = true;
75                 } elseif (count($this->args) >= 2 && $this->getArgument(0) == 'help') {
76                         $command = $this->getArgument(1);
77                         $subHelp = true;
78                         array_shift($this->args);
79                         array_shift($this->args);
80                 } elseif (count($this->args) >= 1) {
81                         $command = $this->getArgument(0);
82                         array_shift($this->args);
83                 }
84
85                 if (is_null($command)) {
86                         $this->out($this->getHelp());
87                         return 0;
88                 }
89
90                 $console = $this->getSubConsole($command);
91
92                 if ($subHelp) {
93                         $console->setOption($this->customHelpOptions, true);
94                 }
95
96                 return $console->execute();
97         }
98
99         private function getSubConsole($command)
100         {
101                 if ($this->getOption('v')) {
102                         $this->out('Command: ' . $command);
103                 }
104
105                 if (!isset($this->subConsoles[$command])) {
106                         throw new \Asika\SimpleConsole\CommandArgsException('Command ' . $command . ' doesn\'t exist');
107                 }
108
109                 $subargs = $this->args;
110                 array_unshift($subargs, $this->executable);
111
112                 $className = $this->subConsoles[$command];
113
114                 $subconsole = new $className($subargs);
115
116                 foreach ($this->options as $name => $value) {
117                         $subconsole->setOption($name, $value);
118                 }
119
120                 return $subconsole;
121         }
122
123 }