Elgg  Version master
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 
19  $this->input = $input;
20  $this->output = $output;
21 
22  $transport = new ResponseTransport($this);
23  _elgg_services()->responseFactory->setTransport($transport);
24 
25  $this->setLanguage();
26  $this->login();
27 
28  try {
29  $result = $this->command();
30 
31  if (is_callable($result)) {
32  $result = call_user_func($result, $this);
33  }
34  } catch (\Exception $ex) {
35  $this->error($ex);
36 
37  $result = $ex->getCode() ? : 1;
38  }
39 
40  $this->dumpRegisters();
41 
42  $this->logout();
43 
44  return (int) $result;
45  }
46 
56  abstract protected function command();
57 
64  final protected function login() {
65  if (!$this->getDefinition()->hasOption('as')) {
66  return;
67  }
68 
69  $username = $this->option('as');
70  if (!$username) {
71  return;
72  }
73 
75  if (!$user) {
76  throw new RuntimeException(elgg_echo('user:username:notfound', [$username]));
77  }
78 
80 
81  elgg_log(elgg_echo('cli:login:success:log', [$username, $user->guid]));
82  }
83 
88  final protected function logout() {
89  if (elgg_is_logged_in()) {
90  elgg_logout();
91  }
92  }
93 
100  final protected function setLanguage() {
101  if (!$this->getDefinition()->hasOption('language')) {
102  return;
103  }
104 
105  $language = (string) $this->option('language');
106  if (empty($language)) {
107  return;
108  }
109 
110  _elgg_services()->translator->setCurrentLanguage($language);
111  }
112 }
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:64
logout()
Logout a user.
Definition: Command.php:88
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:347
command()
Command to be executed.
option($name)
Returns option value.
setLanguage()
Set the language for this cli command.
Definition: Command.php:100