Elgg  Version 3.0
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->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  $username = $this->option('as');
68  if (!$username) {
69  return;
70  }
72  if (!$user) {
73  throw new RuntimeException("User with username $username not found");
74  }
75  if (!login($user)) {
76  throw new RuntimeException("Unable to login as $username");
77  }
78  elgg_log("Logged in as $username [guid: $user->guid]");
79  }
80 
85  final protected function logout() {
86  if (elgg_is_logged_in()) {
87  logout();
88  }
89  }
90 }
elgg_is_logged_in()
Returns whether or not the user is currently logged in.
Definition: sessions.php:48
execute(InputInterface $input, OutputInterface $output)
{}
Definition: Command.php:17
$username
Definition: delete.php:23
Adds interaction to a console command.
Definition: BaseCommand.php:17
login()
Login a user defined by –as option.
Definition: Command.php:63
logout()
Logout a user.
Definition: Command.php:85
error($message)
Print an error.
get_user_by_username($username)
Get user by username.
Definition: users.php:61
dumpRegisters()
Dump and output system and error messages.
elgg_log($message, $level=\Psr\Log\LogLevel::NOTICE)
Log a message.
Definition: elgglib.php:786
$user
Definition: ban.php:7
Cli ResponseTransport.
Abstract command with some utility methods.
Definition: Command.php:12
_elgg_services()
Get the global service provider.
Definition: elgglib.php:1292
command()
Command to be executed.
option($name)
Returns option value.