@@ -24,33 +24,35 @@ class Application extends AbstractApplication
2424 /**
2525 * Register a app group command(by controller)
2626 * @param string $name The controller name
27- * @param string $class The controller class
27+ * @param string|Controller $class The controller class
2828 * @param null|array|string $option
2929 * @return static
3030 * @throws \InvalidArgumentException
3131 */
32- public function controller (string $ name , string $ class = null , $ option = null )
32+ public function controller (string $ name , $ class = null , $ option = null )
3333 {
34- if (! $ class && class_exists ( $ name )) {
35- /** @var Controller $class */
34+ /** @var Controller $class */
35+ if (! $ class && \class_exists ( $ name )) {
3636 $ class = $ name ;
3737 $ name = $ class ::getName ();
3838 }
3939
4040 if (!$ name || !$ class ) {
41- throw new \InvalidArgumentException (
42- 'Group-command "name" and "controller" not allowed to is empty! name: ' . $ name . ', controller: ' . $ class
41+ Helper::throwInvalidArgument (
42+ 'Group-command "name" and "controller" not allowed to is empty! name: %s, controller: %s ' ,
43+ $ name ,
44+ $ class
4345 );
4446 }
4547
4648 $ this ->validateName ($ name , true );
4749
48- if (! class_exists ($ class )) {
49- throw new \ InvalidArgumentException ("The console controller class [ $ class] not exists! " );
50+ if (\is_string ( $ class ) && ! \ class_exists ($ class )) {
51+ Helper:: throwInvalidArgument ("The console controller class [ $ class] not exists! " );
5052 }
5153
52- if (!is_subclass_of ($ class , Controller::class)) {
53- throw new \ InvalidArgumentException ('The console controller class must is subclass of the: ' . Controller::class);
54+ if (!\ is_subclass_of ($ class , Controller::class)) {
55+ Helper:: throwInvalidArgument ('The console controller class must is subclass of the: ' . Controller::class);
5456 }
5557
5658 // not enable
@@ -60,21 +62,19 @@ public function controller(string $name, string $class = null, $option = null)
6062
6163 // allow define aliases in Command class by Controller::aliases()
6264 if ($ aliases = $ class ::aliases ()) {
63- $ option ['aliases ' ] = isset ($ option ['aliases ' ]) ? array_merge ($ option ['aliases ' ], $ aliases ) : $ aliases ;
65+ $ option ['aliases ' ] = isset ($ option ['aliases ' ]) ? \ array_merge ($ option ['aliases ' ], $ aliases ) : $ aliases ;
6466 }
6567
6668 $ this ->controllers [$ name ] = $ class ;
6769
68- if (!$ option ) {
69- return $ this ;
70- }
71-
72- // have option information
73- if (\is_string ($ option )) {
74- $ this ->addCommandMessage ($ name , $ option );
75- } elseif (\is_array ($ option )) {
76- $ this ->addCommandAliases ($ name , $ option ['aliases ' ] ?? null );
77- $ this ->addCommandMessage ($ name , $ option ['description ' ] ?? null );
70+ // has option information
71+ if ($ option ) {
72+ if (\is_string ($ option )) {
73+ $ this ->addCommandMessage ($ name , $ option );
74+ } elseif (\is_array ($ option )) {
75+ $ this ->addCommandAliases ($ name , $ option ['aliases ' ] ?? null );
76+ $ this ->addCommandMessage ($ name , $ option ['description ' ] ?? null );
77+ }
7878 }
7979
8080 return $ this ;
@@ -85,7 +85,7 @@ public function controller(string $name, string $class = null, $option = null)
8585 * @see Application::controller()
8686 * @throws \InvalidArgumentException
8787 */
88- public function addController (string $ name , string $ class = null , $ option = null )
88+ public function addController (string $ name , $ class = null , $ option = null )
8989 {
9090 return $ this ->controller ($ name , $ class , $ option );
9191 }
@@ -109,7 +109,7 @@ public function controllers(array $controllers)
109109 */
110110 public function command (string $ name , $ handler = null , $ option = null )
111111 {
112- if (!$ handler && class_exists ($ name )) {
112+ if (!$ handler && \ class_exists ($ name )) {
113113 /** @var Command $name */
114114 $ handler = $ name ;
115115 $ name = $ name ::getName ();
@@ -126,7 +126,7 @@ public function command(string $name, $handler = null, $option = null)
126126 }
127127
128128 if (\is_string ($ handler )) {
129- if (!class_exists ($ handler )) {
129+ if (!\ class_exists ($ handler )) {
130130 throw new \InvalidArgumentException ("The console command class [ $ handler] not exists! " );
131131 }
132132
@@ -144,7 +144,7 @@ public function command(string $name, $handler = null, $option = null)
144144 if ($ aliases = $ handler ::aliases ()) {
145145 $ option ['aliases ' ] = isset ($ option ['aliases ' ]) ? array_merge ($ option ['aliases ' ], $ aliases ) : $ aliases ;
146146 }
147- } elseif (!\is_object ($ handler ) || !method_exists ($ handler , '__invoke ' )) {
147+ } elseif (!\is_object ($ handler ) || !\ method_exists ($ handler , '__invoke ' )) {
148148 throw new \InvalidArgumentException (sprintf (
149149 'The console command handler must is an subclass of %s OR a Closure OR a object have method __invoke() ' ,
150150 Command::class
@@ -199,7 +199,7 @@ public function addCommand(string $name, $handler = null, $option = null): self
199199 * @return static
200200 * @throws \InvalidArgumentException
201201 */
202- public function addGroup (string $ name , string $ controller = null , $ option = null )
202+ public function addGroup (string $ name , $ controller = null , $ option = null )
203203 {
204204 return $ this ->controller ($ name , $ controller , $ option );
205205 }
@@ -253,7 +253,7 @@ protected function getFileFilter(): callable
253253 $ name = $ f ->getFilename ();
254254
255255 // Skip hidden files and directories.
256- if ($ name[ 0 ] === ' . ' ) {
256+ if (\strpos ( $ name, ' . ' ) === 0 ) {
257257 return false ;
258258 }
259259
@@ -302,7 +302,7 @@ protected function dispatch(string $name)
302302 }
303303
304304 // command not found
305- if (true !== $ this ->fire (self ::ON_NOT_FOUND , [ $ this ] )) {
305+ if (true !== $ this ->fire (self ::ON_NOT_FOUND , $ this )) {
306306 $ this ->output ->liteError ("The command ' {$ name }' is not exists in the console application! " );
307307
308308 $ commands = \array_merge ($ this ->getControllerNames (), $ this ->getCommandNames ());
@@ -325,7 +325,7 @@ protected function dispatch(string $name)
325325 * @return mixed
326326 * @throws \InvalidArgumentException
327327 */
328- public function runCommand ($ name , $ believable = false )
328+ public function runCommand (string $ name , bool $ believable = false )
329329 {
330330 // if $believable = true, will skip check.
331331 if (!$ believable && $ this ->isCommand ($ name )) {
@@ -372,25 +372,29 @@ public function runCommand($name, $believable = false)
372372 * @throws \InvalidArgumentException
373373 * @throws \ReflectionException
374374 */
375- public function runAction ($ name , $ action , $ believable = false , $ standAlone = false )
375+ public function runAction (string $ name , string $ action , bool $ believable = false , bool $ standAlone = false )
376376 {
377377 // if $believable = true, will skip check.
378378 if (!$ believable && !$ this ->isController ($ name )) {
379- throw new \ InvalidArgumentException ( " The console controller-command [ $ name ] not exists! " );
379+ Helper:: throwInvalidArgument ( ' The console controller-command [%s ] not exists! ' , $ name );
380380 }
381381
382382 // Controller class
383- $ controller = $ this ->controllers [$ name ];
383+ $ object = $ this ->controllers [$ name ];
384384
385- if (!class_exists ($ controller )) {
386- throw new \InvalidArgumentException ("The console controller class [ $ controller] not exists! " );
387- }
385+ if (\is_string ($ object )) {
386+ $ class = $ object ;
388387
389- /** @var Controller $object */
390- $ object = new $ controller ($ this ->input , $ this ->output );
388+ if (!\class_exists ($ class )) {
389+ Helper::throwInvalidArgument ('The console controller class [%s] not exists! ' , $ class );
390+ }
391+
392+ /** @var Controller $object */
393+ $ object = new $ class ($ this ->input , $ this ->output );
394+ }
391395
392396 if (!($ object instanceof Controller)) {
393- throw new \ InvalidArgumentException ( " The console controller class [ $ object ] must instanceof the " . Controller::class);
397+ Helper:: throwInvalidArgument ( ' The console controller class [%s ] must instanceof the %s ' , $ object , Controller::class);
394398 }
395399
396400 $ object ::setName ($ name );
0 commit comments