Elgg  Version 4.3
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 $hooks;
29 
33  protected $input;
34 
38  protected $output;
39 
47  public function __construct(
48  PluginHooksService $hooks,
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->hooks = $hooks;
58  $this->input = $input;
59  $this->output = $output;
60  }
61 
66  protected function bootstrap() {
67  $commands = array_merge($this->getCoreCommands(), $this->getPluginCommands());
68  $commands = $this->hooks->trigger('commands', 'cli', null, $commands);
69 
70  foreach ($commands as $command) {
71  $this->add($command);
72  }
73  }
74 
79  protected function getCoreCommands() {
80  $conf = \Elgg\Project\Paths::elgg() . 'engine/cli_commands.php';
81  return \Elgg\Includer::includeFile($conf);
82  }
83 
88  protected function getPluginCommands() {
89  $return = [];
90 
91  $plugins = elgg_get_plugins('active');
92  foreach ($plugins as $plugin) {
93  $plugin_commands = $plugin->getStaticConfig('cli_commands', []);
94  if (empty($plugin_commands)) {
95  continue;
96  }
97 
98  $return = array_merge($return, $plugin_commands);
99  }
100 
101  return $return;
102  }
103 
112  public function add($command) {
113  if (!class_exists($command)) {
114  return;
115  }
116 
117  if (!is_subclass_of($command, BaseCommand::class)) {
118  return;
119  }
120 
121  $command = new $command();
122  /* @var $command BaseCommand */
123 
124  $command->setLogger($this->getLogger());
125 
126  if (!is_subclass_of($command, Command::class)) {
127  $this->console->add($command);
128  return;
129  }
130 
131  // Execute command as a given user
132  $command->addOption('as', 'u', InputOption::VALUE_OPTIONAL,
133  elgg_echo('cli:option:as')
134  );
135 
136  // Change language
137  $command->addOption('language', null, InputOption::VALUE_OPTIONAL,
138  elgg_echo('cli:option:language')
139  );
140 
141  $this->console->add($command);
142  }
143 
151  public function run(bool $bootstrap = true) {
152  if ($bootstrap) {
153  $this->bootstrap();
154  }
155  $this->console->run($this->input, $this->output);
156  }
157 
162  public function getInput() {
163  return $this->input;
164  }
165 
170  public function getOutput() {
171  return $this->output;
172  }
173 }
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
$hooks
Definition: Cli.php:28
getOutput()
Returns console output.
Definition: Cli.php:170
Wrapper for console application.
Definition: Application.php:11
$plugins
Definition: categories.php:3
elgg_echo($message_key, array $args=[], $language="")
Elgg language module Functions to manage language and translations.
Definition: languages.php:18
__construct(PluginHooksService $hooks, 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:79
CLI bootstrap.
Definition: Cli.php:16
getInput()
Returns console input.
Definition: Cli.php:162
$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:88
run(bool $bootstrap=true)
Bootstrap and run console application.
Definition: Cli.php:151
add($command)
Add a new CLI command.
Definition: Cli.php:112
$output
Definition: download.php:9
bootstrap()
Add CLI tools to the console application.
Definition: Cli.php:66
$console
Definition: Cli.php:23