]> git.mxchange.org Git - friendica.git/blob - src/Core/Console.php
Add Console classes
[friendica.git] / src / Core / Console.php
1 <?php\r
2 \r
3 namespace Friendica\Core;\r
4 \r
5 /**\r
6  * Description of Console\r
7  *\r
8  * @author Hypolite Petovan <mrpetovan@gmail.com>\r
9  */\r
10 class Console extends \Asika\SimpleConsole\Console\r
11 {\r
12         // Disables the default help handling\r
13         protected $helpOptions = [];\r
14         protected $customHelpOptions = ['h', 'help', '?'];\r
15 \r
16         protected function getHelp()\r
17         {\r
18 \r
19 \r
20                 $help = <<<HELP\r
21 Usage: bin/console [--version] [-h|--help|-?] <command> [<args>] [-v]\r
22 \r
23 Commands:\r
24         config               Edit site config\r
25         createdoxygen        Generate Doxygen headers\r
26         docbloxerrorchecker  Checks the file tree for DocBlox errors\r
27         globalcommunityblock Silence remote profile from global community page\r
28         help                 Show help about a command, e.g (bin/console help config)\r
29 \r
30 Options:\r
31         -h|--help|-? Show help information\r
32         -v           Show more debug information.\r
33 HELP;\r
34                 return $help;\r
35         }\r
36 \r
37         protected function doExecute()\r
38         {\r
39                 if ($this->getOption('v')) {\r
40                         $this->out('Executable: ' . $this->executable);\r
41                         $this->out('Arguments: ' . var_export($this->args, true));\r
42                         $this->out('Options: ' . var_export($this->options, true));\r
43                 }\r
44 \r
45                 $showHelp = false;\r
46                 $subHelp = false;\r
47                 $command = null;\r
48 \r
49                 if ($this->getOption('version')) {\r
50                         $this->out('Friendica Console version ' . FRIENDICA_VERSION);\r
51 \r
52                         return 0;\r
53                 } elseif ((count($this->options) === 0 || $this->getOption($this->customHelpOptions) === true || $this->getOption($this->customHelpOptions) === 1) && count($this->args) === 0\r
54                 ) {\r
55                         $showHelp = true;\r
56                 } elseif (count($this->args) >= 2 && $this->getArgument(0) == 'help') {\r
57                         $command = $this->getArgument(1);\r
58                         $subHelp = true;\r
59                         array_shift($this->args);\r
60                         array_shift($this->args);\r
61                 } elseif (count($this->args) >= 1) {\r
62                         $command = $this->getArgument(0);\r
63                         array_shift($this->args);\r
64                 }\r
65 \r
66                 if (is_null($command)) {\r
67                         $this->out($this->getHelp());\r
68                         return 0;\r
69                 }\r
70 \r
71                 $console = $this->getSubConsole($command);\r
72 \r
73                 if ($subHelp) {\r
74                         $console->setOption($this->customHelpOptions, true);\r
75                 }\r
76 \r
77                 return $console->execute();\r
78         }\r
79 \r
80         private function getSubConsole($command)\r
81         {\r
82                 if ($this->getOption('v')) {\r
83                         $this->out('Command: ' . $command);\r
84                 }\r
85 \r
86                 $subargs = $this->args;\r
87                 array_unshift($subargs, $this->executable);\r
88 \r
89                 $subconsole = null;\r
90 \r
91                 switch ($command) {\r
92                         case 'config' : $subconsole = new Console\Config($subargs);\r
93                                 break;\r
94                         case 'createdoxygen' : $subconsole = new Console\CreateDoxygen($subargs);\r
95                                 break;\r
96                         case 'docbloxerrorchecker' : $subconsole = new Console\DocBloxErrorChecker($subargs);\r
97                                 break;\r
98                         case 'globalcommunityblock': $subconsole = new Console\GlobalCommunityBlock($subargs);\r
99                                 break;\r
100                         default:\r
101                                 throw new \Asika\SimpleConsole\CommandArgsException('Command ' . $command . ' doesn\'t exist');\r
102                 }\r
103 \r
104                 foreach ($this->options as $name => $value) {\r
105                         $subconsole->setOption($name, $value);\r
106                 }\r
107 \r
108                 return $subconsole;\r
109         }\r
110 \r
111 }\r