1616use Inhere \Console \Traits \UserInteractAwareTrait ;
1717use Inhere \Console \Utils \Annotation ;
1818use Inhere \Console \Utils \FormatUtil ;
19+ use Inhere \Console \Utils \Helper ;
20+ use Swoole \Coroutine ;
1921
2022/**
2123 * Class AbstractCommand
@@ -38,6 +40,11 @@ abstract class AbstractCommand implements BaseCommandInterface
3840 */
3941 protected static $ description = '' ;
4042
43+ /**
44+ * @var bool Whether enable coroutine. It is require swoole extension.
45+ */
46+ protected static $ coroutine = false ;
47+
4148 /**
4249 * Allow display message tags in the command annotation
4350 * @var array
@@ -72,6 +79,16 @@ public static function isEnabled(): bool
7279 return true ;
7380 }
7481
82+ /**
83+ * Setting current command/group name aliases
84+ * @return string[]
85+ */
86+ public static function aliases (): array
87+ {
88+ // return ['alias1', 'alias2'];
89+ return [];
90+ }
91+
7592 /**
7693 * Command constructor.
7794 * @param Input $input
@@ -150,7 +167,6 @@ public function run(string $command = ''): int
150167
151168 if ($ this ->input ->sameOpt (['h ' , 'help ' ])) {
152169 $ this ->showHelp ();
153-
154170 return 0 ;
155171 }
156172
@@ -159,12 +175,29 @@ public function run(string $command = ''): int
159175 return -1 ;
160176 }
161177
162- if (true !== $ this ->beforeExecute ()) {
178+ // return False to deny go on
179+ if (false === $ this ->beforeExecute ()) {
163180 return -1 ;
164181 }
165182
166- $ status = (int )$ this ->execute ($ this ->input , $ this ->output );
167- $ this ->afterExecute ();
183+ $ ok = false ;
184+ $ status = 0 ;
185+
186+ // if enable coroutine
187+ if (self ::isCoroutine () && Helper::isSupportCoroutine ()) {
188+ $ ok = Coroutine::create (function () {
189+ $ status = (int )$ this ->execute ($ this ->input , $ this ->output );
190+
191+ $ this ->afterExecute ();
192+ $ this ->getApp ()->stop ($ status );
193+ });
194+ }
195+
196+ // when not enable coroutine OR coroutine create fail.
197+ if (!$ ok ){
198+ $ status = (int )$ this ->execute ($ this ->input , $ this ->output );
199+ $ this ->afterExecute ();
200+ }
168201
169202 return $ status ;
170203 }
@@ -501,6 +534,22 @@ public static function setDescription(string $description)
501534 static ::$ description = $ description ;
502535 }
503536
537+ /**
538+ * @return bool
539+ */
540+ public static function isCoroutine (): bool
541+ {
542+ return self ::$ coroutine ;
543+ }
544+
545+ /**
546+ * @param bool $coroutine
547+ */
548+ public static function setCoroutine ($ coroutine )
549+ {
550+ self ::$ coroutine = (bool )$ coroutine ;
551+ }
552+
504553 /**
505554 * @return array
506555 */
0 commit comments