Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
html_output_formatter
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 2
72
0.00% covered (danger)
0.00%
0 / 1
 format
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 formatHtml
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
56
1<?php
2/**
3 *
4 * This file is part of the phpBB Forum Software package.
5 *
6 * @copyright (c) phpBB Limited <https://www.phpbb.com>
7 * @license       GNU General Public License, version 2 (GPL-2.0)
8 *
9 * For full copyright and license information, please see
10 * the docs/CREDITS.txt file.
11 *
12 */
13
14namespace phpbb\composer\io;
15
16class html_output_formatter extends \Composer\Console\HtmlOutputFormatter
17{
18    protected static $availableForegroundColors = [
19            30 => 'black',
20            31 => 'red',
21            32 => 'green',
22            33 => 'orange',
23            34 => 'blue',
24            35 => 'magenta',
25            36 => 'cyan',
26            37 => 'white',
27        ];
28
29    protected static $availableBackgroundColors = [
30            40 => 'black',
31            41 => 'red',
32            42 => 'green',
33            43 => 'yellow',
34            44 => 'blue',
35            45 => 'magenta',
36            46 => 'cyan',
37            47 => 'white',
38        ];
39
40    protected static $availableOptions
41        = [
42            1 => 'bold',
43            4 => 'underscore',
44        ];
45
46    /**
47     * {@inheritdoc}
48     */
49    public function format(?string $message): ?string
50    {
51        $formatted = parent::format($message);
52
53        return preg_replace_callback("{[\033\e]\[([0-9;]+)m(.*?)[\033\e]\[[0-9;]+m}s", [$this, 'formatHtml'], $formatted);
54    }
55
56    protected function formatHtml($matches)
57    {
58        $out = '<span style="';
59        foreach (explode(';', $matches[1]) as $code)
60        {
61            if (isset(self::$availableForegroundColors[$code]))
62            {
63                $out .= 'color:' . self::$availableForegroundColors[$code] . ';';
64            }
65            else if (isset(self::$availableBackgroundColors[$code]))
66            {
67                $out .= 'background-color:' . self::$availableBackgroundColors[$code] . ';';
68            }
69            else if (isset(self::$availableOptions[$code]))
70            {
71                switch (self::$availableOptions[$code])
72                {
73                    case 'bold':
74                        $out .= 'font-weight:bold;';
75                        break;
76
77                    case 'underscore':
78                        $out .= 'text-decoration:underline;';
79                        break;
80                }
81            }
82        }
83
84        return $out . '">' . $matches[2] . '</span>';
85    }
86}