1
|
|
|
<?php |
2
|
|
|
|
|
|
|
|
3
|
|
|
namespace Hhxsv5\LaravelS\Illuminate; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
|
|
|
|
6
|
|
|
use Illuminate\Support\Arr; |
|
|
|
|
7
|
|
|
|
8
|
|
|
class LaravelSCommand extends Command |
|
|
|
|
9
|
|
|
{ |
10
|
|
|
protected $signature = 'laravels {action? : publish|config|info} |
11
|
|
|
{--d|daemonize : Run as a daemon} |
12
|
|
|
{--i|ignore : Ignore checking PID file of Master process} |
13
|
|
|
{--x=|x-version= : The version(branch) of the current project, stored in $_ENV/$_SERVER}'; |
14
|
|
|
|
15
|
|
|
protected $description = 'LaravelS console tool'; |
16
|
|
|
|
17
|
|
|
public function fire() |
|
|
|
|
18
|
|
|
{ |
19
|
|
|
$this->handle(); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function handle() |
|
|
|
|
23
|
|
|
{ |
24
|
|
|
$action = (string)$this->argument('action'); |
25
|
|
|
switch ($action) { |
26
|
|
|
case 'publish': |
|
|
|
|
27
|
|
|
$this->publish(); |
28
|
|
|
break; |
29
|
|
|
case 'config': |
|
|
|
|
30
|
|
|
$this->prepareConfig(); |
|
|
|
|
31
|
|
|
case 'info': |
|
|
|
|
32
|
|
|
$this->showInfo(); |
33
|
|
|
break; |
34
|
|
|
default: |
|
|
|
|
35
|
|
|
$this->info(sprintf('Usage: [%s] ./artisan laravels publish|config|info', PHP_BINARY)); |
36
|
|
|
if (in_array($action, ['start', 'stop', 'restart', 'reload'], true)) { |
|
|
|
|
37
|
|
|
$this->error(sprintf( |
|
|
|
|
38
|
|
|
'The "%s" command has been migrated to "bin/laravels", %ssee https://github.com/hhxsv5/laravel-s#run', |
39
|
|
|
$action, |
40
|
|
|
file_exists(base_path('bin/laravels')) ? '' : 'please run `php artisan laravels publish` first, ' |
|
|
|
|
41
|
|
|
)); |
|
|
|
|
42
|
|
|
} |
|
|
|
|
43
|
|
|
break; |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
protected function isLumen() |
|
|
|
|
48
|
|
|
{ |
49
|
|
|
return stripos($this->getApplication()->getVersion(), 'Lumen') !== false; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
protected function loadConfig() |
|
|
|
|
53
|
|
|
{ |
54
|
|
|
// Load configuration laravel.php manually for Lumen |
55
|
|
|
$basePath = config('laravels.laravel_base_path') ?: base_path(); |
|
|
|
|
56
|
|
|
if ($this->isLumen() && file_exists($basePath . '/config/laravels.php')) { |
57
|
|
|
$this->getLaravel()->configure('laravels'); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
protected function showInfo() |
|
|
|
|
62
|
|
|
{ |
63
|
|
|
$this->showLogo(); |
64
|
|
|
$this->showComponents(); |
65
|
|
|
$this->showProtocols(); |
66
|
|
|
$this->comment('>>> Feedback: <options=underscore>https://github.com/hhxsv5/laravel-s</>'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
protected function showLogo() |
|
|
|
|
70
|
|
|
{ |
71
|
|
|
static $logo = <<<EOS |
72
|
|
|
_ _ _____ |
73
|
|
|
| | | |/ ____| |
74
|
|
|
| | __ _ _ __ __ ___ _____| | (___ |
75
|
|
|
| | / _` | '__/ _` \ \ / / _ \ |\___ \ |
76
|
|
|
| |___| (_| | | | (_| |\ V / __/ |____) | |
77
|
|
|
|______\__,_|_| \__,_| \_/ \___|_|_____/ |
78
|
|
|
|
79
|
|
|
EOS; |
80
|
|
|
$this->info($logo); |
81
|
|
|
$this->info('Speed up your Laravel/Lumen'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
protected function showComponents() |
|
|
|
|
85
|
|
|
{ |
86
|
|
|
$this->comment('>>> Components'); |
87
|
|
|
$laravelSVersion = '-'; |
88
|
|
|
$cfg = file_exists(base_path('composer.lock')) ? json_decode(file_get_contents(base_path('composer.lock')), true) : []; |
|
|
|
|
89
|
|
|
if (isset($cfg['packages'])) { |
90
|
|
|
$packages = array_merge($cfg['packages'], Arr::get($cfg, 'packages-dev', [])); |
91
|
|
|
foreach ($packages as $package) { |
92
|
|
|
if (isset($package['name']) && $package['name'] === 'hhxsv5/laravel-s') { |
93
|
|
|
$laravelSVersion = ltrim($package['version'], 'vV'); |
94
|
|
|
break; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
$this->table(['Component', 'Version'], [ |
|
|
|
|
99
|
|
|
[ |
100
|
|
|
'PHP', |
101
|
|
|
phpversion(), |
102
|
|
|
], |
103
|
|
|
[ |
104
|
|
|
'Swoole', |
105
|
|
|
swoole_version(), |
|
|
|
|
106
|
|
|
], |
107
|
|
|
[ |
108
|
|
|
'LaravelS', |
109
|
|
|
$laravelSVersion, |
110
|
|
|
], |
111
|
|
|
[ |
112
|
|
|
$this->getApplication()->getName() . ' [<info>' . env('APP_ENV', config('app.env')) . '</info>]', |
|
|
|
|
113
|
|
|
$this->getApplication()->getVersion(), |
114
|
|
|
], |
115
|
|
|
]); |
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
protected function showProtocols() |
|
|
|
|
119
|
|
|
{ |
120
|
|
|
$this->comment('>>> Protocols'); |
121
|
|
|
|
122
|
|
|
$config = unserialize(file_get_contents($this->getConfPath())); |
123
|
|
|
$socketType = isset($config['server']['socket_type']) ? $config['server']['socket_type'] : SWOOLE_SOCK_TCP; |
124
|
|
|
if (in_array($socketType, [SWOOLE_SOCK_UNIX_DGRAM, SWOOLE_SOCK_UNIX_STREAM])) { |
125
|
|
|
$listenAt = $config['server']['listen_ip']; |
126
|
|
|
} else { |
127
|
|
|
$listenAt = sprintf('%s:%s', $config['server']['listen_ip'], $config['server']['listen_port']); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
$tableRows = [ |
131
|
|
|
[ |
132
|
|
|
'Main HTTP', |
133
|
|
|
'<info>On</info>', |
134
|
|
|
$this->getApplication()->getName(), |
135
|
|
|
$listenAt, |
136
|
|
|
], |
137
|
|
|
]; |
138
|
|
|
if (!empty($config['server']['websocket']['enable'])) { |
139
|
|
|
$tableRows [] = [ |
140
|
|
|
'Main WebSocket', |
141
|
|
|
'<info>On</info>', |
142
|
|
|
$config['server']['websocket']['handler'], |
143
|
|
|
$listenAt, |
144
|
|
|
]; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
$socketTypeNames = [ |
148
|
|
|
SWOOLE_SOCK_TCP => 'TCP IPV4 Socket', |
149
|
|
|
SWOOLE_SOCK_TCP6 => 'TCP IPV6 Socket', |
150
|
|
|
SWOOLE_SOCK_UDP => 'UDP IPV4 Socket', |
151
|
|
|
SWOOLE_SOCK_UDP6 => 'TCP IPV6 Socket', |
152
|
|
|
SWOOLE_SOCK_UNIX_DGRAM => 'Unix Socket Dgram', |
153
|
|
|
SWOOLE_SOCK_UNIX_STREAM => 'Unix Socket Stream', |
154
|
|
|
]; |
155
|
|
|
$sockets = isset($config['server']['sockets']) ? $config['server']['sockets'] : []; |
156
|
|
|
foreach ($sockets as $key => $socket) { |
157
|
|
|
if (isset($socket['enable']) && !$socket['enable']) { |
158
|
|
|
continue; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
$name = 'Port#' . $key . ' '; |
162
|
|
|
$name .= isset($socketTypeNames[$socket['type']]) ? $socketTypeNames[$socket['type']] : 'Unknown socket'; |
163
|
|
|
$tableRows [] = [ |
164
|
|
|
$name, |
165
|
|
|
'<info>On</info>', |
166
|
|
|
$socket['handler'], |
167
|
|
|
sprintf('%s:%s', $socket['host'], $socket['port']), |
168
|
|
|
]; |
169
|
|
|
} |
170
|
|
|
$this->table(['Protocol', 'Status', 'Handler', 'Listen At'], $tableRows); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
protected function prepareConfig() |
|
|
|
|
174
|
|
|
{ |
175
|
|
|
$this->loadConfig(); |
176
|
|
|
|
177
|
|
|
$svrConf = config('laravels'); |
|
|
|
|
178
|
|
|
|
179
|
|
|
$this->preSet($svrConf); |
180
|
|
|
|
181
|
|
|
$ret = $this->preCheck($svrConf); |
182
|
|
|
if ($ret !== 0) { |
183
|
|
|
return $ret; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
$laravelConf = [ |
187
|
|
|
'root_path' => $svrConf['laravel_base_path'], |
188
|
|
|
'static_path' => $svrConf['swoole']['document_root'], |
189
|
|
|
'cleaners' => array_unique((array)Arr::get($svrConf, 'cleaners', [])), |
190
|
|
|
'register_providers' => array_unique((array)Arr::get($svrConf, 'register_providers', [])), |
191
|
|
|
'destroy_controllers' => Arr::get($svrConf, 'destroy_controllers', []), |
192
|
|
|
'is_lumen' => $this->isLumen(), |
193
|
|
|
'_SERVER' => $_SERVER, |
194
|
|
|
'_ENV' => $_ENV, |
195
|
|
|
]; |
196
|
|
|
|
197
|
|
|
$config = ['server' => $svrConf, 'laravel' => $laravelConf]; |
198
|
|
|
return file_put_contents($this->getConfPath(), serialize($config)) > 0 ? 0 : 1; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
protected function getConfPath() |
|
|
|
|
202
|
|
|
{ |
203
|
|
|
return base_path('storage/laravels.conf'); |
|
|
|
|
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
protected function preSet(array &$svrConf) |
|
|
|
|
207
|
|
|
{ |
208
|
|
|
if (!isset($svrConf['enable_gzip'])) { |
209
|
|
|
$svrConf['enable_gzip'] = false; |
210
|
|
|
} |
211
|
|
|
if (empty($svrConf['laravel_base_path'])) { |
212
|
|
|
$svrConf['laravel_base_path'] = base_path(); |
|
|
|
|
213
|
|
|
} |
214
|
|
|
if (empty($svrConf['process_prefix'])) { |
215
|
|
|
$svrConf['process_prefix'] = $svrConf['laravel_base_path']; |
216
|
|
|
} |
217
|
|
|
if ($this->option('ignore')) { |
218
|
|
|
$svrConf['ignore_check_pid'] = true; |
219
|
|
|
} elseif (!isset($svrConf['ignore_check_pid'])) { |
220
|
|
|
$svrConf['ignore_check_pid'] = false; |
221
|
|
|
} |
222
|
|
|
if (empty($svrConf['swoole']['document_root'])) { |
223
|
|
|
$svrConf['swoole']['document_root'] = $svrConf['laravel_base_path'] . '/public'; |
224
|
|
|
} |
225
|
|
|
if ($this->option('daemonize')) { |
226
|
|
|
$svrConf['swoole']['daemonize'] = true; |
227
|
|
|
} elseif (!isset($svrConf['swoole']['daemonize'])) { |
228
|
|
|
$svrConf['swoole']['daemonize'] = false; |
229
|
|
|
} |
230
|
|
|
if (empty($svrConf['swoole']['pid_file'])) { |
231
|
|
|
$svrConf['swoole']['pid_file'] = storage_path('laravels.pid'); |
|
|
|
|
232
|
|
|
} |
233
|
|
|
if (empty($svrConf['timer']['max_wait_time'])) { |
234
|
|
|
$svrConf['timer']['max_wait_time'] = 5; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
// Set X-Version |
238
|
|
|
$xVersion = (string)$this->option('x-version'); |
239
|
|
|
if ($xVersion !== '') { |
240
|
|
|
$_SERVER['X_VERSION'] = $_ENV['X_VERSION'] = $xVersion; |
241
|
|
|
} |
242
|
|
|
return 0; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
protected function preCheck(array $svrConf) |
|
|
|
|
246
|
|
|
{ |
247
|
|
|
if (!empty($svrConf['enable_gzip']) && version_compare(swoole_version(), '4.1.0', '>=')) { |
|
|
|
|
248
|
|
|
$this->error('enable_gzip is DEPRECATED since Swoole 4.1.0, set http_compression of Swoole instead, http_compression is disabled by default.'); |
249
|
|
|
$this->info('If there is a proxy server like Nginx, suggest that enable gzip in Nginx and disable gzip in Swoole, to avoid the repeated gzip compression for response.'); |
250
|
|
|
return 1; |
251
|
|
|
} |
252
|
|
|
if (!empty($svrConf['events'])) { |
253
|
|
|
if (empty($svrConf['swoole']['task_worker_num']) || $svrConf['swoole']['task_worker_num'] <= 0) { |
254
|
|
|
$this->error('Asynchronous event listening needs to set task_worker_num > 0'); |
255
|
|
|
return 1; |
256
|
|
|
} |
257
|
|
|
} |
258
|
|
|
return 0; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
|
262
|
|
|
public function publish() |
|
|
|
|
263
|
|
|
{ |
264
|
|
|
$basePath = config('laravels.laravel_base_path') ?: base_path(); |
|
|
|
|
265
|
|
|
$configPath = $basePath . '/config/laravels.php'; |
266
|
|
|
$todoList = [ |
267
|
|
|
[ |
268
|
|
|
'from' => realpath(__DIR__ . '/../../config/laravels.php'), |
269
|
|
|
'to' => $configPath, |
270
|
|
|
'mode' => 0644, |
271
|
|
|
], |
272
|
|
|
[ |
273
|
|
|
'from' => realpath(__DIR__ . '/../../bin/laravels'), |
274
|
|
|
'to' => $basePath . '/bin/laravels', |
275
|
|
|
'mode' => 0755, |
276
|
|
|
'link' => true, |
277
|
|
|
], |
278
|
|
|
[ |
279
|
|
|
'from' => realpath(__DIR__ . '/../../bin/fswatch'), |
280
|
|
|
'to' => $basePath . '/bin/fswatch', |
281
|
|
|
'mode' => 0755, |
282
|
|
|
'link' => true, |
283
|
|
|
], |
284
|
|
|
[ |
285
|
|
|
'from' => realpath(__DIR__ . '/../../bin/inotify'), |
286
|
|
|
'to' => $basePath . '/bin/inotify', |
287
|
|
|
'mode' => 0755, |
288
|
|
|
'link' => true, |
289
|
|
|
], |
290
|
|
|
]; |
291
|
|
|
if (file_exists($configPath)) { |
292
|
|
|
$choice = $this->anticipate($configPath . ' already exists, do you want to override it ? Y/N', |
|
|
|
|
293
|
|
|
['Y', 'N'], |
294
|
|
|
'N' |
295
|
|
|
); |
296
|
|
|
if (!$choice || strtoupper($choice) !== 'Y') { |
297
|
|
|
array_shift($todoList); |
298
|
|
|
} |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
foreach ($todoList as $todo) { |
302
|
|
|
$toDir = dirname($todo['to']); |
303
|
|
|
if (!is_dir($toDir) && !mkdir($toDir, 0755, true) && !is_dir($toDir)) { |
304
|
|
|
throw new \RuntimeException(sprintf('Directory "%s" was not created', $toDir)); |
305
|
|
|
} |
306
|
|
|
if (file_exists($todo['to'])) { |
307
|
|
|
unlink($todo['to']); |
308
|
|
|
} |
309
|
|
|
$operation = 'Copied'; |
310
|
|
|
if (empty($todo['link'])) { |
311
|
|
|
copy($todo['from'], $todo['to']); |
312
|
|
|
} else { |
313
|
|
|
if (@link($todo['from'], $todo['to'])) { |
314
|
|
|
$operation = 'Linked'; |
315
|
|
|
} else { |
316
|
|
|
copy($todo['from'], $todo['to']); |
317
|
|
|
} |
318
|
|
|
} |
319
|
|
|
chmod($todo['to'], $todo['mode']); |
320
|
|
|
$this->line("<info>{$operation} file</info> <comment>[{$todo['from']}]</comment> <info>To</info> <comment>[{$todo['to']}]</comment>"); |
321
|
|
|
} |
322
|
|
|
return 0; |
323
|
|
|
} |
324
|
|
|
} |
325
|
|
|
|