|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace eXpansion\Core\Services; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Output\ConsoleOutput; |
|
6
|
|
|
use Symfony\Component\Console\Output\ConsoleOutputInterface; |
|
7
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
8
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
class Console |
|
11
|
|
|
{ |
|
12
|
|
|
|
|
13
|
|
|
const black = "\e[0;30m"; |
|
14
|
|
|
const b_black = "\e[30;1m"; |
|
15
|
|
|
|
|
16
|
|
|
const red = "\e[0;31m"; |
|
17
|
|
|
const b_red = "\e[31;1m"; |
|
18
|
|
|
|
|
19
|
|
|
const green = "\e[0;32m"; |
|
20
|
|
|
const b_green = "\e[32;1m"; |
|
21
|
|
|
|
|
22
|
|
|
const yellow = "\e[0;33m"; |
|
23
|
|
|
const b_yellow = "\e[33;1m"; |
|
24
|
|
|
|
|
25
|
|
|
const blue = "\e[0;34m"; |
|
26
|
|
|
const b_blue = "\e[34;1m"; |
|
27
|
|
|
|
|
28
|
|
|
const magenta = "\e[0;35m"; |
|
29
|
|
|
const b_magenta = "\e[35;1m"; |
|
30
|
|
|
|
|
31
|
|
|
const cyan = "\e[0;36m"; |
|
32
|
|
|
const b_cyan = "\e[36;1m"; |
|
33
|
|
|
|
|
34
|
|
|
const white = "\e[0;37m"; |
|
35
|
|
|
const b_white = "\e[37;1m"; |
|
36
|
|
|
|
|
37
|
|
|
// define aliases for colors |
|
38
|
|
|
const error = "\e[37;1m\e[41m"; |
|
39
|
|
|
const success = self::b_green; |
|
40
|
|
|
const normal = self::white; |
|
41
|
|
|
const bold = self::b_white; |
|
42
|
|
|
|
|
43
|
|
|
/** @var ContainerInterface */ |
|
44
|
|
|
protected $container; |
|
45
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
/** @var ConsoleOutput */ |
|
48
|
|
|
protected $consoleOutput; |
|
49
|
|
|
|
|
50
|
|
|
function __construct(ContainerInterface $container) |
|
|
|
|
|
|
51
|
|
|
{ |
|
52
|
|
|
$this->container = $container; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
function init(ConsoleOutputInterface $consoleOutput) |
|
|
|
|
|
|
56
|
|
|
{ |
|
57
|
|
|
$this->consoleOutput = $consoleOutput; |
|
|
|
|
|
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
function writeln($string) |
|
|
|
|
|
|
61
|
|
|
{ |
|
62
|
|
|
$this->write($string, true); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
function write($string, $newline = false) |
|
|
|
|
|
|
66
|
|
|
{ |
|
67
|
|
|
$array = array("000" => self::b_black, |
|
68
|
|
|
"100" => self::red, |
|
69
|
|
|
"010" => self::green, |
|
70
|
|
|
"110" => self::yellow, |
|
71
|
|
|
"001" => self::blue, |
|
72
|
|
|
"011" => self::magenta, |
|
73
|
|
|
"101" => self::cyan, |
|
74
|
|
|
"111" => self::white, |
|
75
|
|
|
"200" => self::b_red, |
|
76
|
|
|
"211" => self::red, |
|
77
|
|
|
"121" => self::green, |
|
78
|
|
|
"020" => self::b_green, |
|
79
|
|
|
"021" => self::green, |
|
80
|
|
|
"012" => self::cyan, |
|
81
|
|
|
"221" => self::b_yellow, |
|
82
|
|
|
"220" => self::b_yellow, |
|
83
|
|
|
"120" => self::green, |
|
84
|
|
|
"210" => self::yellow, |
|
85
|
|
|
"112" => self::b_blue, |
|
86
|
|
|
"002" => self::b_blue, |
|
87
|
|
|
"122" => self::b_cyan, |
|
88
|
|
|
"022" => self::b_cyan, |
|
89
|
|
|
"202" => self::b_magenta, |
|
90
|
|
|
"212" => self::b_magenta, |
|
91
|
|
|
"102" => self::magenta, |
|
92
|
|
|
"201" => self::b_red, |
|
93
|
|
|
"222" => self::b_white, |
|
94
|
|
|
); |
|
95
|
|
|
$matches = array(); |
|
96
|
|
|
preg_match_all("/\\$[A-Fa-f0-9]{3}/", $string, $matches); |
|
97
|
|
|
$split = preg_split("/\\$[A-Fa-f0-9]{3}/", $string); |
|
98
|
|
|
|
|
99
|
|
|
$out = ""; |
|
100
|
|
|
foreach ($matches[0] as $i => $rgb) { |
|
101
|
|
|
$code = $this->fix(hexdec($rgb[1]), hexdec($rgb[2]), hexdec($rgb[3])); |
|
102
|
|
|
if (array_key_exists($code, $array)) { |
|
103
|
|
|
$out .= $array[$code] . $this->stripStyles($split[$i + 1]); |
|
104
|
|
|
} else { |
|
105
|
|
|
$out .= self::white . $this->stripStyles($split[$i + 1]); |
|
106
|
|
|
} |
|
107
|
|
|
$end = $this->stripStyles($split[$i + 1]); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
|
|
111
|
|
|
if (!empty($end)) { |
|
112
|
|
|
if ($end == $this->stripStyles(end($split))) { |
|
113
|
|
|
$end = ""; |
|
114
|
|
|
} |
|
115
|
|
|
} else { |
|
116
|
|
|
$end = ""; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
$out = self::white . $this->stripStyles(reset($split)) . $out . $end; |
|
120
|
|
|
|
|
121
|
|
|
$this->ansiOut($out, $newline); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
|
|
125
|
|
|
private function ansiOut($msg, $newline) |
|
126
|
|
|
{ |
|
127
|
|
|
/*if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { |
|
|
|
|
|
|
128
|
|
|
$msg = str_replace("\e", "", $msg); |
|
129
|
|
|
$msg = preg_replace("/\[(\d{1,2}\;){0,1}\d{1,2}m/", "", $msg); |
|
130
|
|
|
} */ |
|
131
|
|
|
|
|
132
|
|
|
$this->consoleOutput->write($msg, $newline, ConsoleOutput::OUTPUT_RAW); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
|
|
136
|
|
|
private function stripStyles($string) |
|
137
|
|
|
{ |
|
138
|
|
|
|
|
139
|
|
|
$string = preg_replace('/(?<!\$)((?:\$\$)*)\$[^$0-9a-hlp]/iu', '$1', $string); |
|
140
|
|
|
return preg_replace('/(?<!\$)((?:\$\$)*)\$(?:g|[0-9a-f][^\$]{0,2})/iu', '$1', $string); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
|
|
144
|
|
|
public function fix($r, $g, $b) |
|
145
|
|
|
{ |
|
146
|
|
|
$out = "111"; |
|
147
|
|
|
// black/gray/white |
|
148
|
|
|
if ($r == $g && $g == $b && $b == $r) { |
|
149
|
|
|
if ($r >= 0 && $r < 5) { |
|
150
|
|
|
$out = "000"; |
|
151
|
|
|
} |
|
152
|
|
|
if ($r >= 5 && $r < 13) { |
|
153
|
|
|
$out = "111"; |
|
154
|
|
|
} |
|
155
|
|
|
if ($r >= 13 && $r <= 16) { |
|
156
|
|
|
$out = "222"; |
|
157
|
|
|
} |
|
158
|
|
|
} else { |
|
159
|
|
|
$out = $this->convert($r) . $this->convert($g) . $this->convert($b); |
|
160
|
|
|
} |
|
161
|
|
|
return $out; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
public function convert($number) |
|
165
|
|
|
{ |
|
166
|
|
|
$out = "0"; |
|
167
|
|
|
|
|
168
|
|
|
if ($number >= 9 && $number <= 16) { |
|
169
|
|
|
$out = "2"; |
|
170
|
|
|
} |
|
171
|
|
|
if ($number >= 3 && $number < 9) { |
|
172
|
|
|
$out = "1"; |
|
173
|
|
|
} |
|
174
|
|
|
if ($number >= 0 && $number < 3) { |
|
175
|
|
|
$out = "0"; |
|
176
|
|
|
} |
|
177
|
|
|
return $out; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
|
|
181
|
|
|
} |
|
182
|
|
|
|
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.