11<?php
2+
23/**
34 * Created by PhpStorm.
45 * User: inhere
@@ -20,38 +21,30 @@ class Application extends AbstractApplication
2021 /****************************************************************************
2122 * register console controller/command
2223 ****************************************************************************/
23-
2424 /**
2525 * Register a app group command(by controller)
2626 * @param string $name The controller name
2727 * @param string $class The controller class
2828 * @return static
2929 * @throws \InvalidArgumentException
3030 */
31- public function controller (string $ name , string $ class = null )
31+ public function controller ($ name , $ class = null )
3232 {
3333 if (!$ class && class_exists ($ name )) {
3434 /** @var Controller $class */
3535 $ class = $ name ;
3636 $ name = $ class ::getName ();
3737 }
38-
3938 if (!$ name || !$ class ) {
40- throw new \InvalidArgumentException (
41- 'Group-command "name" and "controller" not allowed to is empty! name: ' . $ name . ', controller: ' . $ class
42- );
39+ throw new \InvalidArgumentException ('Group-command "name" and "controller" not allowed to is empty! name: ' . $ name . ', controller: ' . $ class );
4340 }
44-
4541 $ this ->validateName ($ name , true );
46-
4742 if (!class_exists ($ class )) {
48- throw new \InvalidArgumentException ("The console controller class [ $ class] not exists! " );
43+ throw new \InvalidArgumentException ("The console controller class [ { $ class} ] not exists! " );
4944 }
50-
5145 if (!is_subclass_of ($ class , Controller::class)) {
5246 throw new \InvalidArgumentException ('The console controller class must is subclass of the: ' . Controller::class);
5347 }
54-
5548 $ this ->controllers [$ name ] = $ class ;
5649
5750 return $ this ;
@@ -62,7 +55,7 @@ public function controller(string $name, string $class = null)
6255 * @see Application::controller()
6356 * @throws \InvalidArgumentException
6457 */
65- public function addController (string $ name , string $ class = null )
58+ public function addController ($ name , $ class = null )
6659 {
6760 return $ this ->controller ($ name , $ class );
6861 }
@@ -83,42 +76,32 @@ public function controllers(array $controllers)
8376 * @return $this
8477 * @throws \InvalidArgumentException
8578 */
86- public function command (string $ name , $ handler = null , $ description = null )
79+ public function command ($ name , $ handler = null , $ description = null )
8780 {
8881 if (!$ handler && class_exists ($ name )) {
8982 /** @var Command $name */
9083 $ handler = $ name ;
9184 $ name = $ name ::getName ();
9285 }
93-
9486 if (!$ name || !$ handler ) {
95- throw new \InvalidArgumentException ("Command 'name' and 'handler' not allowed to is empty! name: $ name " );
87+ throw new \InvalidArgumentException ("Command 'name' and 'handler' not allowed to is empty! name: { $ name} " );
9688 }
97-
9889 $ this ->validateName ($ name );
99-
10090 if (isset ($ this ->commands [$ name ])) {
101- throw new \InvalidArgumentException ("Command ' $ name' have been registered! " );
91+ throw new \InvalidArgumentException ("Command ' { $ name} ' have been registered! " );
10292 }
103-
10493 if (\is_string ($ handler )) {
10594 if (!class_exists ($ handler )) {
106- throw new \InvalidArgumentException ("The console command class [ $ handler] not exists! " );
95+ throw new \InvalidArgumentException ("The console command class [ { $ handler} ] not exists! " );
10796 }
108-
10997 if (!is_subclass_of ($ handler , Command::class)) {
11098 throw new \InvalidArgumentException ('The console command class must is subclass of the: ' . Command::class);
11199 }
112100 } elseif (!\is_object ($ handler ) || !method_exists ($ handler , '__invoke ' )) {
113- throw new \InvalidArgumentException (sprintf (
114- 'The console command handler must is an subclass of %s OR a Closure OR a object have method __invoke() ' ,
115- Command::class
116- ));
101+ throw new \InvalidArgumentException (sprintf ('The console command handler must is an subclass of %s OR a Closure OR a object have method __invoke() ' , Command::class));
117102 }
118-
119103 // is an class name string
120104 $ this ->commands [$ name ] = $ handler ;
121-
122105 if ($ description ) {
123106 $ this ->addCommandMessage ($ name , $ description );
124107 }
@@ -141,7 +124,7 @@ public function commands(array $commands)
141124 * @return $this
142125 * @throws \InvalidArgumentException
143126 */
144- public function addCommand (string $ name , $ handler = null )
127+ public function addCommand ($ name , $ handler = null )
145128 {
146129 return $ this ->command ($ name , $ handler );
147130 }
@@ -153,7 +136,7 @@ public function addCommand(string $name, $handler = null)
153136 * @return static
154137 * @throws \InvalidArgumentException
155138 */
156- public function addGroup (string $ name , string $ controller = null )
139+ public function addGroup ($ name , $ controller = null )
157140 {
158141 return $ this ->controller ($ name , $ controller );
159142 }
@@ -165,11 +148,10 @@ public function addGroup(string $name, string $controller = null)
165148 * @return $this
166149 * @throws \InvalidArgumentException
167150 */
168- public function registerCommands (string $ namespace , string $ basePath )
151+ public function registerCommands ($ namespace , $ basePath )
169152 {
170153 $ length = \strlen ($ basePath ) + 1 ;
171154 $ iterator = Helper::recursiveDirectoryIterator ($ basePath , $ this ->getFileFilter ());
172-
173155 foreach ($ iterator as $ file ) {
174156 $ class = $ namespace . '\\' . \substr ($ file , $ length , -4 );
175157 $ this ->addCommand ($ class );
@@ -185,11 +167,10 @@ public function registerCommands(string $namespace, string $basePath)
185167 * @return $this
186168 * @throws \InvalidArgumentException
187169 */
188- public function registerGroups (string $ namespace , string $ basePath )
170+ public function registerGroups ($ namespace , $ basePath )
189171 {
190172 $ length = \strlen ($ basePath ) + 1 ;
191173 $ iterator = Helper::recursiveDirectoryIterator ($ basePath , $ this ->getFileFilter ());
192-
193174 foreach ($ iterator as $ file ) {
194175 $ class = $ namespace . '\\' . \substr ($ file , $ length , -4 );
195176 $ this ->addController ($ class );
@@ -205,12 +186,10 @@ protected function getFileFilter()
205186 {
206187 return function (\SplFileInfo $ f ) {
207188 $ name = $ f ->getFilename ();
208-
209189 // Skip hidden files and directories.
210190 if ($ name [0 ] === '. ' ) {
211191 return false ;
212192 }
213-
214193 // go on read sub-dir
215194 if ($ f ->isDir ()) {
216195 return true ;
@@ -220,55 +199,42 @@ protected function getFileFilter()
220199 return $ f ->isFile () && \substr ($ name , -4 ) === '.php ' ;
221200 };
222201 }
223-
224202 /****************************************************************************
225203 * dispatch and run console controller/command
226204 ****************************************************************************/
227-
228205 /**
229206 * @inheritdoc
230207 * @throws \InvalidArgumentException
231208 */
232209 protected function dispatch ($ name )
233210 {
234211 $ sep = $ this ->delimiter ?: '/ ' ;
235-
236212 //// is a command name
237-
238213 if ($ this ->isCommand ($ name )) {
239214 return $ this ->runCommand ($ name , true );
240215 }
241-
242216 //// is a controller name
243-
244217 $ action = '' ;
245-
246218 // like 'home/index'
247219 if (strpos ($ name , $ sep ) > 0 ) {
248220 $ input = array_filter (explode ($ sep , $ name ));
249221 list ($ name , $ action ) = \count ($ input ) > 2 ? array_splice ($ input , 2 ) : $ input ;
250222 }
251-
252223 if ($ this ->isController ($ name )) {
253224 return $ this ->runAction ($ name , $ action , true );
254225 }
255-
256226 // command not found
257227 if (true !== self ::fire (self ::ON_NOT_FOUND , [$ this ])) {
258228 $ this ->output ->liteError ("The console command ' {$ name }' not exists! " );
259-
260229 // find similar command names by similar_text()
261230 $ similar = [];
262231 $ commands = array_merge ($ this ->getControllerNames (), $ this ->getCommandNames ());
263-
264232 foreach ($ commands as $ command ) {
265233 similar_text ($ name , $ command , $ percent );
266-
267234 if (45 <= (int )$ percent ) {
268235 $ similar [] = $ command ;
269236 }
270237 }
271-
272238 if ($ similar ) {
273239 $ this ->write (sprintf ("\nMaybe what you mean is: \n <info>%s</info> " , implode (', ' , $ similar )));
274240 } else {
@@ -290,26 +256,21 @@ public function runCommand($name, $believable = false)
290256 {
291257 // if $believable = true, will skip check.
292258 if (!$ believable && $ this ->isCommand ($ name )) {
293- throw new \InvalidArgumentException ("The console independent-command [ $ name] not exists! " );
259+ throw new \InvalidArgumentException ("The console independent-command [ { $ name} ] not exists! " );
294260 }
295-
296261 /** @var \Closure|string $handler Command class */
297262 $ handler = $ this ->commands [$ name ];
298-
299263 if (\is_object ($ handler ) && method_exists ($ handler , '__invoke ' )) {
300264 $ status = $ handler ($ this ->input , $ this ->output );
301265 } else {
302266 if (!class_exists ($ handler )) {
303- throw new \InvalidArgumentException ("The console command class [ $ handler] not exists! " );
267+ throw new \InvalidArgumentException ("The console command class [ { $ handler} ] not exists! " );
304268 }
305-
306269 /** @var Command $object */
307270 $ object = new $ handler ($ this ->input , $ this ->output );
308-
309- if (!($ object instanceof Command)) {
310- throw new \InvalidArgumentException ("The console command class [ $ handler] must instanceof the " . Command::class);
271+ if (!$ object instanceof Command) {
272+ throw new \InvalidArgumentException ("The console command class [ {$ handler }] must instanceof the " . Command::class);
311273 }
312-
313274 $ object ::setName ($ name );
314275 $ object ->setApp ($ this );
315276 $ status = $ object ->run ();
@@ -330,28 +291,23 @@ public function runAction($name, $action, $believable = false, $standAlone = fal
330291 {
331292 // if $believable = true, will skip check.
332293 if (!$ believable && !$ this ->isController ($ name )) {
333- throw new \InvalidArgumentException ("The console controller-command [ $ name] not exists! " );
294+ throw new \InvalidArgumentException ("The console controller-command [ { $ name} ] not exists! " );
334295 }
335-
336296 // Controller class
337297 $ controller = $ this ->controllers [$ name ];
338-
339298 if (!class_exists ($ controller )) {
340- throw new \InvalidArgumentException ("The console controller class [ $ controller] not exists! " );
299+ throw new \InvalidArgumentException ("The console controller class [ { $ controller} ] not exists! " );
341300 }
342-
343301 /** @var Controller $object */
344302 $ object = new $ controller ($ this ->input , $ this ->output );
345-
346- if (!($ object instanceof Controller)) {
347- throw new \InvalidArgumentException ("The console controller class [ $ object] must instanceof the " . Controller::class);
303+ if (!$ object instanceof Controller) {
304+ throw new \InvalidArgumentException ("The console controller class [ {$ object }] must instanceof the " . Controller::class);
348305 }
349-
350306 $ object ::setName ($ name );
351307 $ object ->setApp ($ this );
352308 $ object ->setDelimiter ($ this ->delimiter );
353309 $ object ->setStandAlone ($ standAlone );
354310
355311 return $ object ->run ($ action );
356312 }
357- }
313+ }
0 commit comments