-
Notifications
You must be signed in to change notification settings - Fork 673
/
Cli.php
159 lines (133 loc) · 3.18 KB
/
Cli.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
namespace Elgg;
use Elgg\Cli\Application as CliApplication;
use Elgg\Cli\BaseCommand;
use Elgg\Cli\Command;
use Elgg\Traits\Loggable;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
/**
* CLI bootstrap
*/
class Cli {
use Loggable;
/**
* @var CliApplication
*/
protected $console;
/**
* Constructor
*
* @param EventsService $events Events service
* @param InputInterface $input Console input
* @param OutputInterface $output Console output
*/
public function __construct(
protected EventsService $events,
protected InputInterface $input,
protected OutputInterface $output
) {
$console = new \Elgg\Cli\Application('Elgg', elgg_get_release());
$console->setup($input, $output);
$this->console = $console;
}
/**
* Add CLI tools to the console application
*
* @return void
*/
protected function bootstrap(): void {
$commands = array_merge($this->getCoreCommands(), $this->getPluginCommands());
$commands = $this->events->triggerResults('commands', 'cli', [], $commands);
foreach ($commands as $command) {
$this->add($command);
}
}
/**
* Returns the core cli commands
*
* @return array
*/
protected function getCoreCommands(): array {
$conf = \Elgg\Project\Paths::elgg() . 'engine/cli_commands.php';
return \Elgg\Includer::includeFile($conf);
}
/**
* Returns the cli commands registered in plugins
*
* @return array
*/
protected function getPluginCommands(): array {
$return = [];
$plugins = elgg_get_plugins('active');
foreach ($plugins as $plugin) {
$plugin_commands = $plugin->getStaticConfig('cli_commands', []);
if (empty($plugin_commands)) {
continue;
}
$return = array_merge($return, $plugin_commands);
}
return $return;
}
/**
* Add a new CLI command
*
* @param string $command Command class
* Must extend \Elgg\Cli\BaseCommand
*
* @return void
*/
public function add(string $command): void {
if (!class_exists($command)) {
return;
}
if (!is_subclass_of($command, BaseCommand::class)) {
return;
}
$command = new $command();
$command->setLogger($this->getLogger());
if (!is_subclass_of($command, Command::class)) {
$this->console->add($command);
return;
}
// Execute command as a given user
$command->addOption('as', 'u', InputOption::VALUE_OPTIONAL,
elgg_echo('cli:option:as')
);
// Change language
$command->addOption('language', null, InputOption::VALUE_OPTIONAL,
elgg_echo('cli:option:language')
);
$this->console->add($command);
}
/**
* Bootstrap and run console application
*
* @param bool $bootstrap Is bootstrap needed?
*
* @return void
*/
public function run(bool $bootstrap = true): void {
if ($bootstrap) {
$this->bootstrap();
}
$this->console->run($this->input, $this->output);
}
/**
* Returns console input
*
* @return InputInterface
*/
public function getInput(): InputInterface {
return $this->input;
}
/**
* Returns console output
*
* @return OutputInterface
*/
public function getOutput(): OutputInterface {
return $this->output;
}
}