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