-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathApplicationHelpTrait.php
397 lines (340 loc) · 13.3 KB
/
ApplicationHelpTrait.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
<?php declare(strict_types=1);
/**
* The file is part of inhere/console
*
* @author https://github.com/inhere
* @homepage https://github.com/inhere/php-console
* @license https://github.com/inhere/php-console/blob/master/LICENSE
*/
namespace Inhere\Console\Decorate;
use Inhere\Console\Console;
use Inhere\Console\ConsoleEvent;
use Inhere\Console\Contract\CommandInterface;
use Inhere\Console\Handler\AbstractHandler;
use Inhere\Console\IO\Input;
use Inhere\Console\IO\Output;
use Inhere\Console\Util\Show;
use Toolkit\Cli\Color\ColorTag;
use Toolkit\Cli\Style;
use Toolkit\PFlag\FlagUtil;
use function array_merge;
use function basename;
use function date;
use function dirname;
use function file_exists;
use function file_get_contents;
use function file_put_contents;
use function get_class;
use function implode;
use function is_object;
use function is_string;
use function is_subclass_of;
use function ksort;
use function sprintf;
use function str_replace;
use function strtr;
use const PHP_EOL;
use const PHP_OS;
use const PHP_VERSION;
use const STR_PAD_LEFT;
/**
* Trait ApplicationHelpTrait
*
* @package Inhere\Console\Decorate
*/
trait ApplicationHelpTrait
{
/**
* @var string|array
*/
protected string|array $moreHelpInfo = '';
/***************************************************************************
* Show information for the application
***************************************************************************/
/**
* Display the application version information
*/
public function showVersionInfo(): void
{
$this->fire(ConsoleEvent::BEFORE_RENDER_APP_VERSION, $this);
Show::aList($this->buildVersionInfo(), '', [
'leftChar' => '',
'sepChar' => ' : ',
'keyPadPos' => STR_PAD_LEFT,
]);
}
/**
* @return string[]
*/
protected function buildVersionInfo(): array
{
$logo = '';
$date = date('Y.m.d');
$name = $this->getParam('name', 'Console Application');
$osName = PHP_OS;
$phpVer = PHP_VERSION;
$version = $this->getParam('version', 'Unknown');
$updateAt = $this->getParam('updateAt', 'Unknown');
$publishAt = $this->getParam('publishAt', 'Unknown');
if ($logoTxt = $this->getLogoText()) {
$logo = ColorTag::wrap($logoTxt, $this->getLogoStyle());
}
$info = [
"$logo\n <info>$name</info>, Version <comment>$version</comment>\n",
'System Info' => "PHP version <info>$phpVer</info>, on <info>$osName</info> system",
'Application Info' => "Update at <info>$updateAt</info>, publish at <info>$publishAt</info>(current $date)",
];
if ($hUrl = $this->getParam('homepage')) {
$info['Homepage URL'] = $hUrl;
}
return $info;
}
/**
* Display the application help information
*
* @param string $command
*/
public function showHelpInfo(string $command = ''): void
{
/** @var Input $in */
$in = $this->input;
// display help for a special command
if ($command) {
$this->debugf('display command help by use help: help COMMAND');
$this->dispatch($command, ['-h']);
return;
}
$this->debugf('display application help by input -h, --help');
$this->fire(ConsoleEvent::BEFORE_RENDER_APP_HELP, $this);
// built in options
// $globalOptions = self::$globalOptions;
$globalOptions = $this->flags->getOptsHelpLines();
// append generate options:
// php examples/app --auto-completion --shell-env zsh --gen-file
// php examples/app --auto-completion --shell-env zsh --gen-file stdout
if ($this->isDebug()) {
$globalOptions['--auto-completion'] = 'Open generate auto completion script';
$globalOptions['--shell-env'] = 'The shell env name for generate auto completion script';
$globalOptions['--gen-file'] = 'The output file for generate auto completion script';
}
$binName = $in->getScriptName();
$helpInfo = [
'Usage' => "$binName <info>{command}</info> [--opt -v -h ...] [arg0 arg1 arg2=value2 ...]",
'Options' => FlagUtil::alignOptions($globalOptions),
'Example' => [
'- run a command/subcommand:',
"$binName test run a independent command",
"$binName home index run a subcommand of the group",
'',
'- display help for command:',
"$binName help COMMAND see a command help information",
"$binName home index -h see a subcommand help of the group",
],
'Help' => [
'Generate shell auto completion scripts:',
" <info>$binName --auto-completion --shell-env [zsh|bash] [--gen-file stdout] [--tpl-file filepath]</info>",
' eg:',
" $binName --auto-completion --shell-env bash --gen-file stdout",
" $binName --auto-completion --shell-env zsh --gen-file stdout",
" $binName --auto-completion --shell-env bash --gen-file myapp.sh",
],
];
// custom more help info
if ($this->moreHelpInfo) {
$helpInfo['More Information'] = $this->moreHelpInfo;
}
/** @var Output $out */
$out = $this->output;
$out->helpPanel($helpInfo);
}
/**
* Display the application group/command list information
*/
public function showCommandList(): void
{
$flags = $this->flags;
// has option: --auto-completion
$autoComp = $flags->getOpt('auto-completion');
// has option: --shell-env
$shellEnv = $flags->getOpt('shell-env');
// input is an path: /bin/bash
if ($shellEnv && str_contains($shellEnv, '/')) {
$shellEnv = basename($shellEnv);
}
// php bin/app list --only-name
if ($autoComp && $shellEnv === 'bash') {
$this->dumpAutoCompletion('bash', []);
return;
}
$this->logf(Console::VERB_DEBUG, 'Display the application commands list');
$hasGroup = $hasCommand = false;
$groupArr = $commandArr = [];
// all console groups/controllers
$router = $this->getRouter();
if ($groups = $router->getControllers()) {
$hasGroup = true;
ksort($groups);
}
// all independent commands, Independent, Single, Alone
if ($commands = $router->getCommands()) {
$hasCommand = true;
ksort($commands);
}
// add split title on both exists.
// if (!$autoComp && $hasCommand && $hasGroup) {
// $groupArr[] = PHP_EOL . '- <bold>Group Commands</bold>';
// $commandArr[] = PHP_EOL . '- <bold>Alone Commands</bold>';
// }
$placeholder = 'No description of the command';
foreach ($groups as $name => $info) {
$controller = $info['handler'];
/** @var AbstractHandler $controller */
$desc = $controller::getDesc() ?: $placeholder;
$config = $info['config'];
$aliases = $config['aliases'];
$extra = $aliases ? ColorTag::wrap(' (alias: ' . implode(',', $aliases) . ')', 'info') : '';
// collect
$groupArr[$name] = $desc . $extra;
}
if (!$hasGroup && $this->isDebug()) {
$groupArr[] = '... Not register any group command(controller)';
}
foreach ($commands as $name => $info) {
$desc = $placeholder;
$config = $info['config'];
$command = $info['handler'];
/** @var AbstractHandler $command */
if (is_subclass_of($command, CommandInterface::class)) {
$desc = $command::getDesc() ?: $placeholder;
} elseif ($msg = $config['desc'] ?? '') {
$desc = $msg;
} elseif (is_string($command)) {
$desc = 'A handler : ' . $command;
} elseif (is_object($command)) {
$desc = 'A handler by ' . get_class($command);
}
$aliases = $config['aliases'];
$extra = $aliases ? ColorTag::wrap(' (alias: ' . implode(',', $aliases) . ')', 'info') : '';
$commandArr[$name] = $desc . $extra;
}
if (!$hasCommand && $this->isDebug()) {
$commandArr[] = '... Not register any alone command';
}
// built in commands
$internalCommands = static::$internalCommands;
if ($autoComp && $shellEnv === 'zsh') {
$map = array_merge($internalCommands, $groupArr, $commandArr);
$this->dumpAutoCompletion('zsh', $map);
return;
}
ksort($internalCommands);
Console::startBuffer();
if ($appDesc = $this->getParam('desc', '')) {
$appVer = $this->getParam('version', '');
Console::writeln(sprintf('%s%s' . PHP_EOL, $appDesc, $appVer ? " (Version: <info>$appVer</info>)" : ''));
}
$scriptName = $this->getScriptName();
// built in options
// $globOpts = self::$globalOptions;
$globOpts = $this->flags->getOptsHelpLines();
Show::mList([
'Usage:' => "$scriptName <info>{COMMAND}</info> [--opt -v -h ...] [arg0 arg1 arg2=value2 ...]",
'Options:' => FlagUtil::alignOptions($globOpts),
'Internal Commands:' => $internalCommands,
'Available Commands:' => array_merge($groupArr, $commandArr),
], [
'sepChar' => ' ',
]);
unset($groupArr, $commandArr, $internalCommands);
Console::write("More command information, please use: <cyan>$scriptName COMMAND -h</cyan>");
Console::flushBuffer();
}
/**
* zsh:
* php examples/app --auto-completion --shell-env zsh
* php examples/app --auto-completion --shell-env zsh --gen-file
* php examples/app --auto-completion --shell-env zsh --gen-file stdout
* bash:
* php examples/app --auto-completion --shell-env bash
* php examples/app --auto-completion --shell-env bash --gen-file
* php examples/app --auto-completion --shell-env bash --gen-file stdout
*
* @param string $shellEnv
* @param array $data
*/
protected function dumpAutoCompletion(string $shellEnv, array $data): void
{
/** @var Input $input */
$input = $this->input;
/** @var Output $output */
$output = $this->output;
$router = $this->getRouter();
// info
$glue = ' ';
$genFile = $this->flags->getOpt('gen-file', 'none');
$tplDir = dirname(__DIR__, 2) . '/resource/templates';
if ($shellEnv === 'bash') {
$tplFile = $tplDir . '/bash-completion.tpl';
$list = array_merge(
$router->getCommandNames(),
$router->getControllerNames(),
$this->getInternalCommands()
);
} else {
$tplFile = $tplDir . '/zsh-completion.tpl';
$glue = PHP_EOL;
$list = [];
foreach ($data as $name => $desc) {
$list[] = $name . ':' . str_replace(':', '\:', $desc);
}
}
// new: support custom tpl file for gen completion script
$userTplFile = $this->flags->getOpt('tpl-file');
if ($userTplFile && file_exists($userTplFile)) {
$tplFile = $userTplFile;
}
$commands = implode($glue, $list);
// only dump commands to stdout.
if ($genFile === 'none') {
$output->write($commands, true, false, ['color' => false]);
return;
}
if ($shellEnv === 'zsh') {
$commands = "'" . implode("'\n'", $list) . "'";
$commands = Style::stripColor($commands);
}
$toStdout = $genFile === 'stdout';
$filename = 'auto-completion.' . $shellEnv;
if (!$toStdout) {
if ($genFile === '1') {
$targetFile = $input->getPwd() . '/' . $filename;
} else {
$filename = basename($genFile);
// $targetDir = dirname($genFile);
$targetFile = $genFile;
}
}
// dump to script file
$binName = $input->getBinName();
$tplText = file_get_contents($tplFile);
$content = strtr($tplText, [
'{{version}}' => $this->getVersion(),
'{{filename}}' => $filename,
'{{commands}}' => $commands,
'{{binName}}' => $binName,
'{{datetime}}' => date('Y-m-d H:i:s'),
'{{fmtBinName}}' => str_replace('/', '_', $binName),
]);
// dump script contents to stdout
if ($toStdout) {
file_put_contents('php://stdout', $content);
return;
}
$output->write(['Target File:', $targetFile, '']);
if (file_put_contents($targetFile, $content) > 10) {
$output->success("O_O! Generate completion file '$filename' successful!");
} else {
$output->error("O^O! Generate completion file '$filename' failure!");
}
}
}