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;
8 use Elgg\Traits\Loggable;
9 use Symfony\Component\Console\Input\InputInterface;
10 use Symfony\Component\Console\Input\InputOption;
11 use Symfony\Component\Console\Output\OutputInterface;
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 }
return[ 'admin/delete_admin_notices'=>['access'=> 'admin'], 'admin/menu/save'=>['access'=> 'admin'], 'admin/plugins/activate'=>['access'=> 'admin'], 'admin/plugins/activate_all'=>['access'=> 'admin'], 'admin/plugins/deactivate'=>['access'=> 'admin'], 'admin/plugins/deactivate_all'=>['access'=> 'admin'], 'admin/plugins/set_priority'=>['access'=> 'admin'], 'admin/security/security_txt'=>['access'=> 'admin'], 'admin/security/settings'=>['access'=> 'admin'], 'admin/security/regenerate_site_secret'=>['access'=> 'admin'], 'admin/site/cache/invalidate'=>['access'=> 'admin'], 'admin/site/flush_cache'=>['access'=> 'admin'], 'admin/site/icons'=>['access'=> 'admin'], 'admin/site/set_maintenance_mode'=>['access'=> 'admin'], 'admin/site/set_robots'=>['access'=> 'admin'], 'admin/site/theme'=>['access'=> 'admin'], 'admin/site/unlock_upgrade'=>['access'=> 'admin'], 'admin/site/settings'=>['access'=> 'admin'], 'admin/upgrade'=>['access'=> 'admin'], 'admin/upgrade/reset'=>['access'=> 'admin'], 'admin/user/ban'=>['access'=> 'admin'], 'admin/user/bulk/ban'=>['access'=> 'admin'], 'admin/user/bulk/delete'=>['access'=> 'admin'], 'admin/user/bulk/unban'=>['access'=> 'admin'], 'admin/user/bulk/validate'=>['access'=> 'admin'], 'admin/user/change_email'=>['access'=> 'admin'], 'admin/user/delete'=>['access'=> 'admin'], 'admin/user/login_as'=>['access'=> 'admin'], 'admin/user/logout_as'=>[], 'admin/user/makeadmin'=>['access'=> 'admin'], 'admin/user/resetpassword'=>['access'=> 'admin'], 'admin/user/removeadmin'=>['access'=> 'admin'], 'admin/user/unban'=>['access'=> 'admin'], 'admin/user/validate'=>['access'=> 'admin'], 'annotation/delete'=>[], 'avatar/upload'=>[], 'comment/save'=>[], 'diagnostics/download'=>['access'=> 'admin'], 'entity/chooserestoredestination'=>[], 'entity/delete'=>[], 'entity/mute'=>[], 'entity/restore'=>[], 'entity/subscribe'=>[], 'entity/trash'=>[], 'entity/unmute'=>[], 'entity/unsubscribe'=>[], 'login'=>['access'=> 'logged_out'], 'logout'=>[], 'notifications/mute'=>['access'=> 'public'], 'plugins/settings/remove'=>['access'=> 'admin'], 'plugins/settings/save'=>['access'=> 'admin'], 'plugins/usersettings/save'=>[], 'register'=>['access'=> 'logged_out', 'middleware'=>[\Elgg\Router\Middleware\RegistrationAllowedGatekeeper::class,],], 'river/delete'=>[], 'settings/notifications'=>[], 'settings/notifications/subscriptions'=>[], 'user/changepassword'=>['access'=> 'public'], 'user/requestnewpassword'=>['access'=> 'public'], 'useradd'=>['access'=> 'admin'], 'usersettings/save'=>[], 'widgets/add'=>[], 'widgets/delete'=>[], 'widgets/move'=>[], 'widgets/save'=>[],]
Definition: actions.php:73
Wrapper for console application.
Definition: Application.php:11
Adds interaction to a console command.
Definition: BaseCommand.php:18
Abstract command with some utility methods.
Definition: Command.php:12
CLI bootstrap.
Definition: Cli.php:16
__construct(protected EventsService $events, protected InputInterface $input, protected OutputInterface $output)
Constructor.
Definition: Cli.php:32
$console
Definition: Cli.php:23
add(string $command)
Add a new CLI command.
Definition: Cli.php:96
getOutput()
Returns console output.
Definition: Cli.php:156
getPluginCommands()
Returns the cli commands registered in plugins.
Definition: Cli.php:72
getInput()
Returns console input.
Definition: Cli.php:147
getCoreCommands()
Returns the core cli commands.
Definition: Cli.php:62
run(bool $bootstrap=true)
Bootstrap and run console application.
Definition: Cli.php:134
bootstrap()
Add CLI tools to the console application.
Definition: Cli.php:48
Events service.
Find Elgg and project paths.
Definition: Paths.php:8
elgg_get_release()
Get the current Elgg release.
$output
Definition: download.php:9
$input
Form field view.
Definition: field.php:13
elgg()
Bootstrapping and helper procedural code available for use in Elgg core and plugins.
Definition: elgglib.php:12
elgg_get_plugins(string $status='active')
Returns an ordered list of plugins.
Definition: plugins.php:39
elgg_echo(string $message_key, array $args=[], string $language='')
Elgg language module Functions to manage language and translations.
Definition: languages.php:17
if(parse_url(elgg_get_site_url(), PHP_URL_PATH) !=='/') if(file_exists(elgg_get_root_path() . 'robots.txt'))
Set robots.txt.
Definition: robots.php:10
$plugin