@@ -63,6 +63,9 @@ abstract class AbstractCommand implements BaseCommandInterface
6363 /** @var string */
6464 private $ processTitle ;
6565
66+ /** @var array */
67+ private $ annotationVars ;
68+
6669 /**
6770 * Command constructor.
6871 * @param Input $input
@@ -73,6 +76,7 @@ public function __construct(Input $input, Output $output, InputDefinition $defin
7376 {
7477 $ this ->input = $ input ;
7578 $ this ->output = $ output ;
79+ $ this ->annotationVars = $ this ->annotationVars ();
7680
7781 if ($ definition ) {
7882 $ this ->definition = $ definition ;
@@ -307,17 +311,54 @@ public function validateInput()
307311 **************************************************************************/
308312
309313 /**
310- * 为命令注解提供可解析解析变量. 可以在命令的注释中使用
314+ * @param string $name
315+ * @param string $value
316+ */
317+ protected function addAnnotationVar (string $ name , $ value )
318+ {
319+ if (!isset ($ this ->annotationVars [$ name ])) {
320+ $ this ->annotationVars [$ name ] = (string )$ value ;
321+ }
322+ }
323+
324+ /**
325+ * @param array $map
326+ */
327+ protected function addAnnotationVars (array $ map )
328+ {
329+ foreach ($ map as $ name => $ value ) {
330+ $ this ->addAnnotationVar ($ name , $ value );
331+ }
332+ }
333+
334+ /**
335+ * @param string $name
336+ * @param string $value
337+ */
338+ protected function setAnnotationVar (string $ name , $ value )
339+ {
340+ $ this ->annotationVars [$ name ] = (string )$ value ;
341+ }
342+
343+ /**
344+ * 替换注解中的变量为对应的值
311345 * @param string $str
312346 * @return string
313347 */
314- protected function handleAnnotationVars ($ str )
348+ protected function parseAnnotationVars ($ str )
315349 {
316- $ map = [] ;
350+ static $ map ;
317351
318- foreach ($ this ->annotationVars () as $ key => $ value ) {
319- $ key = sprintf (self ::ANNOTATION_VAR , $ key );
320- $ map [$ key ] = $ value ;
352+ if ($ map === null ) {
353+ foreach ($ this ->annotationVars as $ key => $ value ) {
354+ $ key = sprintf (self ::ANNOTATION_VAR , $ key );
355+ $ map [$ key ] = $ value ;
356+ }
357+ }
358+
359+ // not use vars
360+ if (false === strpos ($ str , '{ ' )) {
361+ return $ str ;
321362 }
322363
323364 return $ map ? strtr ($ str , $ map ) : $ str ;
@@ -349,7 +390,7 @@ protected function showHelpByMethodAnnotation($method, $action = null, array $al
349390 }
350391
351392 $ doc = $ ref ->getMethod ($ method )->getDocComment ();
352- $ tags = Annotation::getTags ($ this ->handleAnnotationVars ($ doc ));
393+ $ tags = Annotation::getTags ($ this ->parseAnnotationVars ($ doc ));
353394 $ comments = [];
354395
355396 if ($ aliases ) {
@@ -362,7 +403,7 @@ protected function showHelpByMethodAnnotation($method, $action = null, array $al
362403 }
363404
364405 if (isset (self ::$ annotationTags [$ tag ])) {
365- $ msg = trim ($ msg );
406+ $ msg = $ this -> parseAnnotationVars ( trim ($ msg) );
366407
367408 // need multi align
368409 // if (self::$annotationTags[$tag]) {
@@ -453,6 +494,14 @@ public function setDefinition(InputDefinition $definition)
453494 $ this ->definition = $ definition ;
454495 }
455496
497+ /**
498+ * @return array
499+ */
500+ public function getAnnotationVars (): array
501+ {
502+ return $ this ->annotationVars ;
503+ }
504+
456505 /**
457506 * @return ApplicationInterface
458507 */
0 commit comments