Console   A
last analyzed

Complexity

Total Complexity 28

Size/Duplication

Total Lines 231
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 3.95%

Importance

Changes 0
Metric Value
wmc 28
c 0
b 0
f 0
lcom 1
cbo 6
dl 0
loc 231
rs 10
ccs 3
cts 76
cp 0.0395

9 Methods

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