1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of CaptainHook. |
4
|
|
|
* |
5
|
|
|
* (c) Sebastian Feldmann <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
namespace CaptainHook\App\Console\IO; |
11
|
|
|
|
12
|
|
|
use CaptainHook\App\Console\IO; |
13
|
|
|
use CaptainHook\App\Console\IOUtil; |
14
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
15
|
|
|
use Symfony\Component\Console\Helper\HelperSet; |
16
|
|
|
use Symfony\Component\Console\Output\ConsoleOutputInterface; |
17
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
18
|
|
|
use Symfony\Component\Console\Question\ConfirmationQuestion; |
19
|
|
|
use Symfony\Component\Console\Question\Question; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class DefaultIO |
23
|
|
|
* |
24
|
|
|
* @package CaptainHook |
25
|
|
|
* @author Sebastian Feldmann <[email protected]> |
26
|
|
|
* @link https://github.com/captainhookphp/captainhook |
27
|
|
|
* @since Class available since Release 0.9.0 |
28
|
|
|
*/ |
29
|
|
|
class DefaultIO extends Base |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var \Symfony\Component\Console\Input\InputInterface |
33
|
|
|
*/ |
34
|
|
|
protected $input; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var \Symfony\Component\Console\Output\OutputInterface |
38
|
|
|
*/ |
39
|
|
|
protected $output; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var \Symfony\Component\Console\Helper\HelperSet |
43
|
|
|
*/ |
44
|
|
|
protected $helperSet; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var array |
48
|
|
|
*/ |
49
|
|
|
private $verbosityMap; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Constructor |
53
|
|
|
* |
54
|
|
|
* @param \Symfony\Component\Console\Input\InputInterface $input |
55
|
|
|
* @param \Symfony\Component\Console\Output\OutputInterface $output |
56
|
|
|
* @param \Symfony\Component\Console\Helper\HelperSet $helperSet |
57
|
|
|
*/ |
58
|
14 |
|
public function __construct(InputInterface $input, OutputInterface $output, HelperSet $helperSet = null) |
59
|
|
|
{ |
60
|
14 |
|
$this->input = $input; |
61
|
14 |
|
$this->output = $output; |
62
|
14 |
|
$this->helperSet = $helperSet; |
63
|
14 |
|
$this->verbosityMap = [ |
64
|
|
|
IO::QUIET => OutputInterface::VERBOSITY_QUIET, |
65
|
|
|
IO::NORMAL => OutputInterface::VERBOSITY_NORMAL, |
66
|
|
|
IO::VERBOSE => OutputInterface::VERBOSITY_VERBOSE, |
67
|
|
|
IO::VERY_VERBOSE => OutputInterface::VERBOSITY_VERY_VERBOSE, |
68
|
|
|
IO::DEBUG => OutputInterface::VERBOSITY_DEBUG |
69
|
|
|
]; |
70
|
14 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Return the original cli arguments |
74
|
|
|
* |
75
|
|
|
* @return array |
76
|
|
|
*/ |
77
|
1 |
|
public function getArguments(): array |
78
|
|
|
{ |
79
|
1 |
|
return $this->input->getArguments(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* {@inheritDoc} |
84
|
|
|
*/ |
85
|
1 |
|
public function isInteractive() |
86
|
|
|
{ |
87
|
1 |
|
return $this->input->isInteractive(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* {@inheritDoc} |
92
|
|
|
*/ |
93
|
1 |
|
public function isVerbose() |
94
|
|
|
{ |
95
|
1 |
|
return $this->output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* {@inheritDoc} |
100
|
|
|
*/ |
101
|
1 |
|
public function isVeryVerbose() |
102
|
|
|
{ |
103
|
1 |
|
return $this->output->getVerbosity() >= OutputInterface::VERBOSITY_VERY_VERBOSE; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* {@inheritDoc} |
108
|
|
|
*/ |
109
|
1 |
|
public function isDebug() |
110
|
|
|
{ |
111
|
1 |
|
return $this->output->getVerbosity() >= OutputInterface::VERBOSITY_DEBUG; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* {@inheritDoc} |
116
|
|
|
*/ |
117
|
2 |
|
public function write($messages, $newline = true, $verbosity = self::NORMAL) |
118
|
|
|
{ |
119
|
2 |
|
$this->doWrite($messages, $newline, false, $verbosity); |
120
|
2 |
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* {@inheritDoc} |
124
|
|
|
*/ |
125
|
2 |
|
public function writeError($messages, $newline = true, $verbosity = self::NORMAL) |
126
|
|
|
{ |
127
|
2 |
|
$this->doWrite($messages, $newline, true, $verbosity); |
128
|
2 |
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Write to the appropriate user output |
132
|
|
|
* |
133
|
|
|
* @param array|string $messages |
134
|
|
|
* @param bool $newline |
135
|
|
|
* @param bool $stderr |
136
|
|
|
* @param int $verbosity |
137
|
|
|
*/ |
138
|
4 |
|
private function doWrite($messages, $newline, $stderr, $verbosity) |
139
|
|
|
{ |
140
|
4 |
|
$sfVerbosity = $this->verbosityMap[$verbosity]; |
141
|
4 |
|
if ($sfVerbosity > $this->output->getVerbosity()) { |
142
|
1 |
|
return; |
143
|
|
|
} |
144
|
|
|
|
145
|
3 |
|
$this->getOutputToWriteTo($stderr)->write($messages, $newline, $sfVerbosity); |
|
|
|
|
146
|
3 |
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* {@inheritDoc} |
150
|
|
|
*/ |
151
|
1 |
|
public function ask($question, $default = null) |
152
|
|
|
{ |
153
|
|
|
/** @var \Symfony\Component\Console\Helper\QuestionHelper $helper */ |
154
|
1 |
|
$helper = $this->helperSet->get('question'); |
155
|
1 |
|
$question = new Question($question, $default); |
156
|
|
|
|
157
|
1 |
|
return $helper->ask($this->input, $this->getOutputToWriteTo(), $question); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* {@inheritDoc} |
162
|
|
|
*/ |
163
|
1 |
|
public function askConfirmation($question, $default = true) |
164
|
|
|
{ |
165
|
|
|
/** @var \Symfony\Component\Console\Helper\QuestionHelper $helper */ |
166
|
1 |
|
$helper = $this->helperSet->get('question'); |
167
|
1 |
|
$question = new ConfirmationQuestion($question, $default); |
168
|
|
|
|
169
|
1 |
|
return IOUtil::answerToBool($helper->ask($this->input, $this->getOutputToWriteTo(), $question)); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* {@inheritDoc} |
174
|
|
|
*/ |
175
|
1 |
|
public function askAndValidate($question, $validator, $attempts = null, $default = null) |
176
|
|
|
{ |
177
|
|
|
/** @var \Symfony\Component\Console\Helper\QuestionHelper $helper */ |
178
|
1 |
|
$helper = $this->helperSet->get('question'); |
179
|
1 |
|
$question = new Question($question, $default); |
180
|
1 |
|
$question->setValidator($validator); |
181
|
1 |
|
$question->setMaxAttempts($attempts); |
182
|
|
|
|
183
|
1 |
|
return $helper->ask($this->input, $this->getOutputToWriteTo(), $question); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Return the output to write to |
188
|
|
|
* |
189
|
|
|
* @param bool $stdErr |
190
|
|
|
* @return \Symfony\Component\Console\Output\OutputInterface |
191
|
|
|
*/ |
192
|
6 |
|
private function getOutputToWriteTo($stdErr = false) |
193
|
|
|
{ |
194
|
6 |
|
if ($stdErr && $this->output instanceof ConsoleOutputInterface) { |
195
|
1 |
|
return $this->output->getErrorOutput(); |
196
|
|
|
} |
197
|
|
|
|
198
|
5 |
|
return $this->output; |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.