-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPrinter.php
222 lines (183 loc) · 5.92 KB
/
Printer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
declare(strict_types=1);
namespace Here;
use Here\Abstracts\Printer as AbstractsPrinter;
use Here\Styles\ArrayStyle;
use Here\Styles\VarStyle;
use System\Console\Style\Style;
final class Printer extends AbstractsPrinter
{
/**
* Dump var in end of capture code.
*
* @var bool True if print in the end
*/
private $EOL_var = false;
/**
* Dump var in end of capture code.
*
* @param bool $EOL_var True if print in the end
*
* @return self
*/
public function printVarEndOfCode($EOL_var)
{
$this->EOL_var = $EOL_var;
return $this;
}
/** {@inheritdoc} */
protected function send($out)
{
if (self::$mark_test) {
return;
}
$use_socket = Config::get('socket.enable', false);
$uri = Config::castString('socket.uri', '127.0.0.1:8080');
$out = $out === false ? '' : $out;
if ($use_socket === false) {
echo $out;
return;
}
$connector = new \React\Socket\Connector();
$connector->connect($uri)->then(function (\React\Socket\ConnectionInterface $connection) use ($out) {
$connection->end($out);
}, function (\Throwable $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});
}
/** {@inheritdoc} */
public function info()
{
$print = new Style('');
$this->printInfo($print, $this->content);
$this->send($print->__toString());
}
/** {@inheritdoc} */
public function dump(...$var)
{
$print = new Style('');
// print header info
$this->printInfo($print, $this->content);
// print content
$this->printSnapshot($print, $this->content, ...$var);
$this->send($print->__toString());
}
/** {@inheritdoc} */
public function dumpAll()
{
$heres = Here::getHere();
array_pop($heres);
foreach ($heres as $here) {
(new static($here))->dump();
}
}
/** {@inheritdoc} */
public function count($group)
{
$print = new Style();
// group by file name
$group_file = [];
foreach (Here::getHere() as $files) {
if ($files['group'] === $group) {
/** @var string */
$file = $files['file'];
$group_file[$file][] = $files;
}
}
// group by line
$groups = [];
foreach ($group_file as $files) {
foreach ($files as $result) {
if (in_array($result['line'], $groups)) {
continue;
}
$groups[] = $result['line'];
$count = array_filter($files, fn ($item) => $item['line'] === $result['line']);
$this->printInfo($print, $result, count($count));
$this->printSnapshot($print, $result);
$this->send($print->__toString());
}
}
}
/** {@inheritdoc} */
public function countAll()
{
$indexed = [];
foreach (Here::getHere() as $here) {
if ($here['group'] === '' | in_array($here['group'], $indexed)) {
continue;
}
/** @var string */
$group = $here['group'];
$this->count($group);
$indexed[] = $here['group'];
}
}
// helper ----------------------------------------
/** {@inheritdoc} */
protected function printInfo(&$print, $content, $with_counter = false)
{
$print->newLines();
$print->push(' work ')->textDarkGray()->bgGreen();
if ($with_counter !== false) {
$print->push(' ' . $with_counter . 'x')->textDim();
}
$print->push(' - ')->textDim();
// @phpstan-ignore-next-line
$print->push('`' . $content['file'] . '`')->text_yellow_400();
$print->push(':')->textDim();
// @phpstan-ignore-next-line
$print->push('' . $content['line'])->text_blue_400();
}
/** {@inheritdoc} */
protected function printSnapshot(&$print, $content, ...$var)
{
$print->newLines();
$count = count($var);
$has_var = $count > 0;
if ($count === 1) {
$var = $var[0];
}
$max_line = ((int) $content['line']) + 3;
$lenght = \strlen((string) $max_line);
/** @var string */
$file = $content['file'];
/** @var array<int, int> */
$capture = $content['capture'];
$snapshot = Here::getCapture($file, $capture);
foreach ($snapshot as $line => $code) {
$current = $line === $content['line'];
$arrow = $current ? '-> ' : ' ';
$print->push($arrow)->textGreen();
$print->push(str_pad((string) $line, $lenght, ' ', STR_PAD_LEFT) . ' | ' . $code)->textDim();
if ($current
&& $has_var === true
&& $this->EOL_var === false
) {
$this->printVar($print, $var, $lenght);
continue;
}
}
if ($has_var === true && $this->EOL_var === true) {
$this->printVar($print, $var, $lenght);
}
}
/**
* helper, dump variable.
*
* @param Style &$style
* @param string|array<int|string, mixed>|false|null $var
* @param int $margin_left
*
* @return Style
*/
private function printVar(&$style, $var, $margin_left)
{
$style->push(str_pad('', $margin_left, ' ', STR_PAD_LEFT) . 'var : ')->textLightYellow();
$tab_size = (int) round($margin_left / 2);
$style = is_array($var)
? (new ArrayStyle($style))->ref($var)->tabSize($tab_size)->render()
: (new VarStyle($style))->ref($var)->tabSize($tab_size)->render();
return $style->newLines();
}
}