Elgg  Version 5.1
Command.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Cli;
4 
8 
12 abstract class Command extends BaseCommand {
13 
17  final public function execute(InputInterface $input, OutputInterface $output) {
18  $this->input = $input;
19  $this->output = $output;
20 
21  $transport = new ResponseTransport($this);
22  _elgg_services()->responseFactory->setTransport($transport);
23 
24  $this->setLanguage();
25  $this->login();
26 
27  try {
28  $result = $this->command();
29 
30  if (is_callable($result)) {
31  $result = call_user_func($result, $this);
32  }
33  } catch (\Exception $ex) {
34  $this->error($ex);
35 
36  $result = $ex->getCode() ?: 1;
37  }
38 
39  $this->dumpRegisters();
40 
41  $this->logout();
42 
43  return (int) $result;
44  }
45 
55  abstract protected function command();
56 
63  final protected function login() {
64  if (!$this->getDefinition()->hasOption('as')) {
65  return;
66  }
67 
68  $username = $this->option('as');
69  if (!$username) {
70  return;
71  }
72 
74  if (!$user) {
75  throw new RuntimeException(elgg_echo('user:username:notfound', [$username]));
76  }
77 
79 
80  elgg_log(elgg_echo('cli:login:success:log', [$username, $user->guid]));
81  }
82 
87  final protected function logout() {
88  if (elgg_is_logged_in()) {
89  elgg_logout();
90  }
91  }
92 
99  final protected function setLanguage() {
100  if (!$this->getDefinition()->hasOption('language')) {
101  return;
102  }
103 
104  $language = (string) $this->option('language');
105  if (empty($language)) {
106  return;
107  }
108 
109  _elgg_services()->translator->setCurrentLanguage($language);
110  }
111 }
elgg_is_logged_in()
Returns whether or not the user is currently logged in.
Definition: sessions.php:43
execute(InputInterface $input, OutputInterface $output)
{}
Definition: Command.php:17
Exception thrown if an error which can only be found on runtime occurs.
elgg_get_user_by_username(string $username, bool $try_email=false)
Get a user by username.
Definition: users.php:39
Adds interaction to a console command.
Definition: BaseCommand.php:18
login()
Login a user defined by –as option.
Definition: Command.php:63
logout()
Logout a user.
Definition: Command.php:87
error($message)
Print an error.
elgg_echo(string $message_key, array $args=[], string $language= '')
Elgg language module Functions to manage language and translations.
Definition: languages.php:17
$username
Definition: delete.php:23
elgg_logout()
Log the current user out.
Definition: sessions.php:91
dumpRegisters()
Dump and output system and error messages.
$language
Definition: useradd.php:17
elgg_log($message, $level=\Psr\Log\LogLevel::NOTICE)
Log a message.
Definition: elgglib.php:86
$user
Definition: ban.php:7
Cli ResponseTransport.
Abstract command with some utility methods.
Definition: Command.php:12
elgg_login(\ElggUser $user, bool $persistent=false)
Log in a user.
Definition: sessions.php:81
_elgg_services()
Get the global service provider.
Definition: elgglib.php:346
command()
Command to be executed.
option($name)
Returns option value.
setLanguage()
Set the language for this cli command.
Definition: Command.php:99