-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJsonPrinter.php
151 lines (130 loc) · 3.78 KB
/
JsonPrinter.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
<?php
declare(strict_types=1);
namespace Here;
use Here\Abstracts\Printer;
class JsonPrinter extends Printer
{
/** {@inheritdoc} */
public function info()
{
$this->sendJson([
'file' => $this->content['file'],
'line' => $this->content['line'],
]);
}
/** {@inheritdoc} */
public function dump(...$var)
{
/** @var string */
$file = $this->content['file'];
/** @var array<int, int> */
$capture = $this->content['capture'];
$snapshot = Here::getCapture($file, $capture);
$this->sendJson([
'file' => $this->content['file'],
'line' => $this->content['line'],
'snapshot' => array_map(fn ($trim) => trim($trim), $snapshot),
]);
}
/** {@inheritdoc} */
public function dumpAll()
{
$heres = Here::getHere();
array_pop($heres);
foreach ($heres as $here) {
(new self($here))->dump();
}
}
/** {@inheritdoc} */
public function count($group)
{
// 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'];
// counter
$count = array_filter($files, fn ($item) => $item['line'] === $result['line']);
// build snapshot
/** @var array<int, int> */
$capture = $result['capture'];
/** @var string */
$file_name = $result['file'];
$snapshot = Here::getCapture($file_name, $capture);
// send result
$this->sendJson([
'file' => $result['file'],
'line' => $result['line'],
'count' => $count,
'snapshot' => array_map(fn ($trim) => trim($trim), $snapshot),
]);
}
}
}
/** {@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'];
}
}
/**
* Send array to string.
*
* @param array<string, array<int, mixed>|int|string> $out
*
* @return void
*/
private function sendJson($out)
{
$this->send(json_encode($out));
}
/**
* {@inheritdoc}
*/
protected function send($out)
{
if (self::$mark_test) {
return;
}
$uri = Config::castString('socket.uri', '127.0.0.1:8080');
$out = $out === false ? '' : $out;
$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}
*/
protected function printInfo(&$print, $content, $with_counter = false)
{
}
/**
* {@inheritdoc}
*/
protected function printSnapshot(&$print, $content, ...$var)
{
}
}