Elgg  Version master
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 write($messages, $style = 'info'): void {
83  $formatter = new FormatterHelper();
84  $message = $formatter->formatBlock($messages, $style);
85  $this->output->writeln($message);
86  }
87 
95  public function option($name) {
96  return $this->input->getOption($name);
97  }
98 
106  public function argument($name) {
107  return $this->input->getArgument($name);
108  }
109 
118  public function assertNotEmpty($answer) {
119  if (empty($answer)) {
120  throw new RuntimeException('Please enter a required answer');
121  }
122 
123  return $answer;
124  }
125 
130  final protected function dumpRegisters() {
131  $registers = _elgg_services()->system_messages->loadRegisters();
132 
133  foreach ($registers as $prop => $values) {
134  if (!empty($values)) {
135  foreach ($values as $msg) {
136  $prop === 'error' ? $this->write($msg, 'error') : $this->write($msg);
137  }
138  }
139  }
140  }
141 }
if(! $user||! $user->canDelete()) $name
Definition: delete.php:22
Adds interaction to a console command.
Definition: BaseCommand.php:18
option($name)
Returns option value.
Definition: BaseCommand.php:95
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:82
dumpRegisters()
Dump and output system and error messages.
argument($name)
Returns argument value.
Exception thrown if an error which can only be found on runtime occurs.
_elgg_services()
Get the global service provider.
Definition: elgglib.php:333
$default
Definition: checkbox.php:30
$required
Definition: label.php:12
$style
Definition: full.php:95
$messages
Definition: admin.php:12