Elgg  Version 5.1
Cli.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg;
4 
5 use Elgg\Cli\Application as CliApplication;
12 
16 class Cli {
17 
18  use Loggable;
19 
23  protected $console;
24 
28  protected $events;
29 
33  protected $input;
34 
38  protected $output;
39 
47  public function __construct(
48  EventsService $events,
49  InputInterface $input,
50  OutputInterface $output
51  ) {
52 
53  $console = new \Elgg\Cli\Application('Elgg', elgg_get_release());
54  $console->setup($input, $output);
55 
56  $this->console = $console;
57  $this->events = $events;
58  $this->input = $input;
59  $this->output = $output;
60  }
61 
67  protected function bootstrap() {
68  $commands = array_merge($this->getCoreCommands(), $this->getPluginCommands());
69  $commands = $this->events->triggerResults('commands', 'cli', [], $commands);
70 
71  foreach ($commands as $command) {
72  $this->add($command);
73  }
74  }
75 
81  protected function getCoreCommands() {
82  $conf = \Elgg\Project\Paths::elgg() . 'engine/cli_commands.php';
83  return \Elgg\Includer::includeFile($conf);
84  }
85 
91  protected function getPluginCommands() {
92  $return = [];
93 
94  $plugins = elgg_get_plugins('active');
95  foreach ($plugins as $plugin) {
96  $plugin_commands = $plugin->getStaticConfig('cli_commands', []);
97  if (empty($plugin_commands)) {
98  continue;
99  }
100 
101  $return = array_merge($return, $plugin_commands);
102  }
103 
104  return $return;
105  }
106 
115  public function add($command) {
116  if (!class_exists($command)) {
117  return;
118  }
119 
120  if (!is_subclass_of($command, BaseCommand::class)) {
121  return;
122  }
123 
124  $command = new $command();
125 
126  $command->setLogger($this->getLogger());
127 
128  if (!is_subclass_of($command, Command::class)) {
129  $this->console->add($command);
130  return;
131  }
132 
133  // Execute command as a given user
134  $command->addOption('as', 'u', InputOption::VALUE_OPTIONAL,
135  elgg_echo('cli:option:as')
136  );
137 
138  // Change language
139  $command->addOption('language', null, InputOption::VALUE_OPTIONAL,
140  elgg_echo('cli:option:language')
141  );
142 
143  $this->console->add($command);
144  }
145 
153  public function run(bool $bootstrap = true) {
154  if ($bootstrap) {
155  $this->bootstrap();
156  }
157 
158  $this->console->run($this->input, $this->output);
159  }
160 
166  public function getInput() {
167  return $this->input;
168  }
169 
175  public function getOutput() {
176  return $this->output;
177  }
178 }
elgg_get_release()
Get the current Elgg release.
elgg_get_plugins(string $status= 'active')
Returns an ordered list of plugins.
Definition: plugins.php:55
$plugin
static elgg()
Get the Elgg codebase path with "/".
Definition: Paths.php:44
getOutput()
Returns console output.
Definition: Cli.php:175
elgg_echo(string $message_key, array $args=[], string $language= '')
Elgg language module Functions to manage language and translations.
Definition: languages.php:17
Events service.
Wrapper for console application.
Definition: Application.php:11
$plugins
Definition: categories.php:3
__construct(EventsService $events, InputInterface $input, OutputInterface $output)
Constructor.
Definition: Cli.php:47
trait Loggable
Enables adding a logger.
Definition: Loggable.php:14
getCoreCommands()
Returns the core cli commands.
Definition: Cli.php:81
$events
Definition: Cli.php:28
CLI bootstrap.
Definition: Cli.php:16
getInput()
Returns console input.
Definition: Cli.php:166
$output
Definition: Cli.php:38
getLogger()
Returns logger.
Definition: Loggable.php:37
$input
Definition: Cli.php:33
$input
Form field view.
Definition: field.php:13
getPluginCommands()
Returns the cli commands registered in plugins.
Definition: Cli.php:91
run(bool $bootstrap=true)
Bootstrap and run console application.
Definition: Cli.php:153
add($command)
Add a new CLI command.
Definition: Cli.php:115
$output
Definition: download.php:9
bootstrap()
Add CLI tools to the console application.
Definition: Cli.php:67
$console
Definition: Cli.php:23