| 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 |
use Symfony\Component\Console\Exception\InvalidArgumentException; |
| 15 |
use Symfony\Component\Console\Helper\Helper; |
| 16 |
|
| 17 |
use function Symfony\Component\String\b; |
| 18 |
|
| 19 |
/** |
| 20 |
* Formatter class for console output. |
| 21 |
* |
| 22 |
* @author Konstantin Kudryashov <[email protected]> |
| 23 |
* @author Roland Franssen <[email protected]> |
| 24 |
*/ |
| 25 |
class OutputFormatter implements WrappableOutputFormatterInterface |
| 26 |
{ |
| 27 |
private array $styles = []; |
| 28 |
private OutputFormatterStyleStack $styleStack; |
| 29 |
|
| 30 |
public function __clone() |
| 31 |
{ |
| 32 |
$this->styleStack = clone $this->styleStack; |
| 33 |
foreach ($this->styles as $key => $value) { |
| 34 |
$this->styles[$key] = clone $value; |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Escapes "<" and ">" special chars in given text. |
| 40 |
*/ |
| 41 |
public static function escape(string $text): string |
| 42 |
{ |
| 43 |
$text = preg_replace('/([^\\\\]|^)([<>])/', '$1\\\\$2', $text); |
| 44 |
|
| 45 |
return self::escapeTrailingBackslash($text); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Escapes trailing "\" in given text. |
| 50 |
* |
| 51 |
* @internal |
| 52 |
*/ |
| 53 |
public static function escapeTrailingBackslash(string $text): string |
| 54 |
{ |
| 55 |
if (str_ends_with($text, '\\')) { |
| 56 |
$len = \strlen($text); |
| 57 |
$text = rtrim($text, '\\'); |
| 58 |
$text = str_replace("\0", '', $text); |
| 59 |
$text .= str_repeat("\0", $len - \strlen($text)); |
| 60 |
} |
| 61 |
|
| 62 |
return $text; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Initializes console output formatter. |
| 67 |
* |
| 68 |
* @param OutputFormatterStyleInterface[] $styles Array of "name => FormatterStyle" instances |
| 69 |
*/ |
| 70 |
public function __construct( |
| 71 |
private bool $decorated = false, |
| 72 |
array $styles = [], |
| 73 |
) { |
| 74 |
$this->setStyle('error', new OutputFormatterStyle('white', 'red')); |
| 75 |
$this->setStyle('info', new OutputFormatterStyle('green')); |
| 76 |
$this->setStyle('comment', new OutputFormatterStyle('yellow')); |
| 77 |
$this->setStyle('question', new OutputFormatterStyle('black', 'cyan')); |
| 78 |
|
| 79 |
foreach ($styles as $name => $style) { |
| 80 |
$this->setStyle($name, $style); |
| 81 |
} |
| 82 |
|
| 83 |
$this->styleStack = new OutputFormatterStyleStack(); |
| 84 |
} |
| 85 |
|
| 86 |
public function setDecorated(bool $decorated): void |
| 87 |
{ |
| 88 |
$this->decorated = $decorated; |
| 89 |
} |
| 90 |
|
| 91 |
public function isDecorated(): bool |
| 92 |
{ |
| 93 |
return $this->decorated; |
| 94 |
} |
| 95 |
|
| 96 |
public function setStyle(string $name, OutputFormatterStyleInterface $style): void |
| 97 |
{ |
| 98 |
$this->styles[strtolower($name)] = $style; |
| 99 |
} |
| 100 |
|
| 101 |
public function hasStyle(string $name): bool |
| 102 |
{ |
| 103 |
return isset($this->styles[strtolower($name)]); |
| 104 |
} |
| 105 |
|
| 106 |
public function getStyle(string $name): OutputFormatterStyleInterface |
| 107 |
{ |
| 108 |
if (!$this->hasStyle($name)) { |
| 109 |
throw new InvalidArgumentException(\sprintf('Undefined style: "%s".', $name)); |
| 110 |
} |
| 111 |
|
| 112 |
return $this->styles[strtolower($name)]; |
| 113 |
} |
| 114 |
|
| 115 |
public function format(?string $message): ?string |
| 116 |
{ |
| 117 |
return $this->formatAndWrap($message, 0); |
| 118 |
} |
| 119 |
|
| 120 |
public function formatAndWrap(?string $message, int $width): string |
| 121 |
{ |
| 122 |
if (null === $message) { |
| 123 |
return ''; |
| 124 |
} |
| 125 |
|
| 126 |
// For ASCII-only strings, byte positions equal character positions, |
| 127 |
// so we can use native strlen/substr which is much faster than Helper::length/substr. |
| 128 |
$isAscii = !preg_match('/[\x80-\xFF]/', $message); |
| 129 |
|
| 130 |
$offset = 0; |
| 131 |
$output = ''; |
| 132 |
$openTagRegex = '[a-z](?:[^\\\\<>]*+ | \\\\.)*'; |
| 133 |
$closeTagRegex = '[a-z][^<>]*+'; |
| 134 |
$currentLineLength = 0; |
| 135 |
preg_match_all("#<(($openTagRegex) | /($closeTagRegex)?)>#ix", $message, $matches, \PREG_OFFSET_CAPTURE); |
| 136 |
foreach ($matches[0] as $i => $match) { |
| 137 |
$pos = $match[1]; |
| 138 |
$text = $match[0]; |
| 139 |
|
| 140 |
if (0 != $pos && '\\' == $message[$pos - 1]) { |
| 141 |
continue; |
| 142 |
} |
| 143 |
|
| 144 |
if ($isAscii) { |
| 145 |
// For ASCII, byte position = character position, no conversion needed |
| 146 |
$output .= $this->applyCurrentStyle(substr($message, $offset, $pos - $offset), $output, $width, $currentLineLength); |
| 147 |
$offset = $pos + \strlen($text); |
| 148 |
} else { |
| 149 |
// convert byte position to character position. |
| 150 |
$pos = Helper::length(substr($message, 0, $pos)); |
| 151 |
// add the text up to the next tag |
| 152 |
$output .= $this->applyCurrentStyle(Helper::substr($message, $offset, $pos - $offset), $output, $width, $currentLineLength); |
| 153 |
$offset = $pos + Helper::length($text); |
| 154 |
} |
| 155 |
|
| 156 |
// opening tag? |
| 157 |
if ($open = '/' !== $text[1]) { |
| 158 |
$tag = $matches[1][$i][0]; |
| 159 |
} else { |
| 160 |
$tag = $matches[3][$i][0] ?? ''; |
| 161 |
} |
| 162 |
|
| 163 |
if (!$open && !$tag) { |
| 164 |
// </> |
| 165 |
$this->styleStack->pop(); |
| 166 |
} elseif (null === $style = $this->createStyleFromString($tag)) { |
| 167 |
$output .= $this->applyCurrentStyle($text, $output, $width, $currentLineLength); |
| 168 |
} elseif ($open) { |
| 169 |
$this->styleStack->push($style); |
| 170 |
} else { |
| 171 |
$this->styleStack->pop($style); |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
$output .= $this->applyCurrentStyle($isAscii ? substr($message, $offset) : Helper::substr($message, $offset), $output, $width, $currentLineLength); |
| 176 |
|
| 177 |
return strtr($output, ["\0" => '\\', '\\<' => '<', '\\>' => '>']); |
| 178 |
} |
| 179 |
|
| 180 |
public function getStyleStack(): OutputFormatterStyleStack |
| 181 |
{ |
| 182 |
return $this->styleStack; |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Tries to create new style instance from string. |
| 187 |
*/ |
| 188 |
private function createStyleFromString(string $string): ?OutputFormatterStyleInterface |
| 189 |
{ |
| 190 |
if (isset($this->styles[$string])) { |
| 191 |
return $this->styles[$string]; |
| 192 |
} |
| 193 |
|
| 194 |
if (!preg_match_all('/([^=]+)=([^;]+)(;|$)/', $string, $matches, \PREG_SET_ORDER)) { |
| 195 |
return null; |
| 196 |
} |
| 197 |
|
| 198 |
$style = new OutputFormatterStyle(); |
| 199 |
foreach ($matches as $match) { |
| 200 |
array_shift($match); |
| 201 |
$match[0] = strtolower($match[0]); |
| 202 |
|
| 203 |
if ('fg' == $match[0]) { |
| 204 |
$style->setForeground(strtolower($match[1])); |
| 205 |
} elseif ('bg' == $match[0]) { |
| 206 |
$style->setBackground(strtolower($match[1])); |
| 207 |
} elseif ('href' === $match[0]) { |
| 208 |
$url = preg_replace('{\\\\([<>])}', '$1', $match[1]); |
| 209 |
$style->setHref($url); |
| 210 |
} elseif ('options' === $match[0]) { |
| 211 |
preg_match_all('([^,;]+)', strtolower($match[1]), $options); |
| 212 |
$options = array_shift($options); |
| 213 |
foreach ($options as $option) { |
| 214 |
$style->setOption($option); |
| 215 |
} |
| 216 |
} else { |
| 217 |
return null; |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
return $style; |
| 222 |
} |
| 223 |
|
| 224 |
/** |
| 225 |
* Applies current style from stack to text, if must be applied. |
| 226 |
*/ |
| 227 |
private function applyCurrentStyle(string $text, string $current, int $width, int &$currentLineLength): string |
| 228 |
{ |
| 229 |
if ('' === $text) { |
| 230 |
return ''; |
| 231 |
} |
| 232 |
|
| 233 |
if (!$width) { |
| 234 |
return $this->isDecorated() ? $this->styleStack->getCurrent()->apply($text) : $text; |
| 235 |
} |
| 236 |
|
| 237 |
if (!$currentLineLength && '' !== $current) { |
| 238 |
$text = ltrim($text); |
| 239 |
} |
| 240 |
|
| 241 |
if ($currentLineLength) { |
| 242 |
$lines = explode("\n", $text, 2); |
| 243 |
$prefix = Helper::substr($lines[0], 0, $i = $width - $currentLineLength)."\n"; |
| 244 |
$text = Helper::substr($lines[0], $i); |
| 245 |
|
| 246 |
if (isset($lines[1])) { |
| 247 |
// $prefix may contain the full first line in which the \n is already a part of $prefix. |
| 248 |
if ('' !== $text) { |
| 249 |
$text .= "\n"; |
| 250 |
} |
| 251 |
|
| 252 |
$text .= $lines[1]; |
| 253 |
} |
| 254 |
} else { |
| 255 |
$prefix = ''; |
| 256 |
} |
| 257 |
|
| 258 |
preg_match('~(\\n)$~', $text, $matches); |
| 259 |
$text = $prefix.$this->addLineBreaks($text, $width); |
| 260 |
$text = rtrim($text, "\n").($matches[1] ?? ''); |
| 261 |
|
| 262 |
if (!$currentLineLength && '' !== $current && !str_ends_with($current, "\n")) { |
| 263 |
$text = "\n".$text; |
| 264 |
} |
| 265 |
|
| 266 |
$lines = explode("\n", $text); |
| 267 |
|
| 268 |
foreach ($lines as $i => $line) { |
| 269 |
$currentLineLength = 0 === $i ? $currentLineLength + Helper::length($line) : Helper::length($line); |
| 270 |
if ($width <= $currentLineLength) { |
| 271 |
$currentLineLength = 0; |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
if ($this->isDecorated()) { |
| 276 |
foreach ($lines as $i => $line) { |
| 277 |
$lines[$i] = $this->styleStack->getCurrent()->apply($line); |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
return implode("\n", $lines); |
| 282 |
} |
| 283 |
|
| 284 |
private function addLineBreaks(string $text, int $width): string |
| 285 |
{ |
| 286 |
$encoding = mb_detect_encoding($text, null, true) ?: 'UTF-8'; |
| 287 |
|
| 288 |
return b($text)->toUnicodeString($encoding)->wordwrap($width, "\n", true)->toByteString($encoding); |
| 289 |
} |
| 290 |
} |