8
8
9
9
namespace Toolkit \Cli ;
10
10
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
+
11
25
/**
12
26
* Class App - A lite CLI Application
27
+ *
13
28
* @package Inhere\Console
14
29
*/
15
30
class App
@@ -27,10 +42,14 @@ class App
27
42
*/
28
43
private $ opts = [];
29
44
30
- /** @var string */
45
+ /**
46
+ * @var string
47
+ */
31
48
private $ script ;
32
49
33
- /** @var string */
50
+ /**
51
+ * @var string
52
+ */
34
53
private $ command = '' ;
35
54
36
55
/**
@@ -50,24 +69,26 @@ class App
50
69
51
70
/**
52
71
* Class constructor.
72
+ *
53
73
* @param array|null $argv
54
74
*/
55
75
public function __construct (array $ argv = null )
56
76
{
57
77
// get current dir
58
- $ this ->pwd = \ getcwd ();
78
+ $ this ->pwd = getcwd ();
59
79
60
80
// parse cli argv
61
81
$ argv = $ argv ?? (array )$ _SERVER ['argv ' ];
62
82
// get script file
63
- $ this ->script = \ array_shift ($ argv );
83
+ $ this ->script = array_shift ($ argv );
64
84
// parse flags
65
- [$ this ->args , $ this ->opts ] = Flags::simpleParseArgv ($ argv );
85
+ [$ this ->args , $ this ->opts ] = Flags::parseArgv ($ argv );
66
86
}
67
87
68
88
/**
69
89
* @param bool $exit
70
- * @throws \InvalidArgumentException
90
+ *
91
+ * @throws InvalidArgumentException
71
92
*/
72
93
public function run (bool $ exit = true ): void
73
94
{
@@ -81,7 +102,8 @@ public function run(bool $exit = true): void
81
102
82
103
/**
83
104
* @param bool $exit
84
- * @throws \InvalidArgumentException
105
+ *
106
+ * @throws InvalidArgumentException
85
107
*/
86
108
public function dispatch (bool $ exit = true ): void
87
109
{
@@ -98,7 +120,7 @@ public function dispatch(bool $exit = true): void
98
120
} else {
99
121
$ this ->displayHelp ("The command {$ command } not exists! " );
100
122
}
101
- } catch (\ Throwable $ e ) {
123
+ } catch (Throwable $ e ) {
102
124
$ status = $ this ->handleException ($ e );
103
125
}
104
126
@@ -118,51 +140,47 @@ public function stop($code = 0): void
118
140
/**
119
141
* @param string $command
120
142
* @param $handler
143
+ *
121
144
* @return mixed
122
- * @throws \ InvalidArgumentException
145
+ * @throws InvalidArgumentException
123
146
*/
124
147
public function runHandler (string $ command , $ handler )
125
148
{
126
- if (\ is_string ($ handler )) {
149
+ if (is_string ($ handler )) {
127
150
// function name
128
- if (\ function_exists ($ handler )) {
151
+ if (function_exists ($ handler )) {
129
152
return $ handler ($ this );
130
153
}
131
154
132
- if (\ class_exists ($ handler )) {
155
+ if (class_exists ($ handler )) {
133
156
$ handler = new $ handler ;
134
157
135
158
// $handler->execute()
136
- if (\ method_exists ($ handler , 'execute ' )) {
159
+ if (method_exists ($ handler , 'execute ' )) {
137
160
return $ handler ->execute ($ this );
138
161
}
139
162
}
140
163
}
141
164
142
165
// a \Closure OR $handler->__invoke()
143
- if (\ method_exists ($ handler , '__invoke ' )) {
166
+ if (method_exists ($ handler , '__invoke ' )) {
144
167
return $ handler ($ this );
145
168
}
146
169
147
- throw new \ InvalidArgumentException ("Invalid handler of the command: $ command " );
170
+ throw new InvalidArgumentException ("Invalid handler of the command: $ command " );
148
171
}
149
172
150
173
/**
151
- * @param \Throwable $e
174
+ * @param Throwable $e
175
+ *
152
176
* @return int
153
177
*/
154
- protected function handleException (\ Throwable $ e ): int
178
+ protected function handleException (Throwable $ e ): int
155
179
{
156
180
$ code = $ e ->getCode () !== 0 ? $ e ->getCode () : 133 ;
157
181
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 ());
166
184
167
185
return $ code ;
168
186
}
@@ -171,33 +189,35 @@ protected function handleException(\Throwable $e): int
171
189
* @param string $command
172
190
* @param callable $handler
173
191
* @param string $description
174
- * @throws \InvalidArgumentException
192
+ *
193
+ * @throws InvalidArgumentException
175
194
*/
176
195
public function addCommand (string $ command , callable $ handler , string $ description = '' ): void
177
196
{
178
197
if (!$ command || !$ handler ) {
179
- throw new \ InvalidArgumentException ('Invalid arguments ' );
198
+ throw new InvalidArgumentException ('Invalid arguments ' );
180
199
}
181
200
182
- if (($ len = \ strlen ($ command )) > $ this ->keyWidth ) {
201
+ if (($ len = strlen ($ command )) > $ this ->keyWidth ) {
183
202
$ this ->keyWidth = $ len ;
184
203
}
185
204
186
205
$ this ->commands [$ command ] = $ handler ;
187
- $ this ->messages [$ command ] = \ trim ($ description );
206
+ $ this ->messages [$ command ] = trim ($ description );
188
207
}
189
208
190
209
/**
191
210
* @param array $commands
192
- * @throws \InvalidArgumentException
211
+ *
212
+ * @throws InvalidArgumentException
193
213
*/
194
214
public function commands (array $ commands ): void
195
215
{
196
216
foreach ($ commands as $ command => $ handler ) {
197
217
$ des = '' ;
198
218
199
- if (\ is_array ($ handler )) {
200
- $ conf = \ array_values ($ handler );
219
+ if (is_array ($ handler )) {
220
+ $ conf = array_values ($ handler );
201
221
$ handler = $ conf [0 ];
202
222
$ des = $ conf [1 ] ?? '' ;
203
223
}
@@ -224,7 +244,7 @@ public function displayHelp(string $err = ''): void
224
244
$ help = "Welcome to the Lite Console Application. \n\n<comment>Available Commands:</comment> \n" ;
225
245
226
246
foreach ($ this ->messages as $ command => $ desc ) {
227
- $ command = \ str_pad ($ command , $ commandWidth , ' ' );
247
+ $ command = str_pad ($ command , $ commandWidth , ' ' );
228
248
$ desc = $ desc ?: 'No description for the command ' ;
229
249
$ help .= " $ command $ desc \n" ;
230
250
}
@@ -236,6 +256,7 @@ public function displayHelp(string $err = ''): void
236
256
/**
237
257
* @param string|int $name
238
258
* @param mixed $default
259
+ *
239
260
* @return mixed|null
240
261
*/
241
262
public function getArg ($ name , $ default = null )
@@ -246,6 +267,7 @@ public function getArg($name, $default = null)
246
267
/**
247
268
* @param string $name
248
269
* @param mixed $default
270
+ *
249
271
* @return mixed|null
250
272
*/
251
273
public function getOpt (string $ name , $ default = null )
0 commit comments