|
11 | 11 | use Inhere\Console\Component\ErrorHandler; |
12 | 12 | use Inhere\Console\Component\Style\Style; |
13 | 13 | use Inhere\Console\Face\ApplicationInterface; |
14 | | -use Inhere\Console\Face\CommandInterface; |
15 | 14 | use Inhere\Console\Face\ErrorHandlerInterface; |
16 | 15 | use Inhere\Console\IO\Input; |
17 | 16 | use Inhere\Console\IO\InputInterface; |
18 | 17 | use Inhere\Console\IO\Output; |
19 | 18 | use Inhere\Console\IO\OutputInterface; |
| 19 | +use Inhere\Console\Traits\ApplicationHelpTrait; |
20 | 20 | use Inhere\Console\Traits\InputOutputAwareTrait; |
21 | 21 | use Inhere\Console\Traits\SimpleEventTrait; |
22 | | -use Inhere\Console\Util\FormatUtil; |
23 | | -use Inhere\Console\Util\Helper; |
24 | 22 | use Toolkit\PhpUtil\PhpHelper; |
25 | 23 |
|
26 | 24 | /** |
|
29 | 27 | */ |
30 | 28 | abstract class AbstractApplication implements ApplicationInterface |
31 | 29 | { |
32 | | - use InputOutputAwareTrait, SimpleEventTrait; |
| 30 | + use ApplicationHelpTrait, InputOutputAwareTrait, SimpleEventTrait; |
33 | 31 |
|
34 | 32 | /** @var array */ |
35 | 33 | protected static $internalCommands = [ |
@@ -359,157 +357,6 @@ protected function validateName(string $name, bool $isGroup = false) |
359 | 357 | } |
360 | 358 | } |
361 | 359 |
|
362 | | - /*************************************************************************** |
363 | | - * some information for the application |
364 | | - ***************************************************************************/ |
365 | | - |
366 | | - /** |
367 | | - * show the application help information |
368 | | - * @param bool $quit |
369 | | - * @param string $command |
370 | | - */ |
371 | | - public function showHelpInfo(bool $quit = true, string $command = '') |
372 | | - { |
373 | | - // display help for a special command |
374 | | - if ($command) { |
375 | | - $this->input->setCommand($command); |
376 | | - $this->input->setSOpt('h', true); |
377 | | - $this->input->clearArgs(); |
378 | | - $this->dispatch($command); |
379 | | - $this->stop(); |
380 | | - } |
381 | | - |
382 | | - $script = $this->input->getScript(); |
383 | | - $sep = $this->delimiter; |
384 | | - |
385 | | - $this->output->helpPanel([ |
386 | | - 'usage' => "$script <info>{command}</info> [--opt -v -h ...] [arg0 arg1 arg2=value2 ...]", |
387 | | - 'example' => [ |
388 | | - "$script test (run a independent command)", |
389 | | - "$script home{$sep}index (run a command of the group)", |
390 | | - "$script help {command} (see a command help information)", |
391 | | - "$script home{$sep}index -h (see a command help of the group)", |
392 | | - ] |
393 | | - ], $quit); |
394 | | - } |
395 | | - |
396 | | - /** |
397 | | - * show the application version information |
398 | | - * @param bool $quit |
399 | | - */ |
400 | | - public function showVersionInfo($quit = true) |
401 | | - { |
402 | | - $os = \PHP_OS; |
403 | | - $date = \date('Y.m.d'); |
404 | | - $logo = ''; |
405 | | - $name = $this->getConfig('name', 'Console Application'); |
406 | | - $version = $this->getConfig('version', 'Unknown'); |
407 | | - $publishAt = $this->getConfig('publishAt', 'Unknown'); |
408 | | - $updateAt = $this->getConfig('updateAt', 'Unknown'); |
409 | | - $phpVersion = \PHP_VERSION; |
410 | | - |
411 | | - if ($logoTxt = $this->getLogoText()) { |
412 | | - $logo = Helper::wrapTag($logoTxt, $this->getLogoStyle()); |
413 | | - } |
414 | | - |
415 | | - $this->output->aList([ |
416 | | - "$logo\n <info>{$name}</info>, Version <comment>$version</comment>\n", |
417 | | - 'System Info' => "PHP version <info>$phpVersion</info>, on <info>$os</info> system", |
418 | | - 'Application Info' => "Update at <info>$updateAt</info>, publish at <info>$publishAt</info>(current $date)", |
419 | | - ], null, [ |
420 | | - 'leftChar' => '', |
421 | | - 'sepChar' => ' : ' |
422 | | - ]); |
423 | | - |
424 | | - $quit && $this->stop(); |
425 | | - } |
426 | | - |
427 | | - /** |
428 | | - * show the application command list information |
429 | | - * @param bool $quit |
430 | | - */ |
431 | | - public function showCommandList($quit = true) |
432 | | - { |
433 | | - $script = $this->getScriptName(); |
434 | | - $hasGroup = $hasCommand = false; |
435 | | - $controllerArr = $commandArr = []; |
436 | | - $desPlaceholder = 'No description of the command'; |
437 | | - |
438 | | - // all console controllers |
439 | | - if ($controllers = $this->controllers) { |
440 | | - $hasGroup = true; |
441 | | - \ksort($controllers); |
442 | | - } |
443 | | - |
444 | | - // all independent commands, Independent, Single, Alone |
445 | | - if ($commands = $this->commands) { |
446 | | - $hasCommand = true; |
447 | | - \ksort($commands); |
448 | | - } |
449 | | - |
450 | | - // add split title on both exists. |
451 | | - if ($hasCommand && $hasGroup) { |
452 | | - $commandArr[] = \PHP_EOL . '- <bold>Alone Commands</bold>'; |
453 | | - $controllerArr[] = \PHP_EOL . '- <bold>Group Commands</bold>'; |
454 | | - } |
455 | | - |
456 | | - foreach ($controllers as $name => $controller) { |
457 | | - /** @var AbstractCommand $controller */ |
458 | | - $desc = $controller::getDescription() ?: $desPlaceholder; |
459 | | - $aliases = $this->getCommandAliases($name); |
460 | | - $extra = $aliases ? Helper::wrapTag(' [alias: ' . \implode(',', $aliases) . ']', 'info') : ''; |
461 | | - $controllerArr[$name] = $desc . $extra; |
462 | | - } |
463 | | - |
464 | | - if (!$hasGroup && $this->isDebug()) { |
465 | | - $controllerArr[] = '... Not register any group command(controller)'; |
466 | | - } |
467 | | - |
468 | | - foreach ($commands as $name => $command) { |
469 | | - $desc = $desPlaceholder; |
470 | | - |
471 | | - /** @var AbstractCommand $command */ |
472 | | - if (\is_subclass_of($command, CommandInterface::class)) { |
473 | | - $desc = $command::getDescription() ?: $desPlaceholder; |
474 | | - } elseif ($msg = $this->getCommandMetaValue($name, 'description')) { |
475 | | - $desc = $msg; |
476 | | - } elseif (\is_string($command)) { |
477 | | - $desc = 'A handler : ' . $command; |
478 | | - } elseif (\is_object($command)) { |
479 | | - $desc = 'A handler by ' . \get_class($command); |
480 | | - } |
481 | | - |
482 | | - $aliases = $this->getCommandAliases($name); |
483 | | - $extra = $aliases ? Helper::wrapTag(' [alias: ' . \implode(',', $aliases) . ']', 'info') : ''; |
484 | | - $commandArr[$name] = $desc . $extra; |
485 | | - } |
486 | | - |
487 | | - if (!$hasCommand && $this->isDebug()) { |
488 | | - $commandArr[] = '... Not register any alone command'; |
489 | | - } |
490 | | - |
491 | | - // built in commands |
492 | | - $internalCommands = static::$internalCommands; |
493 | | - \ksort($internalCommands); |
494 | | - |
495 | | - // built in options |
496 | | - $internalOptions = FormatUtil::alignOptions(self::$internalOptions); |
497 | | - |
498 | | - $this->output->mList([ |
499 | | - 'Usage:' => "$script <info>{command}</info> [--opt -v -h ...] [arg0 arg1 arg2=value2 ...]", |
500 | | - 'Options:' => $internalOptions, |
501 | | - 'Internal Commands:' => $internalCommands, |
502 | | - 'Available Commands:' => \array_merge($controllerArr, $commandArr), |
503 | | - ], [ |
504 | | - 'sepChar' => ' ', |
505 | | - ]); |
506 | | - |
507 | | - unset($controllerArr, $commandArr, $internalCommands); |
508 | | - $this->output->write("More command information, please use: <cyan>$script {command} -h</cyan>"); |
509 | | - |
510 | | - $quit && $this->stop(); |
511 | | - } |
512 | | - |
513 | 360 | /** |
514 | 361 | * @param string $name |
515 | 362 | * @param string|array $aliases |
|
0 commit comments