88
99namespace Toolkit \Cli ;
1010
11+ use InvalidArgumentException ;
12+ use Throwable ;
13+ use function array_shift ;
14+ use function array_values ;
15+ use function class_exists ;
16+ use function function_exists ;
17+ use function getcwd ;
18+ use function is_array ;
19+ use function is_string ;
20+ use function method_exists ;
21+ use function str_pad ;
22+ use function strlen ;
23+ use function trim ;
24+
1125/**
1226 * Class App - A lite CLI Application
27+ *
1328 * @package Inhere\Console
1429 */
1530class App
@@ -27,10 +42,14 @@ class App
2742 */
2843 private $ opts = [];
2944
30- /** @var string */
45+ /**
46+ * @var string
47+ */
3148 private $ script ;
3249
33- /** @var string */
50+ /**
51+ * @var string
52+ */
3453 private $ command = '' ;
3554
3655 /**
@@ -50,24 +69,26 @@ class App
5069
5170 /**
5271 * Class constructor.
72+ *
5373 * @param array|null $argv
5474 */
5575 public function __construct (array $ argv = null )
5676 {
5777 // get current dir
58- $ this ->pwd = \ getcwd ();
78+ $ this ->pwd = getcwd ();
5979
6080 // parse cli argv
6181 $ argv = $ argv ?? (array )$ _SERVER ['argv ' ];
6282 // get script file
63- $ this ->script = \ array_shift ($ argv );
83+ $ this ->script = array_shift ($ argv );
6484 // parse flags
65- [$ this ->args , $ this ->opts ] = Flags::simpleParseArgv ($ argv );
85+ [$ this ->args , $ this ->opts ] = Flags::parseArgv ($ argv );
6686 }
6787
6888 /**
6989 * @param bool $exit
70- * @throws \InvalidArgumentException
90+ *
91+ * @throws InvalidArgumentException
7192 */
7293 public function run (bool $ exit = true ): void
7394 {
@@ -81,7 +102,8 @@ public function run(bool $exit = true): void
81102
82103 /**
83104 * @param bool $exit
84- * @throws \InvalidArgumentException
105+ *
106+ * @throws InvalidArgumentException
85107 */
86108 public function dispatch (bool $ exit = true ): void
87109 {
@@ -98,7 +120,7 @@ public function dispatch(bool $exit = true): void
98120 } else {
99121 $ this ->displayHelp ("The command {$ command } not exists! " );
100122 }
101- } catch (\ Throwable $ e ) {
123+ } catch (Throwable $ e ) {
102124 $ status = $ this ->handleException ($ e );
103125 }
104126
@@ -118,51 +140,47 @@ public function stop($code = 0): void
118140 /**
119141 * @param string $command
120142 * @param $handler
143+ *
121144 * @return mixed
122- * @throws \ InvalidArgumentException
145+ * @throws InvalidArgumentException
123146 */
124147 public function runHandler (string $ command , $ handler )
125148 {
126- if (\ is_string ($ handler )) {
149+ if (is_string ($ handler )) {
127150 // function name
128- if (\ function_exists ($ handler )) {
151+ if (function_exists ($ handler )) {
129152 return $ handler ($ this );
130153 }
131154
132- if (\ class_exists ($ handler )) {
155+ if (class_exists ($ handler )) {
133156 $ handler = new $ handler ;
134157
135158 // $handler->execute()
136- if (\ method_exists ($ handler , 'execute ' )) {
159+ if (method_exists ($ handler , 'execute ' )) {
137160 return $ handler ->execute ($ this );
138161 }
139162 }
140163 }
141164
142165 // a \Closure OR $handler->__invoke()
143- if (\ method_exists ($ handler , '__invoke ' )) {
166+ if (method_exists ($ handler , '__invoke ' )) {
144167 return $ handler ($ this );
145168 }
146169
147- throw new \ InvalidArgumentException ("Invalid handler of the command: $ command " );
170+ throw new InvalidArgumentException ("Invalid handler of the command: $ command " );
148171 }
149172
150173 /**
151- * @param \Throwable $e
174+ * @param Throwable $e
175+ *
152176 * @return int
153177 */
154- protected function handleException (\ Throwable $ e ): int
178+ protected function handleException (Throwable $ e ): int
155179 {
156180 $ code = $ e ->getCode () !== 0 ? $ e ->getCode () : 133 ;
157181
158- printf (
159- "Exception(%d): %s \nFile: %s(Line %d) \nTrace: \n%s \n" ,
160- $ code ,
161- $ e ->getMessage (),
162- $ e ->getFile (),
163- $ e ->getLine (),
164- $ e ->getTraceAsString ()
165- );
182+ printf ("Exception(%d): %s \nFile: %s(Line %d) \nTrace: \n%s \n" , $ code , $ e ->getMessage (), $ e ->getFile (),
183+ $ e ->getLine (), $ e ->getTraceAsString ());
166184
167185 return $ code ;
168186 }
@@ -171,33 +189,35 @@ protected function handleException(\Throwable $e): int
171189 * @param string $command
172190 * @param callable $handler
173191 * @param string $description
174- * @throws \InvalidArgumentException
192+ *
193+ * @throws InvalidArgumentException
175194 */
176195 public function addCommand (string $ command , callable $ handler , string $ description = '' ): void
177196 {
178197 if (!$ command || !$ handler ) {
179- throw new \ InvalidArgumentException ('Invalid arguments ' );
198+ throw new InvalidArgumentException ('Invalid arguments ' );
180199 }
181200
182- if (($ len = \ strlen ($ command )) > $ this ->keyWidth ) {
201+ if (($ len = strlen ($ command )) > $ this ->keyWidth ) {
183202 $ this ->keyWidth = $ len ;
184203 }
185204
186205 $ this ->commands [$ command ] = $ handler ;
187- $ this ->messages [$ command ] = \ trim ($ description );
206+ $ this ->messages [$ command ] = trim ($ description );
188207 }
189208
190209 /**
191210 * @param array $commands
192- * @throws \InvalidArgumentException
211+ *
212+ * @throws InvalidArgumentException
193213 */
194214 public function commands (array $ commands ): void
195215 {
196216 foreach ($ commands as $ command => $ handler ) {
197217 $ des = '' ;
198218
199- if (\ is_array ($ handler )) {
200- $ conf = \ array_values ($ handler );
219+ if (is_array ($ handler )) {
220+ $ conf = array_values ($ handler );
201221 $ handler = $ conf [0 ];
202222 $ des = $ conf [1 ] ?? '' ;
203223 }
@@ -224,7 +244,7 @@ public function displayHelp(string $err = ''): void
224244 $ help = "Welcome to the Lite Console Application. \n\n<comment>Available Commands:</comment> \n" ;
225245
226246 foreach ($ this ->messages as $ command => $ desc ) {
227- $ command = \ str_pad ($ command , $ commandWidth , ' ' );
247+ $ command = str_pad ($ command , $ commandWidth , ' ' );
228248 $ desc = $ desc ?: 'No description for the command ' ;
229249 $ help .= " $ command $ desc \n" ;
230250 }
@@ -236,6 +256,7 @@ public function displayHelp(string $err = ''): void
236256 /**
237257 * @param string|int $name
238258 * @param mixed $default
259+ *
239260 * @return mixed|null
240261 */
241262 public function getArg ($ name , $ default = null )
@@ -246,6 +267,7 @@ public function getArg($name, $default = null)
246267 /**
247268 * @param string $name
248269 * @param mixed $default
270+ *
249271 * @return mixed|null
250272 */
251273 public function getOpt (string $ name , $ default = null )
0 commit comments