-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
DrushStyle.php
68 lines (57 loc) · 2.04 KB
/
DrushStyle.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
namespace Drush\Style;
use Drush\Drush;
use Drush\Exceptions\UserAbortException;
use Symfony\Component\Console\Question\Question;
use Symfony\Component\Console\Style\SymfonyStyle;
class DrushStyle extends SymfonyStyle
{
public function confirm(string $question, bool $default = true): bool
{
// Automatically accept confirmations if the --yes argument was supplied.
if (Drush::affirmative()) {
$this->comment($question . ': yes.');
return true;
} elseif (Drush::negative()) {
// Automatically cancel confirmations if the --no argument was supplied.
$this->warning($question . ': no.');
return false;
}
return parent::confirm($question, $default);
}
public function choice(string $question, array $choices, mixed $default = null): mixed
{
// Display the choices without their keys.
$choices_values = array_values($choices);
$return = parent::choice($question, $choices_values, $default);
return array_search($return, $choices);
}
public function warning(string|array $message)
{
$this->block($message, 'WARNING', 'fg=black;bg=yellow', ' ! ', true);
}
public function note(string|array $message)
{
$this->block($message, 'NOTE', 'fg=black;bg=yellow', ' ! ');
}
public function caution(string|array $message)
{
$this->block($message, 'CAUTION', 'fg=black;bg=yellow', ' ! ', true);
}
/**
* @return mixed
*/
public function askRequired($question)
{
$question = new Question($question);
$question->setValidator(function (?string $value) {
// FALSE is not considered as empty value because question helper use
// it as negative answer on confirmation questions.
if ($value === null || $value === '') {
throw new \UnexpectedValueException('This value is required.');
}
return $value;
});
return $this->askQuestion($question);
}
}