@@ -23,7 +23,7 @@ class Application extends AbstractApplication
2323
2424 /**
2525 * Register a app group command(by controller)
26- * @param string $name The controller name
26+ * @param string $name The controller name
2727 * @param string|Controller $class The controller class
2828 * @param null|array|string $option
2929 * @return static
@@ -34,7 +34,7 @@ public function controller(string $name, $class = null, $option = null)
3434 /** @var Controller $class */
3535 if (!$ class && \class_exists ($ name )) {
3636 $ class = $ name ;
37- $ name = $ class ::getName ();
37+ $ name = $ class ::getName ();
3838 }
3939
4040 if (!$ name || !$ class ) {
@@ -101,9 +101,9 @@ public function controllers(array $controllers)
101101
102102 /**
103103 * Register a app independent console command
104- * @param string|Command $name
104+ * @param string|Command $name
105105 * @param string|\Closure|Command $handler
106- * @param null|array|string $option
106+ * @param null|array|string $option
107107 * @return $this|mixed
108108 * @throws \InvalidArgumentException
109109 */
@@ -112,26 +112,26 @@ public function command(string $name, $handler = null, $option = null)
112112 if (!$ handler && \class_exists ($ name )) {
113113 /** @var Command $name */
114114 $ handler = $ name ;
115- $ name = $ name ::getName ();
115+ $ name = $ name ::getName ();
116116 }
117117
118118 if (!$ name || !$ handler ) {
119- throw new \ InvalidArgumentException ("Command 'name' and 'handler' not allowed to is empty! name: $ name " );
119+ Helper:: throwInvalidArgument ("Command 'name' and 'handler' not allowed to is empty! name: $ name " );
120120 }
121121
122122 $ this ->validateName ($ name );
123123
124124 if (isset ($ this ->commands [$ name ])) {
125- throw new \ InvalidArgumentException ("Command ' $ name' have been registered! " );
125+ Helper:: throwInvalidArgument ("Command ' $ name' have been registered! " );
126126 }
127127
128128 if (\is_string ($ handler )) {
129129 if (!\class_exists ($ handler )) {
130- throw new \ InvalidArgumentException ("The console command class [ $ handler] not exists! " );
130+ Helper:: throwInvalidArgument ("The console command class [ $ handler] not exists! " );
131131 }
132132
133133 if (!is_subclass_of ($ handler , Command::class)) {
134- throw new \ InvalidArgumentException ('The console command class must is subclass of the: ' . Command::class);
134+ Helper:: throwInvalidArgument ('The console command class must is subclass of the: ' . Command::class);
135135 }
136136
137137 // not enable
@@ -142,28 +142,26 @@ public function command(string $name, $handler = null, $option = null)
142142
143143 // allow define aliases in Command class by Command::aliases()
144144 if ($ aliases = $ handler ::aliases ()) {
145- $ option ['aliases ' ] = isset ($ option ['aliases ' ]) ? array_merge ($ option ['aliases ' ], $ aliases ) : $ aliases ;
145+ $ option ['aliases ' ] = isset ($ option ['aliases ' ]) ? \ array_merge ($ option ['aliases ' ], $ aliases ) : $ aliases ;
146146 }
147147 } elseif (!\is_object ($ handler ) || !\method_exists ($ handler , '__invoke ' )) {
148- throw new \ InvalidArgumentException ( sprintf (
148+ Helper:: throwInvalidArgument (
149149 'The console command handler must is an subclass of %s OR a Closure OR a object have method __invoke() ' ,
150150 Command::class
151- )) ;
151+ );
152152 }
153153
154154 // is an class name string
155155 $ this ->commands [$ name ] = $ handler ;
156156
157- if (!$ option ) {
158- return $ this ;
159- }
160-
161157 // have option information
162- if (\is_string ($ option )) {
163- $ this ->setCommandMetaValue ($ name , 'description ' , $ option );
164- } elseif (\is_array ($ option )) {
165- $ this ->addCommandAliases ($ name , $ option ['aliases ' ] ?? null );
166- $ this ->setCommandMeta ($ name , $ option );
158+ if ($ option ) {
159+ if (\is_string ($ option )) {
160+ $ this ->setCommandMetaValue ($ name , 'description ' , $ option );
161+ } elseif (\is_array ($ option )) {
162+ $ this ->addCommandAliases ($ name , $ option ['aliases ' ] ?? null );
163+ $ this ->setCommandMeta ($ name , $ option );
164+ }
167165 }
168166
169167 return $ this ;
@@ -179,25 +177,19 @@ public function commands(array $commands)
179177 }
180178
181179 /**
182- * addCommand
183- * @param string $name
184- * @param mixed $handler
185- * @param null|string|array $option
186- * @return $this
187- * @throws \InvalidArgumentException
180+ * add command
181+ * @inheritdoc
182+ * @see command()
188183 */
189184 public function addCommand (string $ name , $ handler = null , $ option = null ): self
190185 {
191186 return $ this ->command ($ name , $ handler , $ option );
192187 }
193188
194189 /**
195- * addGroup
196- * @param string $name
197- * @param string|null $controller
198- * @param null|string|array $option
199- * @return static
200- * @throws \InvalidArgumentException
190+ * add group/controller
191+ * @inheritdoc
192+ * @see controller()
201193 */
202194 public function addGroup (string $ name , $ controller = null , $ option = null )
203195 {
@@ -213,7 +205,7 @@ public function addGroup(string $name, $controller = null, $option = null)
213205 */
214206 public function registerCommands (string $ namespace , string $ basePath ): self
215207 {
216- $ length = \strlen ($ basePath ) + 1 ;
208+ $ length = \strlen ($ basePath ) + 1 ;
217209 $ iterator = Helper::directoryIterator ($ basePath , $ this ->getFileFilter ());
218210
219211 foreach ($ iterator as $ file ) {
@@ -233,7 +225,7 @@ public function registerCommands(string $namespace, string $basePath): self
233225 */
234226 public function registerGroups (string $ namespace , string $ basePath ): self
235227 {
236- $ length = \strlen ($ basePath ) + 1 ;
228+ $ length = \strlen ($ basePath ) + 1 ;
237229 $ iterator = Helper::directoryIterator ($ basePath , $ this ->getFileFilter ());
238230
239231 foreach ($ iterator as $ file ) {
@@ -287,7 +279,7 @@ protected function dispatch(string $name)
287279 return $ this ->runCommand ($ realName , true );
288280 }
289281
290- // maybe is a controller name
282+ // maybe is a controller/group name
291283 $ action = '' ;
292284
293285 // like 'home:index'
@@ -321,15 +313,15 @@ protected function dispatch(string $name)
321313 /**
322314 * run a command
323315 * @param string $name Command name
324- * @param bool $believable The `$name` is believable
316+ * @param bool $believable The `$name` is believable
325317 * @return mixed
326318 * @throws \InvalidArgumentException
327319 */
328320 public function runCommand (string $ name , bool $ believable = false )
329321 {
330322 // if $believable = true, will skip check.
331323 if (!$ believable && $ this ->isCommand ($ name )) {
332- throw new \ InvalidArgumentException ("The console independent-command [ $ name] not exists! " );
324+ Helper:: throwInvalidArgument ("The console independent-command [ $ name] not exists! " );
333325 }
334326
335327 /** @var \Closure|string $handler Command class */
@@ -345,14 +337,14 @@ public function runCommand(string $name, bool $believable = false)
345337 $ status = $ handler ($ this ->input , $ this ->output );
346338 } else {
347339 if (!\class_exists ($ handler )) {
348- throw new \ InvalidArgumentException ("The console command class [ $ handler] not exists! " );
340+ Helper:: throwInvalidArgument ("The console command class [ $ handler] not exists! " );
349341 }
350342
351343 /** @var Command $object */
352344 $ object = new $ handler ($ this ->input , $ this ->output );
353345
354346 if (!($ object instanceof Command)) {
355- throw new \ InvalidArgumentException ("The console command class [ $ handler] must instanceof the " . Command::class);
347+ Helper:: throwInvalidArgument ("The console command class [ $ handler] must instanceof the " . Command::class);
356348 }
357349
358350 $ object ::setName ($ name );
@@ -366,8 +358,8 @@ public function runCommand(string $name, bool $believable = false)
366358 /**
367359 * @param string $name group name
368360 * @param string $action Command method, no suffix
369- * @param bool $believable The `$name` is believable
370- * @param bool $standAlone
361+ * @param bool $believable The `$name` is believable
362+ * @param bool $standAlone
371363 * @return mixed
372364 * @throws \InvalidArgumentException
373365 * @throws \ReflectionException
@@ -394,7 +386,8 @@ public function runAction(string $name, string $action, bool $believable = false
394386 }
395387
396388 if (!($ object instanceof Controller)) {
397- Helper::throwInvalidArgument ('The console controller class [%s] must instanceof the %s ' , $ object , Controller::class);
389+ Helper::throwInvalidArgument ('The console controller class [%s] must instanceof the %s ' , $ object ,
390+ Controller::class);
398391 }
399392
400393 $ object ::setName ($ name );
0 commit comments