23
23
use Inhere \Console \Util \FormatUtil ;
24
24
use Inhere \Console \Util \Helper ;
25
25
use InvalidArgumentException ;
26
- use ReflectionClass ;
27
- use ReflectionMethod ;
26
+ use ReflectionException ;
28
27
use RuntimeException ;
29
28
use Swoole \Coroutine ;
30
29
use Swoole \Event ;
31
30
use Throwable ;
32
31
use Toolkit \PFlag \FlagsParser ;
33
32
use Toolkit \PFlag \SFlags ;
33
+ use Toolkit \Stdlib \Helper \PhpHelper ;
34
34
use Toolkit \Stdlib \Obj \ConfigObject ;
35
35
use Toolkit \Stdlib \Util \PhpDoc ;
36
- use function array_keys ;
37
36
use function array_merge ;
38
37
use function cli_set_process_title ;
39
38
use function error_get_last ;
40
39
use function function_exists ;
41
40
use function implode ;
42
- use function is_array ;
43
41
use function is_string ;
44
42
use function preg_replace ;
45
43
use function sprintf ;
@@ -151,7 +149,7 @@ public static function aliases(): array
151
149
/**
152
150
* Command constructor.
153
151
*
154
- * @param Input $input
152
+ * @param Input $input
155
153
* @param Output $output
156
154
*/
157
155
// TODO public function __construct(Input $input = null, Output $output = null, InputDefinition $definition = null)
@@ -411,7 +409,7 @@ protected function beforeExecute(): bool
411
409
/**
412
410
* Do execute command
413
411
*
414
- * @param Input $input
412
+ * @param Input $input
415
413
* @param Output $output
416
414
*
417
415
* @return int|mixed
@@ -498,12 +496,12 @@ public function initParams(array $params): ConfigObject
498
496
* @param string $method
499
497
* @param FlagsParser $fs
500
498
*
501
- * @throws \ ReflectionException
499
+ * @throws ReflectionException
502
500
*/
503
501
public function loadRulesByDocblock (string $ method , FlagsParser $ fs ): void
504
502
{
505
503
$ this ->debugf ('not config flags, load flag rules by docblock, method: %s ' , $ method );
506
- $ rftMth = new ReflectionMethod ($ this , $ method );
504
+ $ rftMth = PhpHelper:: reflectMethod ($ this , $ method );
507
505
508
506
// parse doc for get flag rules
509
507
$ dr = DocblockRules::newByDocblock ($ rftMth ->getDocComment ());
@@ -524,95 +522,33 @@ public function loadRulesByDocblock(string $method, FlagsParser $fs): void
524
522
*/
525
523
abstract protected function showHelp (): bool ;
526
524
527
- /**
528
- * @param InputDefinition $definition
529
- * @param array $aliases
530
- */
531
- protected function showHelpByDefinition (InputDefinition $ definition , array $ aliases = []): void
532
- {
533
- $ this ->log (Console::VERB_DEBUG , 'display help information by input definition ' );
534
-
535
- // if has InputDefinition object. (The comment of the command will not be parsed and used at this time.)
536
- $ help = $ definition ->getSynopsis ();
537
- // parse example
538
- $ example = $ help ['example: ' ];
539
- if (!empty ($ example )) {
540
- if (is_string ($ example )) {
541
- $ help ['example: ' ] = $ this ->parseCommentsVars ($ example );
542
- } elseif (is_array ($ example )) {
543
- foreach ($ example as &$ item ) {
544
- $ item = $ this ->parseCommentsVars ($ item );
545
- }
546
- unset($ item );
547
- } else {
548
- $ help ['example: ' ] = '' ;
549
- }
550
- }
551
-
552
- $ binName = $ this ->getScriptName ();
553
-
554
- // build usage
555
- if ($ this ->isAttached ()) {
556
- $ help ['usage: ' ] = sprintf ('%s %s %s ' , $ binName , $ this ->getCommandName (), $ help ['usage: ' ]);
557
- } else {
558
- $ help ['usage: ' ] = $ binName . ' ' . $ help ['usage: ' ];
559
- }
560
-
561
- // align global options
562
- $ help ['global options: ' ] = FormatUtil::alignOptions (GlobalOption::getOptions ());
563
-
564
- $ isAlone = $ this ->isAlone ();
565
- if ($ isAlone && empty ($ help [0 ])) {
566
- $ help [0 ] = self ::getDescription ();
567
- }
568
-
569
- if (empty ($ help [0 ])) {
570
- $ help [0 ] = 'No description message for the command ' ;
571
- }
572
-
573
- // output description
574
- $ this ->write (ucfirst ($ help [0 ]) . PHP_EOL );
575
-
576
- if ($ aliases ) {
577
- $ this ->output ->writef ('<comment>Alias:</comment> %s ' , implode (', ' , $ aliases ));
578
- }
579
-
580
- unset($ help [0 ]);
581
- $ this ->output ->mList ($ help , ['sepChar ' => ' ' ]);
582
- }
583
-
584
525
/**
585
526
* Display command/action help by parse method annotations
586
527
*
587
528
* @param string $method
588
529
* @param string $action action of an group
589
- * @param array $aliases
530
+ * @param array $aliases
590
531
*
591
532
* @return int
533
+ * @throws ReflectionException
592
534
*/
593
535
protected function showHelpByAnnotations (string $ method , string $ action = '' , array $ aliases = []): int
594
536
{
595
- $ ref = new ReflectionClass ($ this );
596
537
$ name = $ this ->input ->getCommand ();
597
538
598
- if (!$ ref ->hasMethod ($ method )) {
599
- $ subCmd = $ this ->input ->getSubCommand ();
600
- $ this ->write ("The command '<info> $ subCmd</info>' dont exist in the group: " . static ::getName ());
601
- return 0 ;
602
- }
603
-
604
539
// subcommand: is a console controller subcommand
605
- if ($ action && !$ ref ->getMethod ($ method )->isPublic ()) {
540
+ $ rftMth = PhpHelper::reflectMethod ($ this , $ method );
541
+ if ($ action && !$ rftMth ->isPublic ()) {
606
542
$ this ->write ("The command [<info> $ name</info>] don't allow access in the class. " );
607
543
return 0 ;
608
544
}
609
545
610
- $ allowedTags = array_keys ( self :: $ annotationTags );
546
+ $ allowedTags = DocblockRules:: getAllowedTags ( );
611
547
$ this ->logf (Console::VERB_DEBUG , "render help for the command: %s " , $ this ->input ->getCommandId ());
612
548
613
549
$ help = [];
614
- $ doc = $ ref -> getMethod ( $ method ) ->getDocComment ();
615
- $ tags = PhpDoc::getTags ($ this -> parseCommentsVars (( string ) $ doc) , [
550
+ $ doc = $ this -> parseCommentsVars (( string ) $ rftMth ->getDocComment () );
551
+ $ tags = PhpDoc::getTags ($ doc , [
616
552
'allow ' => $ allowedTags ,
617
553
]);
618
554
@@ -631,7 +567,7 @@ protected function showHelpByAnnotations(string $method, string $action = '', ar
631
567
}
632
568
633
569
// is an command object
634
- $ isCommand = $ ref -> isSubclassOf (CommandInterface::class );
570
+ $ isCommand = $ this -> isCommand ( );
635
571
foreach ($ allowedTags as $ tag ) {
636
572
if (empty ($ tags [$ tag ]) || !is_string ($ tags [$ tag ])) {
637
573
// for alone command
@@ -790,7 +726,7 @@ public static function addAnnotationTag(string $name): void
790
726
791
727
/**
792
728
* @param array $annotationTags
793
- * @param bool $replace
729
+ * @param bool $replace
794
730
*/
795
731
public static function setAnnotationTags (array $ annotationTags , $ replace = false ): void
796
732
{
0 commit comments