1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Symfony package. |
5
|
|
|
* |
6
|
|
|
* (c) Fabien Potencier <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Symfony\Component\Console\Formatter; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Formatter class for console output. |
16
|
|
|
* |
17
|
|
|
* @author Konstantin Kudryashov <[email protected]> |
18
|
|
|
* |
19
|
|
|
* @api |
20
|
|
|
*/ |
21
|
|
|
class OutputFormatter implements OutputFormatterInterface |
22
|
|
|
{ |
23
|
|
|
private $decorated; |
24
|
|
|
private $styles = array(); |
25
|
|
|
private $styleStack; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Escapes "<" special char in given text. |
29
|
|
|
* |
30
|
|
|
* @param string $text Text to escape |
31
|
|
|
* |
32
|
|
|
* @return string Escaped text |
33
|
|
|
*/ |
34
|
|
|
public static function escape($text) |
35
|
|
|
{ |
36
|
|
|
return preg_replace('/([^\\\\]?)</is', '$1\\<', $text); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Initializes console output formatter. |
41
|
|
|
* |
42
|
|
|
* @param bool $decorated Whether this formatter should actually decorate strings |
43
|
|
|
* @param OutputFormatterStyleInterface[] $styles Array of "name => FormatterStyle" instances |
44
|
|
|
* |
45
|
|
|
* @api |
46
|
|
|
*/ |
47
|
|
|
public function __construct($decorated = false, array $styles = array()) |
48
|
|
|
{ |
49
|
|
|
$this->decorated = (bool) $decorated; |
50
|
|
|
|
51
|
|
|
$this->setStyle('error', new OutputFormatterStyle('white', 'red')); |
52
|
|
|
$this->setStyle('info', new OutputFormatterStyle('green')); |
53
|
|
|
$this->setStyle('comment', new OutputFormatterStyle('yellow')); |
54
|
|
|
$this->setStyle('question', new OutputFormatterStyle('black', 'cyan')); |
55
|
|
|
|
56
|
|
|
foreach ($styles as $name => $style) { |
57
|
|
|
$this->setStyle($name, $style); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$this->styleStack = new OutputFormatterStyleStack(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Sets the decorated flag. |
65
|
|
|
* |
66
|
|
|
* @param bool $decorated Whether to decorate the messages or not |
67
|
|
|
* |
68
|
|
|
* @api |
69
|
|
|
*/ |
70
|
|
|
public function setDecorated($decorated) |
71
|
|
|
{ |
72
|
|
|
$this->decorated = (bool) $decorated; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Gets the decorated flag. |
77
|
|
|
* |
78
|
|
|
* @return bool true if the output will decorate messages, false otherwise |
79
|
|
|
* |
80
|
|
|
* @api |
81
|
|
|
*/ |
82
|
|
|
public function isDecorated() |
83
|
|
|
{ |
84
|
|
|
return $this->decorated; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Sets a new style. |
89
|
|
|
* |
90
|
|
|
* @param string $name The style name |
91
|
|
|
* @param OutputFormatterStyleInterface $style The style instance |
92
|
|
|
* |
93
|
|
|
* @api |
94
|
|
|
*/ |
95
|
|
|
public function setStyle($name, OutputFormatterStyleInterface $style) |
96
|
|
|
{ |
97
|
|
|
$this->styles[strtolower($name)] = $style; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Checks if output formatter has style with specified name. |
102
|
|
|
* |
103
|
|
|
* @param string $name |
104
|
|
|
* |
105
|
|
|
* @return bool |
106
|
|
|
* |
107
|
|
|
* @api |
108
|
|
|
*/ |
109
|
|
|
public function hasStyle($name) |
110
|
|
|
{ |
111
|
|
|
return isset($this->styles[strtolower($name)]); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Gets style options from style with specified name. |
116
|
|
|
* |
117
|
|
|
* @param string $name |
118
|
|
|
* |
119
|
|
|
* @return OutputFormatterStyleInterface |
120
|
|
|
* |
121
|
|
|
* @throws \InvalidArgumentException When style isn't defined |
122
|
|
|
* |
123
|
|
|
* @api |
124
|
|
|
*/ |
125
|
|
|
public function getStyle($name) |
126
|
|
|
{ |
127
|
|
|
if (!$this->hasStyle($name)) { |
128
|
|
|
throw new \InvalidArgumentException(sprintf('Undefined style: %s', $name)); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return $this->styles[strtolower($name)]; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Formats a message according to the given styles. |
136
|
|
|
* |
137
|
|
|
* @param string $message The message to style |
138
|
|
|
* |
139
|
|
|
* @return string The styled message |
140
|
|
|
* |
141
|
|
|
* @api |
142
|
|
|
*/ |
143
|
|
|
public function format($message) |
144
|
|
|
{ |
145
|
|
|
$offset = 0; |
146
|
|
|
$output = ''; |
147
|
|
|
$tagRegex = '[a-z][a-z0-9_=;-]*'; |
148
|
|
|
preg_match_all("#<(($tagRegex) | /($tagRegex)?)>#isx", $message, $matches, PREG_OFFSET_CAPTURE); |
149
|
|
|
foreach ($matches[0] as $i => $match) { |
150
|
|
|
$pos = $match[1]; |
151
|
|
|
$text = $match[0]; |
152
|
|
|
|
153
|
|
|
if (0 != $pos && '\\' == $message[$pos - 1]) { |
154
|
|
|
continue; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
// add the text up to the next tag |
158
|
|
|
$output .= $this->applyCurrentStyle(substr($message, $offset, $pos - $offset)); |
159
|
|
|
$offset = $pos + strlen($text); |
160
|
|
|
|
161
|
|
|
// opening tag? |
162
|
|
|
if ($open = '/' != $text[1]) { |
163
|
|
|
$tag = $matches[1][$i][0]; |
164
|
|
|
} else { |
165
|
|
|
$tag = isset($matches[3][$i][0]) ? $matches[3][$i][0] : ''; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
if (!$open && !$tag) { |
169
|
|
|
// </> |
170
|
|
|
$this->styleStack->pop(); |
171
|
|
|
} elseif (false === $style = $this->createStyleFromString(strtolower($tag))) { |
172
|
|
|
$output .= $this->applyCurrentStyle($text); |
173
|
|
|
} elseif ($open) { |
174
|
|
|
$this->styleStack->push($style); |
|
|
|
|
175
|
|
|
} else { |
176
|
|
|
$this->styleStack->pop($style); |
|
|
|
|
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
$output .= $this->applyCurrentStyle(substr($message, $offset)); |
181
|
|
|
|
182
|
|
|
return str_replace('\\<', '<', $output); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @return OutputFormatterStyleStack |
187
|
|
|
*/ |
188
|
|
|
public function getStyleStack() |
189
|
|
|
{ |
190
|
|
|
return $this->styleStack; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Tries to create new style instance from string. |
195
|
|
|
* |
196
|
|
|
* @param string $string |
197
|
|
|
* |
198
|
|
|
* @return OutputFormatterStyle|bool false if string is not format string |
199
|
|
|
*/ |
200
|
|
|
private function createStyleFromString($string) |
201
|
|
|
{ |
202
|
|
|
if (isset($this->styles[$string])) { |
203
|
|
|
return $this->styles[$string]; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
if (!preg_match_all('/([^=]+)=([^;]+)(;|$)/', strtolower($string), $matches, PREG_SET_ORDER)) { |
207
|
|
|
return false; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
$style = new OutputFormatterStyle(); |
211
|
|
|
foreach ($matches as $match) { |
212
|
|
|
array_shift($match); |
213
|
|
|
|
214
|
|
|
if ('fg' == $match[0]) { |
215
|
|
|
$style->setForeground($match[1]); |
216
|
|
|
} elseif ('bg' == $match[0]) { |
217
|
|
|
$style->setBackground($match[1]); |
218
|
|
|
} else { |
219
|
|
|
try { |
220
|
|
|
$style->setOption($match[1]); |
221
|
|
|
} catch (\InvalidArgumentException $e) { |
222
|
|
|
return false; |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
return $style; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Applies current style from stack to text, if must be applied. |
232
|
|
|
* |
233
|
|
|
* @param string $text Input text |
234
|
|
|
* |
235
|
|
|
* @return string Styled text |
236
|
|
|
*/ |
237
|
|
|
private function applyCurrentStyle($text) |
238
|
|
|
{ |
239
|
|
|
return $this->isDecorated() && strlen($text) > 0 ? $this->styleStack->getCurrent()->apply($text) : $text; |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.