-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathServeCommand.php
197 lines (162 loc) · 5.25 KB
/
ServeCommand.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
<?php
declare(strict_types=1);
namespace Here\Commands;
use Here\Config;
use System\Console\Command;
use System\Console\Style\Style;
use System\Console\Traits\PrintHelpTrait;
use function System\Console\info;
use function System\Console\style;
use function System\Console\warn;
final class ServeCommand extends Command
{
use PrintHelpTrait;
public function main()
{
Config::load();
/** @var string */
$option = $this->CMD;
$option = strtolower($option);
switch ($option) {
case 'serve':
$this->serve();
break;
case 'config':
$this->config();
break;
default:
$this->help()->out();
break;
}
}
/**
* Serve socket.
*
* @return void
*/
public function serve()
{
$style = new Style();
if (Config::get('socket.enable', false) === false) {
$style->tap(
warn('Runing server with socket report disable')
);
$style->push('Enable socket report?')->textYellow();
$style->push(' (yes) ');
$style->out(false);
if (!$this->promt()) {
exit;
}
$style->newLines()->flush();
}
/** @var string */
$uri = $this->OPTION[0] ?? Config::get('socket.uri', '127.0.0.1:8080');
// header information
$style->push('socket server')->textGreen()->newLines();
$style->tap(info($uri));
$style->push('listening...')->textDim()->out();
// socket
$socket = new \React\Socket\SocketServer($uri);
$socket->on('connection', function (\React\Socket\ConnectionInterface $connection) {
$connection->on('data', function ($chunk) {
style((new \DateTime())->format('Y-m-d H:i:s'))->textDim()->underline()
->push($chunk)
->out();
});
});
}
/**
* @return Style
*/
public function help()
{
$this->command_describes = [
'serve' => 'Start socket server, default - ' . Config::castString('socket.uri', '127.0.0.1:8080'),
'config' => 'Set config to config file',
'help' => 'Show help command information',
];
$this->option_describes = [
'--init' => 'Config, create new Here configuration in root application',
'--line' => 'Config, set default printer up/down line count',
'--var-end' => 'Config, set default printer up/down line count',
'--var-max' => 'Config, set default printer maximum line to be print',
'--socket' => 'Config, enable/disable socket reporting',
'--uri' => 'Config, set default socket uri',
];
$this->command_relation = [
'serve' => ['[uri:port]'],
'config' => ['[option]'],
];
$this->print_help = [
'margin-left' => 4,
'column-1-min-lenght' => 8,
];
$print = new Style('Here CLI');
$print
->textGreen()
->push(' command line application')->textBlue()
->newLines(2);
$print->push('command:')->newLines();
$print = $this->printCommands($print)->newLines();
$print->push('option:')->newLines();
$print = $this->printOptions($print);
return $print;
}
/**
* Config.
*
* @return void
*/
public function config()
{
// init config
if ($this->option('init')) {
$config_file = dirname(__DIR__, 5) . '/here.config.json';
$config = json_encode(Config::all(), JSON_PRETTY_PRINT);
if (file_put_contents($config_file, $config) !== false) {
style('Success create config file ' . $config_file)
->textYellow()
->out();
}
}
// set socket.uri
$socket = $this->option('socket');
if ($socket !== null) {
if (is_string($socket)) {
$socket = strtolower($socket) === 'true' ? true : false;
}
Config::set('socket.enable', $socket);
}
// set socket.enable
$uri = $this->option('uri', '127.0.0.1:8080');
if ($uri !== true) {
Config::set('socket.uri', $uri);
}
// set print.line
$line = $this->option('line', 2);
if ($line !== true) {
Config::set('print.line', (int) $line);
}
// set print.line
$var_end = $this->option('varend', false);
Config::set('print.var.end', $var_end);
// set print.var.max
$max_line = $this->option('varmax', false);
if ($line !== true) {
Config::set('print.var.max', (int) $max_line);
}
}
private function promt(): bool
{
$input = fgets(STDIN);
if ($input === false) {
throw new \Exception('Cant read input');
}
$asal = trim($input);
if ($asal === 'no' || $asal === 'n') {
return false;
}
Config::set('socket.enable', true);
return true;
}
}