]> git.mxchange.org Git - friendica.git/blob - src/Core/Console.php
e1654fbef68a08f8fc0828a6e2cd01098708f08a
[friendica.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         maintenance            Set maintenance mode for this node
42         newpassword            Set a new password for a given user
43         php2po                 Generate a messages.po file from a strings.php file
44         po2php                 Generate a strings.php file from a messages.po file
45         typo                   Checks for parse errors in Friendica files
46         postupdate             Execute pending post update scripts (can last days)
47         serverblock            Manage blocked servers
48         storage                Manage storage backend
49
50 Options:
51         -h|--help|-? Show help information
52         -v           Show more debug information.
53 HELP;
54                 return $help;
55         }
56
57         protected $subConsoles = [
58                 'cache'                  => Friendica\Console\Cache::class,
59                 'config'                 => Friendica\Console\Config::class,
60                 'createdoxygen'          => Friendica\Console\CreateDoxygen::class,
61                 'docbloxerrorchecker'    => Friendica\Console\DocBloxErrorChecker::class,
62                 'dbstructure'            => Friendica\Console\DatabaseStructure::class,
63                 'extract'                => Friendica\Console\Extract::class,
64                 'globalcommunityblock'   => Friendica\Console\GlobalCommunityBlock::class,
65                 'globalcommunitysilence' => Friendica\Console\GlobalCommunitySilence::class,
66                 'archivecontact'         => Friendica\Console\ArchiveContact::class,
67                 'autoinstall'            => Friendica\Console\AutomaticInstallation::class,
68                 'maintenance'            => Friendica\Console\Maintenance::class,
69                 'newpassword'            => Friendica\Console\NewPassword::class,
70                 'php2po'                 => Friendica\Console\PhpToPo::class,
71                 'po2php'                 => Friendica\Console\PoToPhp::class,
72                 'typo'                   => Friendica\Console\Typo::class,
73                 'postupdate'             => Friendica\Console\PostUpdate::class,
74                 'serverblock'            => Friendica\Console\ServerBlock::class,
75                 'storage'                => Friendica\Console\Storage::class,
76         ];
77
78         /**
79          * CliInput Friendica constructor.
80          *
81          * @param Dice $dice The DI library
82          * @param array $argv
83          */
84         public function __construct(Dice $dice, array $argv = null)
85         {
86                 parent::__construct($argv);
87
88                 $this->dice = $dice;
89         }
90
91         protected function doExecute()
92         {
93                 if ($this->getOption('v')) {
94                         $this->out('Executable: ' . $this->executable);
95                         $this->out('Arguments: ' . var_export($this->args, true));
96                         $this->out('Options: ' . var_export($this->options, true));
97                 }
98
99                 $subHelp = false;
100                 $command = null;
101
102                 if ($this->getOption('version')) {
103                         $this->out('Friendica Console version ' . FRIENDICA_VERSION);
104
105                         return 0;
106                 } elseif ((count($this->options) === 0 || $this->getOption($this->customHelpOptions) === true || $this->getOption($this->customHelpOptions) === 1) && count($this->args) === 0
107                 ) {
108                 } elseif (count($this->args) >= 2 && $this->getArgument(0) == 'help') {
109                         $command = $this->getArgument(1);
110                         $subHelp = true;
111                         array_shift($this->args);
112                         array_shift($this->args);
113                 } elseif (count($this->args) >= 1) {
114                         $command = $this->getArgument(0);
115                         array_shift($this->args);
116                 }
117
118                 if (is_null($command)) {
119                         $this->out($this->getHelp());
120                         return 0;
121                 }
122
123                 $console = $this->getSubConsole($command);
124
125                 if ($subHelp) {
126                         $console->setOption($this->customHelpOptions, true);
127                 }
128
129                 return $console->execute();
130         }
131
132         private function getSubConsole($command)
133         {
134                 if ($this->getOption('v')) {
135                         $this->out('Command: ' . $command);
136                 }
137
138                 if (!isset($this->subConsoles[$command])) {
139                         throw new \Asika\SimpleConsole\CommandArgsException('Command ' . $command . ' doesn\'t exist');
140                 }
141
142                 $subargs = $this->args;
143                 array_unshift($subargs, $this->executable);
144
145                 $className = $this->subConsoles[$command];
146
147                 Friendica\BaseObject::setDependencyInjection($this->dice);
148
149                 /** @var Console $subconsole */
150                 $subconsole = $this->dice->create($className, [$subargs]);
151
152                 foreach ($this->options as $name => $value) {
153                         $subconsole->setOption($name, $value);
154                 }
155
156                 return $subconsole;
157         }
158
159 }