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 \Symfony\Component\Console\Style\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
|
|
|
* @param bool $default |
129
|
|
|
* |
130
|
|
|
* @return string |
131
|
|
|
*/ |
132
|
|
|
protected function confirm($question, $default = false) |
133
|
|
|
{ |
134
|
|
|
return $this->doAsk(new ConfirmationQuestion($this->formatQuestion($question . ' (y/n)'), $default)); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param \Symfony\Component\Console\Question\Question $question |
139
|
|
|
* |
140
|
|
|
* @return string |
141
|
|
|
*/ |
142
|
|
|
protected function doAsk(Question $question) |
143
|
|
|
{ |
144
|
|
|
return $this->getDialog()->ask($this->input(), $this->output(), $question); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param string $message |
149
|
|
|
* |
150
|
|
|
* @return string |
151
|
|
|
*/ |
152
|
|
|
protected function formatQuestion($message) |
153
|
|
|
{ |
154
|
|
|
return "<question>? $message</question> "; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @return \Symfony\Component\Console\Helper\QuestionHelper |
159
|
|
|
*/ |
160
|
|
|
protected function getDialog() |
161
|
|
|
{ |
162
|
|
|
return new QuestionHelper(); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param $text |
167
|
|
|
*/ |
168
|
|
|
protected function writeln($text) |
169
|
|
|
{ |
170
|
|
|
$this->output()->writeln($text); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|