Elgg  Version master
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 
32  public function __construct(
33  protected EventsService $events,
34  protected InputInterface $input,
35  protected OutputInterface $output
36  ) {
37  $console = new \Elgg\Cli\Application('Elgg', elgg_get_release());
38  $console->setup($input, $output);
39 
40  $this->console = $console;
41  }
42 
48  protected function bootstrap(): void {
49  $commands = array_merge($this->getCoreCommands(), $this->getPluginCommands());
50  $commands = $this->events->triggerResults('commands', 'cli', [], $commands);
51 
52  foreach ($commands as $command) {
53  $this->add($command);
54  }
55  }
56 
62  protected function getCoreCommands(): array {
63  $conf = \Elgg\Project\Paths::elgg() . 'engine/cli_commands.php';
64  return \Elgg\Includer::includeFile($conf);
65  }
66 
72  protected function getPluginCommands(): array {
73  $return = [];
74 
75  $plugins = elgg_get_plugins('active');
76  foreach ($plugins as $plugin) {
77  $plugin_commands = $plugin->getStaticConfig('cli_commands', []);
78  if (empty($plugin_commands)) {
79  continue;
80  }
81 
82  $return = array_merge($return, $plugin_commands);
83  }
84 
85  return $return;
86  }
87 
96  public function add(string $command): void {
97  if (!class_exists($command)) {
98  return;
99  }
100 
101  if (!is_subclass_of($command, BaseCommand::class)) {
102  return;
103  }
104 
105  $command = new $command();
106 
107  $command->setLogger($this->getLogger());
108 
109  if (!is_subclass_of($command, Command::class)) {
110  $this->console->add($command);
111  return;
112  }
113 
114  // Execute command as a given user
115  $command->addOption('as', 'u', InputOption::VALUE_OPTIONAL,
116  elgg_echo('cli:option:as')
117  );
118 
119  // Change language
120  $command->addOption('language', null, InputOption::VALUE_OPTIONAL,
121  elgg_echo('cli:option:language')
122  );
123 
124  $this->console->add($command);
125  }
126 
134  public function run(bool $bootstrap = true): void {
135  if ($bootstrap) {
136  $this->bootstrap();
137  }
138 
139  $this->console->run($this->input, $this->output);
140  }
141 
147  public function getInput(): InputInterface {
148  return $this->input;
149  }
150 
156  public function getOutput(): OutputInterface {
157  return $this->output;
158  }
159 }
elgg_get_release()
Get the current Elgg release.
elgg_get_plugins(string $status= 'active')
Returns an ordered list of plugins.
Definition: plugins.php:39
$plugin
__construct(protected EventsService $events, protected InputInterface $input, protected OutputInterface $output)
Constructor.
Definition: Cli.php:32
static elgg()
Get the Elgg codebase path with "/".
Definition: Paths.php:44
getOutput()
Returns console output.
Definition: Cli.php:156
c Accompany it with the information you received as to the offer to distribute corresponding source complete source code means all the source code for all modules it plus any associated interface definition plus the scripts used to control compilation and installation of the executable as a special the source code distributed need not include anything that is normally and so on of the operating system on which the executable unless that component itself accompanies the executable If distribution of executable or object code is made by offering access to copy from a designated then offering equivalent access to copy the source code from the same place counts as distribution of the source even though third parties are not compelled to copy the source along with the object code You may not or distribute the Program except as expressly provided under this License Any attempt otherwise to sublicense or distribute the Program is void
Definition: LICENSE.txt:215
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
trait Loggable
Enables adding a logger.
Definition: Loggable.php:14
getCoreCommands()
Returns the core cli commands.
Definition: Cli.php:62
add(string $command)
Add a new CLI command.
Definition: Cli.php:96
CLI bootstrap.
Definition: Cli.php:16
getInput()
Returns console input.
Definition: Cli.php:147
getLogger()
Returns logger.
Definition: Loggable.php:37
$input
Form field view.
Definition: field.php:13
getPluginCommands()
Returns the cli commands registered in plugins.
Definition: Cli.php:72
run(bool $bootstrap=true)
Bootstrap and run console application.
Definition: Cli.php:134
$output
Definition: download.php:9
bootstrap()
Add CLI tools to the console application.
Definition: Cli.php:48
$console
Definition: Cli.php:23