@@ -296,40 +296,46 @@ protected function runCommand(array $info, array $config, array $args): mixed
296296 $ handler = $ info ['handler ' ];
297297 $ iptName = $ info ['name ' ];
298298
299- if (is_object ($ handler ) && method_exists ($ handler , '__invoke ' )) {
300- $ fs = SFlags::new ();
301- $ fs ->setName ($ iptName );
302- $ fs ->addOptsByRules (GlobalOption::getAloneOptions ());
303-
304- // command flags load
305- if ($ cmdOpts = $ config ['options ' ] ?? null ) {
306- $ fs ->addOptsByRules ($ cmdOpts );
299+ if (is_object ($ handler )) {
300+ // Command object
301+ if ($ handler instanceof Command) {
302+ $ handler ->setInputOutput ($ this ->input , $ this ->output );
303+ $ result = $ handler ->run ($ args );
304+ } else { // Closure
305+ $ fs = SFlags::new ();
306+ $ fs ->setName ($ iptName );
307+ $ fs ->addOptsByRules (GlobalOption::getAloneOptions ());
308+
309+ // command flags load
310+ if ($ cmdOpts = $ config ['options ' ] ?? null ) {
311+ $ fs ->addOptsByRules ($ cmdOpts );
312+ }
313+ if ($ cmdArgs = $ config ['arguments ' ] ?? null ) {
314+ $ fs ->addArgsByRules ($ cmdArgs );
315+ }
316+
317+ $ fs ->setDesc ($ config ['desc ' ] ?? 'No command description message ' );
318+
319+ // save to input object
320+ $ this ->input ->setFs ($ fs );
321+
322+ if (!$ fs ->parse ($ args )) {
323+ return 0 ; // render help
324+ }
325+
326+ $ result = $ handler ($ fs , $ this ->output );
307327 }
308- if ($ cmdArgs = $ config ['arguments ' ] ?? null ) {
309- $ fs ->addArgsByRules ($ cmdArgs );
310- }
311-
312- $ fs ->setDesc ($ config ['desc ' ] ?? 'No command description message ' );
313-
314- // save to input object
315- $ this ->input ->setFs ($ fs );
316-
317- if (!$ fs ->parse ($ args )) {
318- return 0 ; // render help
319- }
320-
321- $ result = $ handler ($ fs , $ this ->output );
322328 } else {
323329 Assert::isTrue (class_exists ($ handler ), "The console command class [ $ handler] not exists! " );
324330
325- $ object = new $ handler ($ this ->input , $ this ->output );
326- Assert::isTrue ($ object instanceof Command, "Command class [ $ handler] must instanceof the " . Command::class);
331+ /** @var $cmd Command */
332+ $ cmd = new $ handler ($ this ->input , $ this ->output );
333+ Assert::isTrue ($ cmd instanceof Command, "Command class [ $ handler] must instanceof the " . Command::class);
327334
328- /** @var Command $object */
329- $ object ::setName ($ info ['cmdId ' ]); // real command name.
330- $ object ->setApp ($ this );
331- $ object ->setCommandName ($ iptName );
332- $ result = $ object ->run ($ args );
335+ $ cmd ::setName ($ info ['cmdId ' ]); // real command name.
336+ $ cmd ->setApp ($ this );
337+ $ cmd ->setCommandName ($ iptName );
338+ $ result = $ cmd ->run ($ args );
333339 }
334340
335341 return $ result ;
0 commit comments