Elgg  Version 3.0
BaseCommand.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Cli;
4 
5 use Elgg\Loggable;
13 
17 abstract class BaseCommand extends \Symfony\Component\Console\Command\Command {
18 
19  use Loggable;
20 
21  const DEFAULT_VERBOSITY = OutputInterface::VERBOSITY_NORMAL;
22 
26  protected $input;
27 
31  protected $output;
32 
43  public function ask($question, $default = null, $hidden = false, $required = true) {
44 
45  /* @var $helper QuestionHelper */
46  $helper = $this->getHelper('question');
47 
48  $q = new Question($question, $default);
49 
50  if ($hidden) {
51  $q->setHidden(true);
52  $q->setHiddenFallback(false);
53  }
54 
55  if ($required) {
56  $q->setValidator([
57  $this,
58  'assertNotEmpty'
59  ]);
60  $q->setMaxAttempts(2);
61  }
62 
63  return $helper->ask($this->input, $this->output, $q);
64  }
65 
73  final public function dump($data) {
74  VarDumper::dump($data);
75  }
76 
85  final public function write($messages, $level = LogLevel::INFO) {
86  $formatter = new FormatterHelper();
87 
88  switch ($level) {
89  case LogLevel::EMERGENCY :
90  case LogLevel::CRITICAL :
91  case LogLevel::ALERT :
92  case LogLevel::ERROR :
93  $style = 'error';
94  break;
95 
96  case LogLevel::WARNING :
97  $style = 'comment';
98  break;
99 
100  default :
101  $style = 'info';
102  break;
103  }
104 
105  $message = $formatter->formatBlock($messages, $style);
106  $this->output->writeln($message);
107  }
108 
116  public function error($message) {
117  if (!$this->log(LogLevel::ERROR, $message)) {
118  $this->write($message, LogLevel::ERROR);
119  }
120  }
121 
129  public function notice($message) {
130  if (!$this->log(LogLevel::NOTICE, $message)) {
131  $this->write($message, LogLevel::NOTICE);
132  }
133  }
134 
142  public function option($name) {
143  return $this->input->getOption($name);
144  }
145 
153  public function argument($name) {
154  return $this->input->getArgument($name);
155  }
156 
164  public function assertNotEmpty($answer) {
165  if (empty($answer)) {
166  throw new \RuntimeException('Please enter a required answer');
167  }
168 
169  return $answer;
170  }
171 
176  final protected function dumpRegisters() {
177  $registers = _elgg_services()->systemMessages->loadRegisters();
178 
179  foreach ($registers as $prop => $values) {
180  if (!empty($values)) {
181  foreach ($values as $msg) {
182  $prop == 'error' ? $this->error($msg) : $this->notice($msg);
183  }
184  }
185  }
186  }
187 }
$messages
Definition: admin.php:15
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
ask($question, $default=null, $hidden=false, $required=true)
Ask a question.
Definition: BaseCommand.php:43
Adds interaction to a console command.
Definition: BaseCommand.php:17
error($message)
Print an error.
trait Loggable
Enables adding a logger.
Definition: Loggable.php:12
if(elgg_trigger_plugin_hook('usersettings:save', 'user', $hooks_params, true)) foreach($request->validation() ->all() as $item) $data
Definition: save.php:57
notice($message)
Print a notce.
dumpRegisters()
Dump and output system and error messages.
write($messages, $level=LogLevel::INFO)
Write messages to output buffer.
Definition: BaseCommand.php:85
$default
Definition: checkbox.php:35
if(empty($user_guids)) $hidden
Definition: delete.php:11
log($level, $message, array $context=[])
Log a message.
Definition: Loggable.php:55
$required
Definition: label.php:12
assertNotEmpty($answer)
Question validator for required user response.
_elgg_services()
Get the global service provider.
Definition: elgglib.php:1292
elgg ajax ERROR
Definition: ajax.js:33
argument($name)
Returns argument value.
dump($data)
Dump a variable.
Definition: BaseCommand.php:73
option($name)
Returns option value.