Elgg  Version 6.3
Command.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Cli;
4 
6 use Psr\Log\LogLevel;
7 use Symfony\Component\Console\Input\InputInterface;
8 use Symfony\Component\Console\Output\OutputInterface;
9 
13 abstract class Command extends BaseCommand {
14 
18  final public function execute(InputInterface $input, OutputInterface $output) {
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  if ($this->option('quiet')) {
29  ob_start();
30  }
31 
32  try {
33  $result = $this->command();
34 
35  if (is_callable($result)) {
36  $result = call_user_func($result, $this);
37  }
38  } catch (\Exception $ex) {
39  elgg_log($ex, LogLevel::ERROR);
40 
41  $result = $ex->getCode() ?: self::FAILURE;
42  }
43 
44  if ($this->option('quiet')) {
45  ob_end_clean();
46  }
47 
48  $this->dumpRegisters();
49 
50  $this->logout();
51 
52  return (int) $result;
53  }
54 
64  abstract protected function command();
65 
72  final protected function login() {
73  if (!$this->getDefinition()->hasOption('as')) {
74  return;
75  }
76 
77  $username = $this->option('as');
78  if (!$username) {
79  return;
80  }
81 
83  if (!$user) {
84  throw new RuntimeException(elgg_echo('user:username:notfound', [$username]));
85  }
86 
88 
89  elgg_log(elgg_echo('cli:login:success:log', [$username, $user->guid]));
90  }
91 
96  final protected function logout() {
97  if (elgg_is_logged_in()) {
98  elgg_logout();
99  }
100  }
101 
108  final protected function setLanguage() {
109  if (!$this->getDefinition()->hasOption('language')) {
110  return;
111  }
112 
113  $language = (string) $this->option('language');
114  if (empty($language)) {
115  return;
116  }
117 
118  _elgg_services()->translator->setCurrentLanguage($language);
119  }
120 }
$username
Definition: delete.php:23
$language
Definition: useradd.php:17
$user
Definition: ban.php:7
Adds interaction to a console command.
Definition: BaseCommand.php:18
option($name)
Returns option value.
dumpRegisters()
Dump and output system and error messages.
Abstract command with some utility methods.
Definition: Command.php:13
command()
Command to be executed.
execute(InputInterface $input, OutputInterface $output)
{}
Definition: Command.php:18
logout()
Logout a user.
Definition: Command.php:96
setLanguage()
Set the language for this cli command.
Definition: Command.php:108
login()
Login a user defined by –as option.
Definition: Command.php:72
Cli ResponseTransport.
Base exception of exceptions in the Elgg system.
Definition: Exception.php:11
Exception thrown if an error which can only be found on runtime occurs.
elgg_log($message, $level=\Psr\Log\LogLevel::NOTICE)
Log a message.
Definition: elgglib.php:88
_elgg_services()
Get the global service provider.
Definition: elgglib.php:337
elgg_get_user_by_username(string $username, bool $try_email=false)
Get a user by username.
Definition: users.php:31
elgg_echo(string $message_key, array $args=[], string $language='')
Elgg language module Functions to manage language and translations.
Definition: languages.php:17
elgg_login(\ElggUser $user, bool $persistent=false)
Log in a user.
Definition: sessions.php:81
elgg_logout()
Log the current user out.
Definition: sessions.php:91
elgg_is_logged_in()
Returns whether or not the user is currently logged in.
Definition: sessions.php:43