Elgg  Version 4.x
BaseCommand.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Cli;
4 
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  $question = trim($question);
49  $question = rtrim($question, ':');
50  if (is_scalar($default) && !is_bool($default)) {
51  $question .= " [{$default}]";
52  }
53  $question .= ': ';
54 
55  $q = new Question($question, $default);
56 
57  if ($hidden) {
58  $q->setHidden(true);
59  $q->setHiddenFallback(false);
60  }
61 
62  if ($required) {
63  $q->setValidator([
64  $this,
65  'assertNotEmpty'
66  ]);
67  $q->setMaxAttempts(2);
68  }
69 
70  return $helper->ask($this->input, $this->output, $q);
71  }
72 
80  final public function dump($data) {
81  VarDumper::dump($data);
82  }
83 
92  final public function write($messages, $level = LogLevel::INFO) {
93  $formatter = new FormatterHelper();
94 
95  switch ($level) {
96  case LogLevel::EMERGENCY :
97  case LogLevel::CRITICAL :
98  case LogLevel::ALERT :
99  case LogLevel::ERROR :
100  $style = 'error';
101  break;
102 
103  case LogLevel::WARNING :
104  $style = 'comment';
105  break;
106 
107  default :
108  $style = 'info';
109  break;
110  }
111 
112  $message = $formatter->formatBlock($messages, $style);
113  $this->output->writeln($message);
114  }
115 
123  public function error($message) {
124  $this->write($message, LogLevel::ERROR);
125  }
126 
134  public function notice($message) {
135  $this->write($message, LogLevel::NOTICE);
136  }
137 
145  public function option($name) {
146  return $this->input->getOption($name);
147  }
148 
156  public function argument($name) {
157  return $this->input->getArgument($name);
158  }
159 
167  public function assertNotEmpty($answer) {
168  if (empty($answer)) {
169  throw new \RuntimeException('Please enter a required answer');
170  }
171 
172  return $answer;
173  }
174 
179  final protected function dumpRegisters() {
180  $registers = _elgg_services()->system_messages->loadRegisters();
181 
182  foreach ($registers as $prop => $values) {
183  if (!empty($values)) {
184  foreach ($values as $msg) {
185  $prop == 'error' ? $this->error($msg) : $this->notice($msg);
186  }
187  }
188  }
189  }
190 }
$default
Definition: checkbox.php:31
$messages
Definition: admin.php:12
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.
if(elgg_trigger_plugin_hook('usersettings:save', 'user', $hooks_params, true)) foreach($request->validation() ->all() as $item) $data
Definition: save.php:53
notice($message)
Print a notce.
trait Loggable
Enables adding a logger.
Definition: Loggable.php:14
dumpRegisters()
Dump and output system and error messages.
write($messages, $level=LogLevel::INFO)
Write messages to output buffer.
Definition: BaseCommand.php:92
$style
Definition: full.php:94
$required
Definition: label.php:12
assertNotEmpty($answer)
Question validator for required user response.
_elgg_services()
Get the global service provider.
Definition: elgglib.php:777
argument($name)
Returns argument value.
dump($data)
Dump a variable.
Definition: BaseCommand.php:80
option($name)
Returns option value.