Elgg  Version master
BaseCommand.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Cli;
4 
14 
18 abstract class BaseCommand extends \Symfony\Component\Console\Command\Command {
19 
20  use Loggable;
21 
22  const DEFAULT_VERBOSITY = OutputInterface::VERBOSITY_NORMAL;
23 
27  protected $input;
28 
32  protected $output;
33 
44  public function ask($question, $default = null, $hidden = false, $required = true) {
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 
54  $question .= ': ';
55 
56  $q = new Question($question, $default);
57 
58  if ($hidden) {
59  $q->setHidden(true);
60  $q->setHiddenFallback(false);
61  }
62 
63  if ($required) {
64  $q->setValidator([
65  $this,
66  'assertNotEmpty'
67  ]);
68  $q->setMaxAttempts(2);
69  }
70 
71  return $helper->ask($this->input, $this->output, $q);
72  }
73 
81  final public function dump($data) {
82  VarDumper::dump($data);
83  }
84 
93  final public function write($messages, $level = LogLevel::INFO) {
94  $formatter = new FormatterHelper();
95 
96  switch ($level) {
97  case LogLevel::EMERGENCY:
98  case LogLevel::CRITICAL:
99  case LogLevel::ALERT:
100  case LogLevel::ERROR:
101  $style = 'error';
102  break;
103 
104  case LogLevel::WARNING:
105  $style = 'comment';
106  break;
107 
108  default:
109  $style = 'info';
110  break;
111  }
112 
113  $message = $formatter->formatBlock($messages, $style);
114  $this->output->writeln($message);
115  }
116 
124  public function error($message) {
125  $this->write($message, LogLevel::ERROR);
126  }
127 
135  public function notice($message) {
136  $this->write($message, LogLevel::NOTICE);
137  }
138 
146  public function option($name) {
147  return $this->input->getOption($name);
148  }
149 
157  public function argument($name) {
158  return $this->input->getArgument($name);
159  }
160 
169  public function assertNotEmpty($answer) {
170  if (empty($answer)) {
171  throw new RuntimeException('Please enter a required answer');
172  }
173 
174  return $answer;
175  }
176 
181  final protected function dumpRegisters() {
182  $registers = _elgg_services()->system_messages->loadRegisters();
183 
184  foreach ($registers as $prop => $values) {
185  if (!empty($values)) {
186  foreach ($values as $msg) {
187  $prop == 'error' ? $this->error($msg) : $this->notice($msg);
188  }
189  }
190  }
191  }
192 }
$default
Definition: checkbox.php:30
$messages
Definition: admin.php:12
Exception thrown if an error which can only be found on runtime occurs.
ask($question, $default=null, $hidden=false, $required=true)
Ask a question.
Definition: BaseCommand.php:44
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
Adds interaction to a console command.
Definition: BaseCommand.php:18
error($message)
Print an error.
notice($message)
Print a notce.
if(!$entity instanceof\ElggUser) $data
Definition: attributes.php:13
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:93
$style
Definition: full.php:95
$required
Definition: label.php:12
assertNotEmpty($answer)
Question validator for required user response.
_elgg_services()
Get the global service provider.
Definition: elgglib.php:346
argument($name)
Returns argument value.
dump($data)
Dump a variable.
Definition: BaseCommand.php:81
option($name)
Returns option value.