Elgg  Version 6.3
BaseCommand.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Cli;
4 
6 use Elgg\Traits\Loggable;
7 use Psr\Log\LogLevel;
8 use Symfony\Component\Console\Helper\FormatterHelper;
9 use Symfony\Component\Console\Helper\QuestionHelper;
10 use Symfony\Component\Console\Input\InputInterface;
11 use Symfony\Component\Console\Output\OutputInterface;
12 use Symfony\Component\Console\Question\Question;
13 use Symfony\Component\VarDumper\VarDumper;
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 
82  final public function dump($data) {
83  elgg_deprecated_notice(__METHOD__ . ' is deprecated and will be removed', '6.3');
84 
85  VarDumper::dump($data);
86  }
87 
96  final public function write($messages, $style = 'info'): void {
97  $formatter = new FormatterHelper();
98 
99  if (!$this->output->getFormatter()->hasStyle($style)) {
100  elgg_deprecated_notice(__METHOD__ . " using '{$style}' needs to be a supported style, use [info, error, comment or question]", '6.3');
101 
102  switch ($style) {
103  case LogLevel::EMERGENCY:
104  case LogLevel::CRITICAL:
105  case LogLevel::ALERT:
106  case LogLevel::ERROR:
107  $style = 'error';
108  break;
109 
110  case LogLevel::WARNING:
111  $style = 'comment';
112  break;
113 
114  default:
115  $style = 'info';
116  break;
117  }
118  }
119 
120  $message = $formatter->formatBlock($messages, $style);
121  $this->output->writeln($message);
122  }
123 
132  public function error($message) {
133  elgg_deprecated_notice(__METHOD__ . ' has been deprecated, use elgg_log()', '6.3');
134 
135  elgg_log($message, LogLevel::ERROR);
136  }
137 
146  public function notice($message) {
147  elgg_deprecated_notice(__METHOD__ . ' has been deprecated, use elgg_log()', '6.3');
148 
149  elgg_log($message, LogLevel::NOTICE);
150  }
151 
159  public function option($name) {
160  return $this->input->getOption($name);
161  }
162 
170  public function argument($name) {
171  return $this->input->getArgument($name);
172  }
173 
182  public function assertNotEmpty($answer) {
183  if (empty($answer)) {
184  throw new RuntimeException('Please enter a required answer');
185  }
186 
187  return $answer;
188  }
189 
194  final protected function dumpRegisters() {
195  $registers = _elgg_services()->system_messages->loadRegisters();
196 
197  foreach ($registers as $prop => $values) {
198  if (!empty($values)) {
199  foreach ($values as $msg) {
200  $prop === 'error' ? $this->write($msg, 'error') : $this->write($msg);
201  }
202  }
203  }
204  }
205 }
if(! $user||! $user->canDelete()) $name
Definition: delete.php:22
if(! $entity instanceof \ElggUser) $data
Definition: attributes.php:13
Adds interaction to a console command.
Definition: BaseCommand.php:18
option($name)
Returns option value.
dump($data)
Dump a variable.
Definition: BaseCommand.php:82
ask($question, $default=null, $hidden=false, $required=true)
Ask a question.
Definition: BaseCommand.php:44
assertNotEmpty($answer)
Question validator for required user response.
write($messages, $style='info')
Write messages to output buffer.
Definition: BaseCommand.php:96
dumpRegisters()
Dump and output system and error messages.
notice($message)
Print a notice.
error($message)
Print an error.
argument($name)
Returns argument value.
Exception thrown if an error which can only be found on runtime occurs.
elgg_deprecated_notice(string $msg, string $dep_version)
Log a notice about deprecated use of a function, view, etc.
Definition: elgglib.php:101
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
$default
Definition: checkbox.php:30
$required
Definition: label.php:12
$style
Definition: full.php:95
$messages
Definition: admin.php:12