88
99namespace inhere \console ;
1010
11+ use inhere \console \helpers \Annotation ;
1112use inhere \console \io \Input ;
1213use inhere \console \io \Output ;
13- use inhere \console \utils \TraitInputOutput ;
14- use inhere \console \utils \TraitInteract ;
14+ use inhere \console \traits \TraitInputOutput ;
15+ use inhere \console \traits \TraitInteract ;
1516
1617/**
1718 * Class AbstractCommand
@@ -26,6 +27,9 @@ abstract class AbstractCommand
2627 // please use the const setting current controller/command description
2728 const DESCRIPTION = '' ;
2829
30+ // name -> {$name}
31+ const ANNOTATION_VAR = '{$%s} ' ;
32+
2933 /**
3034 * TODO ...
3135 * command description message
@@ -41,10 +45,17 @@ abstract class AbstractCommand
4145 public static $ name = '' ;
4246
4347 /**
44- * allow display message tags in the command
48+ * Allow display message tags in the command annotation
4549 * @var array
4650 */
47- protected static $ allowTags = ['description ' , 'usage ' , 'example ' ];
51+ protected static $ allowTags = [
52+ // tag name => multi line align
53+ 'description ' => false ,
54+ 'usage ' => false ,
55+ 'arguments ' => true ,
56+ 'options ' => true ,
57+ 'example ' => true ,
58+ ];
4859
4960 /**
5061 * Command constructor.
@@ -57,16 +68,78 @@ public function __construct(Input $input, Output $output)
5768 $ this ->output = $ output ;
5869 }
5970
71+ /**
72+ * @param string $arg
73+ */
6074 abstract public function run ($ arg = '' );
6175
76+ /**
77+ * @param string $action
78+ */
6279 protected function beforeRun ($ action )
6380 {
6481 }
6582
83+ /**
84+ * @param string $action
85+ */
6686 protected function afterRun ($ action )
6787 {
6888 }
6989
90+ /**
91+ * 为命令注解提供可解析解析变量. 可以在命令的注释中使用
92+ * @return array
93+ */
94+ protected function annotationVars ()
95+ {
96+ // e.g: `more info see {$name}/index`
97+ return [
98+ 'command ' => $ this ->input ->getCommand (),
99+ 'name ' => static ::$ name ,
100+ ];
101+ }
102+
103+ /**
104+ * show help by parse method annotation
105+ * @param string $method
106+ * @param null|string $action
107+ * @return int
108+ */
109+ protected function showHelpByMethodAnnotation ($ method , $ action = null )
110+ {
111+ $ ref = new \ReflectionClass ($ this );
112+ $ cName = lcfirst (self ::getName () ?: $ ref ->getShortName ());
113+
114+ if (!$ ref ->hasMethod ($ method ) || !$ ref ->getMethod ($ method )->isPublic ()) {
115+ $ name = $ action ? "$ cName/ $ action " : $ cName ;
116+ $ this ->write ("Command [<info> $ name</info>] don't exist or don't allow access in the class. " );
117+
118+ return 0 ;
119+ }
120+
121+ $ m = $ ref ->getMethod ($ method );
122+ $ tags = Annotation::tagList ($ m ->getDocComment ());
123+
124+ foreach ($ tags as $ tag => $ msg ) {
125+ if (!is_string ($ msg )) {
126+ continue ;
127+ }
128+
129+ if (isset (self ::$ allowTags [$ tag ])) {
130+ // need multi align
131+ if (self ::$ allowTags [$ tag ]) {
132+ $ msg = implode ("\n " , array_filter (explode ("\n" , $ msg ), 'trim ' ));
133+ }
134+
135+ $ tag = ucfirst ($ tag );
136+ $ this ->write ("<comment> $ tag:</comment> \n $ msg \n" );
137+ }
138+ }
139+
140+ return 0 ;
141+ }
142+
70143 /**
71144 * handle action/command runtime exception
72145 *
0 commit comments