1
|
|
|
<?php |
2
|
|
|
namespace Robo\Common; |
3
|
|
|
|
4
|
|
|
use Symfony\Component\Console\Helper\QuestionHelper; |
5
|
|
|
use Symfony\Component\Console\Question\ConfirmationQuestion; |
6
|
|
|
use Symfony\Component\Console\Question\Question; |
7
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
8
|
|
|
|
9
|
|
|
trait IO |
10
|
|
|
{ |
11
|
|
|
use InputAwareTrait; |
12
|
|
|
use OutputAwareTrait; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var \Symfony\Component\Console\Style\SymfonyStyle |
16
|
|
|
*/ |
17
|
|
|
protected $io; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Provide access to SymfonyStyle object. |
21
|
|
|
* |
22
|
|
|
* @return SymfonyStyle |
23
|
|
|
* |
24
|
|
|
* @see http://symfony.com/blog/new-in-symfony-2-8-console-style-guide |
25
|
|
|
*/ |
26
|
|
|
protected function io() |
27
|
|
|
{ |
28
|
|
|
if (!$this->io) { |
29
|
|
|
$this->io = new SymfonyStyle($this->input(), $this->output()); |
30
|
|
|
} |
31
|
|
|
return $this->io; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param string $nonDecorated |
36
|
|
|
* @param string $decorated |
37
|
|
|
* |
38
|
|
|
* @return string |
39
|
|
|
*/ |
40
|
|
|
protected function decorationCharacter($nonDecorated, $decorated) |
41
|
|
|
{ |
42
|
|
|
if (!$this->output()->isDecorated() || (strncasecmp(PHP_OS, 'WIN', 3) == 0)) { |
43
|
|
|
return $nonDecorated; |
44
|
|
|
} |
45
|
|
|
return $decorated; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param string $text |
50
|
|
|
*/ |
51
|
|
|
protected function say($text) |
52
|
|
|
{ |
53
|
|
|
$char = $this->decorationCharacter('>', '➜'); |
54
|
|
|
$this->writeln("$char $text"); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param string $text |
59
|
|
|
* @param int $length |
60
|
|
|
* @param string $color |
61
|
|
|
*/ |
62
|
|
|
protected function yell($text, $length = 40, $color = 'green') |
63
|
|
|
{ |
64
|
|
|
$char = $this->decorationCharacter(' ', '➜'); |
65
|
|
|
$format = "$char <fg=white;bg=$color;options=bold>%s</fg=white;bg=$color;options=bold>"; |
66
|
|
|
$this->formattedOutput($text, $length, $format); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param string $text |
71
|
|
|
* @param int $length |
72
|
|
|
* @param string $format |
73
|
|
|
*/ |
74
|
|
|
protected function formattedOutput($text, $length, $format) |
75
|
|
|
{ |
76
|
|
|
$lines = explode("\n", trim($text, "\n")); |
77
|
|
|
$maxLineLength = array_reduce(array_map('strlen', $lines), 'max'); |
78
|
|
|
$length = max($length, $maxLineLength); |
79
|
|
|
$len = $length + 2; |
80
|
|
|
$space = str_repeat(' ', $len); |
81
|
|
|
$this->writeln(sprintf($format, $space)); |
82
|
|
|
foreach ($lines as $line) { |
83
|
|
|
$line = str_pad($line, $length, ' ', STR_PAD_BOTH); |
84
|
|
|
$this->writeln(sprintf($format, " $line ")); |
85
|
|
|
} |
86
|
|
|
$this->writeln(sprintf($format, $space)); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param string $question |
91
|
|
|
* @param bool $hideAnswer |
92
|
|
|
* |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
|
|
protected function ask($question, $hideAnswer = false) |
96
|
|
|
{ |
97
|
|
|
if ($hideAnswer) { |
98
|
|
|
return $this->askHidden($question); |
99
|
|
|
} |
100
|
|
|
return $this->doAsk(new Question($this->formatQuestion($question))); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param string $question |
105
|
|
|
* |
106
|
|
|
* @return string |
107
|
|
|
*/ |
108
|
|
|
protected function askHidden($question) |
109
|
|
|
{ |
110
|
|
|
$question = new Question($this->formatQuestion($question)); |
111
|
|
|
$question->setHidden(true); |
112
|
|
|
return $this->doAsk($question); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param string $question |
117
|
|
|
* @param string $default |
118
|
|
|
* |
119
|
|
|
* @return string |
120
|
|
|
*/ |
121
|
|
|
protected function askDefault($question, $default) |
122
|
|
|
{ |
123
|
|
|
return $this->doAsk(new Question($this->formatQuestion("$question [$default]"), $default)); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param string $question |
128
|
|
|
* |
129
|
|
|
* @return string |
130
|
|
|
*/ |
131
|
|
|
protected function confirm($question) |
132
|
|
|
{ |
133
|
|
|
return $this->doAsk(new ConfirmationQuestion($this->formatQuestion($question . ' (y/n)'), false)); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param string $question |
138
|
|
|
* @param bool $default |
139
|
|
|
* |
140
|
|
|
* @return string |
141
|
|
|
*/ |
142
|
|
|
protected function confirmDefault($question, $default) |
143
|
|
|
{ |
144
|
|
|
$formatted_question = $this->formatQuestion(sprintf('%s (y/n) [%s]', $question, $default ? 'y' : 'n')); |
145
|
|
|
return $this->doAsk(new ConfirmationQuestion($formatted_question, $default)); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param \Symfony\Component\Console\Question\Question $question |
150
|
|
|
* |
151
|
|
|
* @return string |
152
|
|
|
*/ |
153
|
|
|
protected function doAsk(Question $question) |
154
|
|
|
{ |
155
|
|
|
return $this->getDialog()->ask($this->input(), $this->output(), $question); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param string $message |
160
|
|
|
* |
161
|
|
|
* @return string |
162
|
|
|
*/ |
163
|
|
|
protected function formatQuestion($message) |
164
|
|
|
{ |
165
|
|
|
return "<question>? $message</question> "; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @return \Symfony\Component\Console\Helper\QuestionHelper |
170
|
|
|
*/ |
171
|
|
|
protected function getDialog() |
172
|
|
|
{ |
173
|
|
|
return new QuestionHelper(); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @param $text |
178
|
|
|
*/ |
179
|
|
|
protected function writeln($text) |
180
|
|
|
{ |
181
|
|
|
$this->output()->writeln($text); |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|