Completed
Pull Request — master (#165)
by
unknown
02:58
created

Console   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 213
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 26
lcom 1
cbo 4
dl 0
loc 213
rs 10
c 0
b 0
f 0
ccs 0
cts 71
cp 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A init() 0 5 1
A writeln() 0 4 1
C write() 0 35 7
A ansiOut() 0 8 2
A stripStyles() 0 4 1
C doHslConvert() 0 55 11
A getConsoleOutput() 0 8 2
1
<?php
2
3
namespace eXpansion\Framework\Core\Services;
4
5
use eXpansion\Framework\Core\Helpers\ColorConversion;
6
use eXpansion\Framework\Core\Services\Application\Dispatcher;
7
use Symfony\Component\Console\Output\NullOutput;
8
use Symfony\Component\Console\Output\OutputInterface;
9
10
/**
11
 * Class Console to print in the console.
12
 *
13
 * @package eXpansion\Framework\Core\Services
14
 * @author  Reaby
15
 */
16
class Console
17
{
18
19
    const black = "\e[0;30m";
20
    const b_black = "\e[30;1m";
21
22
    const red = "\e[0;31m";
23
    const b_red = "\e[1;31m";
24
25
    const green = "\e[0;32m";
26
    const b_green = "\e[1;32m";
27
28
    const yellow = "\e[0;33m";
29
    const b_yellow = "\e[1;33m";
30
31
    const blue = "\e[0;34m";
32
    const b_blue = "\e[1;34m";
33
34
    const magenta = "\e[0;35m";
35
    const b_magenta = "\e[1;35m";
36
37
    const cyan = "\e[0;36m";
38
    const b_cyan = "\e[1;36m";
39
40
    const white = "\e[0;37m";
41
    const b_white = "\e[1;37m";
42
43
    // define aliases for colors
44
    const error = "\e[37;1m\e[41m";
45
    const success = self::b_green;
46
    const normal = "\e[0m";
47
    const bold = self::b_white;
48
49
50
    /** @var OutputInterface */
51
    protected $consoleOutput;
52
53
    /** @var boolean Color console enabled */
54
    protected $colorEnabled;
55
    /**
56
     * @var Dispatcher
57
     */
58
    private $dispatcher;
59
60
    /**
61
     * Console constructor.
62
     *
63
     * @param bool $colorEnabled
64
     */
65
    public function __construct($colorEnabled)
66
    {
67
        $this->colorEnabled = $colorEnabled;
68
    }
69
70
71
    /**
72
     * Initialize service with the console output.
73
     *
74
     * @param OutputInterface $consoleOutput
75
     * @param                 $dispatcher
76
     */
77
    public function init(OutputInterface $consoleOutput, $dispatcher)
78
    {
79
        $this->consoleOutput = $consoleOutput;
80
        $this->dispatcher = $dispatcher;
81
    }
82
83
    /**
84
     * @inheritdoc
85
     */
86
    public function writeln($string)
87
    {
88
        $this->write($string, true);
89
    }
90
91
    /**
92
     * @inheritdoc
93
     */
94
    public function write($string, $newline = false)
95
    {
96
        if ($this->dispatcher instanceof Dispatcher) {
97
            $this->dispatcher->dispatch("expansion.console.message", [$string]);
98
        }
99
100
        if ($this->colorEnabled && $this->consoleOutput->isDecorated()) {
101
102
            $matches = array();
103
            preg_match_all("/\\$[A-Fa-f0-9]{3}/", $string, $matches);
104
            $split = preg_split("/\\$[A-Fa-f0-9]{3}/", $string);
105
            $out = "";
106
107
            foreach ($matches[0] as $i => $rgb) {
108
                $code = $this->doHslConvert(hexdec($rgb[1].$rgb[1]), hexdec($rgb[2].$rgb[2]), hexdec($rgb[3].$rgb[3]));
109
                $out .= $code.$this->stripStyles($split[$i + 1]);
110
                $end = $this->stripStyles($split[$i + 1]);
111
            }
112
113
114
            if (!empty($end)) {
115
                if ($end == $this->stripStyles(end($split))) {
116
                    $end = "";
117
                }
118
            } else {
119
                $end = "";
120
            }
121
122
            $out = self::white.$this->stripStyles(reset($split)).$out.$end.self::normal;
123
        } else {
124
            $out = $this->stripStyles($string);
125
        }
126
127
        $this->ansiOut($out, $newline);
128
    }
129
130
    /**
131
     * Outoyt brute text.
132
     *
133
     * @param string  $msg
134
     * @param boolean $newline
135
     *
136
     */
137
    protected function ansiOut($msg, $newline)
138
    {
139
        $nl = "";
140
        if ($newline) {
141
            $nl = "\n";
142
        }
143
        echo $msg.$nl;
144
    }
145
146
    /**
147
     * Strip styles from a string.
148
     *
149
     * @param $string
150
     *
151
     * @return mixed
152
     */
153
    protected function stripStyles($string)
154
    {
155
        return preg_replace('/(\$[wnoitsgz><]|\$[lh]\[.+\]|\$[lh]|\$[0-9a-f]{3})+/i', '', $string);
156
    }
157
158
159
    protected function doHslConvert($r, $g, $b)
160
    {
161
        $hsl = ColorConversion::rgbToHsl($r, $g, $b);
162
163
        $lightness = 100 * $hsl[2];
164
165
        $color = "37";
166
        // if color has saturation
167
        if ($hsl[1] > 0) {
168
            $h = $hsl[0];
169
            if ($h < 20) {
170
                $color = "31";
171
            } else {
172
                if ($h < 70) {
173
                    $color = "33"; // yellow
174
                } else {
175
                    if ($h < 160) {
176
                        $color = "32"; // green
177
                    } else {
178
                        if ($h < 214) {
179
                            $color = "36"; // cyan
180
                        } else {
181
                            if ($h < 284) {
182
                                $color = "34"; // blue
183
                            } else {
184
                                if ($h < 333) {
185
                                    $color = "35"; // magenta
186
                                } else {
187
                                    $color = "31"; // red
188
                                }
189
                            }
190
                        }
191
                    }
192
                }
193
            }
194
        }
195
196
        if ($lightness < 10) {
197
            $attr = "1";
198
            $color = "30";
199
        } else {
200
            if ($lightness < 44) {
201
                $attr = "0";
202
            } else {
203
                if ($lightness < 95) {
204
                    $attr = "1";
205
                } else {
206
                    $color = "37";
207
                    $attr = "1";
208
                }
209
            }
210
        }
211
212
        return "\e[".$attr.";".$color."m";
213
    }
214
215
    /**
216
     * Get symphony console.
217
     *
218
     * @return OutputInterface
219
     */
220
    public function getConsoleOutput()
221
    {
222
        if (is_null($this->consoleOutput)) {
223
            $this->consoleOutput = new NullOutput();
224
        }
225
226
        return $this->consoleOutput;
227
    }
228
}
229