-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathConsole.php
234 lines (215 loc) · 6.12 KB
/
Console.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\ComposerRootUpdatePlugin\Utils;
use Composer\IO\IOInterface;
use Composer\IO\NullIO;
use Exception;
use InvalidArgumentException;
/**
* Singleton logger with console interaction methods
*/
class Console
{
/**
* Log message formatting tags
*/
public const FORMAT_INFO = '<info>';
public const FORMAT_COMMENT = '<comment>';
public const FORMAT_WARN = '<warning>';
public const FORMAT_ERROR = '<error>';
/**
* Verbosity levels copied from IOInterface for clarity
*/
public const QUIET = IOInterface::QUIET;
public const NORMAL = IOInterface::NORMAL;
public const VERBOSE = IOInterface::VERBOSE;
public const VERY_VERBOSE = IOInterface::VERY_VERBOSE;
public const DEBUG = IOInterface::DEBUG;
/**
* @var IOInterface $io
*/
protected $io;
/**
* @var string $verboseLabel
*/
protected $verboseLabel;
/**
* @var bool $interactive
*/
protected $interactive;
/**
* Console constructor.
*
* @param IOInterface|null $io
* @param bool $interactive
* @param string|null $verboseLabel
* @return void
*/
public function __construct(?IOInterface $io = null, bool $interactive = false, ?string $verboseLabel = null)
{
if ($io === null) {
$this->io = new NullIO();
} else {
$this->io = $io;
}
$this->verboseLabel = $verboseLabel;
$this->interactive = $interactive;
}
/**
* Get the Composer IOInterface instance
*
* @return IOInterface
*/
public function getIO(): IOInterface
{
return $this->io;
}
/**
* Whether or not ask() should interactively ask the question or just return the default value
*
* @param bool $interactive
* @return void
*/
public function setInteractive(bool $interactive)
{
$this->interactive = $interactive;
}
/**
* Ask the user a yes or no question and return the result
*
* If the console is not interactive, instead do not ask and just return the default
*
* @param string $question
* @param bool $default
* @return bool
*/
public function ask(string $question, bool $default = false): bool
{
$result = $default;
if ($this->interactive) {
if (!$this->getIO()->isInteractive()) {
throw new InvalidArgumentException(
'Interactive options cannot be used in non-interactive terminals.'
);
}
$opts = $default ? 'Y,n' : 'y,N';
$result = $this->getIO()->askConfirmation("<info>$question</info> [<comment>$opts</comment>]? ", $default);
}
return $result;
}
/**
* Log the given message with verbosity and formatting
*
* @param string $message
* @param int $verbosity
* @param string|null $format
* @return void
*/
public function log(string $message, int $verbosity = Console::NORMAL, ?string $format = null)
{
if ($format) {
$message = $this->formatString($message, $format);
}
$this->getIO()->writeError($message, true, $verbosity);
}
/**
* Helper method to log the given message with <info> formatting
*
* @param string $message
* @param int $verbosity
* @return void
*/
public function info(string $message, int $verbosity = Console::NORMAL)
{
$this->log($message, $verbosity, self::FORMAT_INFO);
}
/**
* Helper method to log the given message with <comment> formatting
*
* @param string $message
* @param int $verbosity
* @return void
*/
public function comment(string $message, int $verbosity = Console::NORMAL)
{
$this->log($message, $verbosity, self::FORMAT_COMMENT);
}
/**
* Helper method to log the given message with <warning> formatting
*
* @param string $message
* @param int $verbosity
* @return void
*/
public function warning(string $message, int $verbosity = Console::NORMAL)
{
$this->log($message, $verbosity, self::FORMAT_WARN);
}
/**
* Label and log the given message if output is set to verbose
*
* A null $label will use the globally configured $verboseLabel
*
* @param string $message
* @param string|null $label
* @param int $verbosity
* @param string|null $format
* @return void
*/
public function labeledVerbose(
string $message,
?string $label = null,
int $verbosity = Console::VERBOSE,
?string $format = null
) {
if ($format) {
$message = $this->formatString($message, $format);
}
if ($label === null) {
$label = $this->verboseLabel;
}
if ($label) {
$message = " <comment>[</comment>$label<comment>]</comment> $message";
}
$this->log($message, $verbosity);
}
/**
* Formats with <error> and logs to Console::QUIET followed by the exception's message at Console::NORMAL
*
* @param string $message
* @param Exception|null $exception
* @return void
*/
public function error(string $message, ?Exception $exception = null)
{
$this->log($message, self::QUIET, self::FORMAT_ERROR);
if ($exception) {
$this->log($exception->getMessage());
}
}
/**
* Sets the label to apply to labeledVerbose() messages if not overridden
*
* @param string $verboseLabel
* @return void
*/
public function setVerboseLabel(string $verboseLabel)
{
$this->verboseLabel = $verboseLabel;
}
/**
* Helper function to wrap a string in the given format tag
*
* @param string $str
* @param string $formatTag
* @return string
*/
public function formatString(string $str, string $formatTag): string
{
$formatClose = str_replace('<', '</', $formatTag);
return $formatTag . $str . $formatClose;
}
}