class HtmlOutputFormatter
@author Jordi Boggiano <j.boggiano@seld.be>
Hierarchy
- class \Symfony\Component\Console\Formatter\OutputFormatter implements \Symfony\Component\Console\Formatter\WrappableOutputFormatterInterface
- class \Composer\Console\HtmlOutputFormatter extends \Symfony\Component\Console\Formatter\OutputFormatter
Expanded class hierarchy of HtmlOutputFormatter
File
-
vendor/
composer/ composer/ src/ Composer/ Console/ HtmlOutputFormatter.php, line 23
Namespace
Composer\ConsoleView source
class HtmlOutputFormatter extends OutputFormatter {
/** @var array<int, string> */
private static $availableForegroundColors = [
30 => 'black',
31 => 'red',
32 => 'green',
33 => 'yellow',
34 => 'blue',
35 => 'magenta',
36 => 'cyan',
37 => 'white',
];
/** @var array<int, string> */
private static $availableBackgroundColors = [
40 => 'black',
41 => 'red',
42 => 'green',
43 => 'yellow',
44 => 'blue',
45 => 'magenta',
46 => 'cyan',
47 => 'white',
];
/** @var array<int, string> */
private static $availableOptions = [
1 => 'bold',
4 => 'underscore',
];
/**
* @param array<string, OutputFormatterStyle> $styles Array of "name => FormatterStyle" instances
*/
public function __construct(array $styles = []) {
parent::__construct(true, $styles);
}
public function format(?string $message) : ?string {
$formatted = parent::format($message);
if ($formatted === null) {
return null;
}
$clearEscapeCodes = '(?:39|49|0|22|24|25|27|28)';
return Preg::replaceCallback("{\x1b\\[([0-9;]+)m(.*?)\x1b\\[(?:" . $clearEscapeCodes . ";)*?" . $clearEscapeCodes . "m}s", Closure::fromCallable([
$this,
'formatHtml',
]), $formatted);
}
/**
* @param array<string|null> $matches
*/
private function formatHtml(array $matches) : string {
assert(is_string($matches[1]));
$out = '<span style="';
foreach (explode(';', $matches[1]) as $code) {
if (isset(self::$availableForegroundColors[(int) $code])) {
$out .= 'color:' . self::$availableForegroundColors[(int) $code] . ';';
}
elseif (isset(self::$availableBackgroundColors[(int) $code])) {
$out .= 'background-color:' . self::$availableBackgroundColors[(int) $code] . ';';
}
elseif (isset(self::$availableOptions[(int) $code])) {
switch (self::$availableOptions[(int) $code]) {
case 'bold':
$out .= 'font-weight:bold;';
break;
case 'underscore':
$out .= 'text-decoration:underline;';
break;
}
}
}
return $out . '">' . $matches[2] . '</span>';
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overriden Title |
---|---|---|---|---|
HtmlOutputFormatter::$availableBackgroundColors | private static | property | @var array<int, string> | |
HtmlOutputFormatter::$availableForegroundColors | private static | property | @var array<int, string> | |
HtmlOutputFormatter::$availableOptions | private static | property | @var array<int, string> | |
HtmlOutputFormatter::format | public | function | Formats a message according to the given styles. | Overrides OutputFormatter::format |
HtmlOutputFormatter::formatHtml | private | function | ||
HtmlOutputFormatter::__construct | public | function | Overrides OutputFormatter::__construct | |
OutputFormatter::$styles | private | property | ||
OutputFormatter::$styleStack | private | property | ||
OutputFormatter::addLineBreaks | private | function | ||
OutputFormatter::applyCurrentStyle | private | function | Applies current style from stack to text, if must be applied. | |
OutputFormatter::createStyleFromString | private | function | Tries to create new style instance from string. | |
OutputFormatter::escape | public static | function | Escapes "<" and ">" special chars in given text. | |
OutputFormatter::escapeTrailingBackslash | public static | function | Escapes trailing "\" in given text. | |
OutputFormatter::formatAndWrap | public | function | Formats a message according to the given styles, wrapping at `$width` (0 means no wrapping). | Overrides WrappableOutputFormatterInterface::formatAndWrap |
OutputFormatter::getStyle | public | function | Gets style options from style with specified name. | Overrides OutputFormatterInterface::getStyle |
OutputFormatter::getStyleStack | public | function | ||
OutputFormatter::hasStyle | public | function | Checks if output formatter has style with specified name. | Overrides OutputFormatterInterface::hasStyle |
OutputFormatter::isDecorated | public | function | Whether the output will decorate messages. | Overrides OutputFormatterInterface::isDecorated |
OutputFormatter::setDecorated | public | function | Sets the decorated flag. | Overrides OutputFormatterInterface::setDecorated |
OutputFormatter::setStyle | public | function | Sets a new style. | Overrides OutputFormatterInterface::setStyle |
OutputFormatter::__clone | public | function |