1
1
<?php
2
+
2
3
/**
3
4
* Created by PhpStorm.
4
5
* User: inhere
@@ -20,38 +21,30 @@ class Application extends AbstractApplication
20
21
/****************************************************************************
21
22
* register console controller/command
22
23
****************************************************************************/
23
-
24
24
/**
25
25
* Register a app group command(by controller)
26
26
* @param string $name The controller name
27
27
* @param string $class The controller class
28
28
* @return static
29
29
* @throws \InvalidArgumentException
30
30
*/
31
- public function controller (string $ name , string $ class = null )
31
+ public function controller ($ name , $ class = null )
32
32
{
33
33
if (!$ class && class_exists ($ name )) {
34
34
/** @var Controller $class */
35
35
$ class = $ name ;
36
36
$ name = $ class ::getName ();
37
37
}
38
-
39
38
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 );
43
40
}
44
-
45
41
$ this ->validateName ($ name , true );
46
-
47
42
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! " );
49
44
}
50
-
51
45
if (!is_subclass_of ($ class , Controller::class)) {
52
46
throw new \InvalidArgumentException ('The console controller class must is subclass of the: ' . Controller::class);
53
47
}
54
-
55
48
$ this ->controllers [$ name ] = $ class ;
56
49
57
50
return $ this ;
@@ -62,7 +55,7 @@ public function controller(string $name, string $class = null)
62
55
* @see Application::controller()
63
56
* @throws \InvalidArgumentException
64
57
*/
65
- public function addController (string $ name , string $ class = null )
58
+ public function addController ($ name , $ class = null )
66
59
{
67
60
return $ this ->controller ($ name , $ class );
68
61
}
@@ -83,42 +76,32 @@ public function controllers(array $controllers)
83
76
* @return $this
84
77
* @throws \InvalidArgumentException
85
78
*/
86
- public function command (string $ name , $ handler = null , $ description = null )
79
+ public function command ($ name , $ handler = null , $ description = null )
87
80
{
88
81
if (!$ handler && class_exists ($ name )) {
89
82
/** @var Command $name */
90
83
$ handler = $ name ;
91
84
$ name = $ name ::getName ();
92
85
}
93
-
94
86
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} " );
96
88
}
97
-
98
89
$ this ->validateName ($ name );
99
-
100
90
if (isset ($ this ->commands [$ name ])) {
101
- throw new \InvalidArgumentException ("Command ' $ name' have been registered! " );
91
+ throw new \InvalidArgumentException ("Command ' { $ name} ' have been registered! " );
102
92
}
103
-
104
93
if (\is_string ($ handler )) {
105
94
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! " );
107
96
}
108
-
109
97
if (!is_subclass_of ($ handler , Command::class)) {
110
98
throw new \InvalidArgumentException ('The console command class must is subclass of the: ' . Command::class);
111
99
}
112
100
} 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));
117
102
}
118
-
119
103
// is an class name string
120
104
$ this ->commands [$ name ] = $ handler ;
121
-
122
105
if ($ description ) {
123
106
$ this ->addCommandMessage ($ name , $ description );
124
107
}
@@ -141,7 +124,7 @@ public function commands(array $commands)
141
124
* @return $this
142
125
* @throws \InvalidArgumentException
143
126
*/
144
- public function addCommand (string $ name , $ handler = null )
127
+ public function addCommand ($ name , $ handler = null )
145
128
{
146
129
return $ this ->command ($ name , $ handler );
147
130
}
@@ -153,7 +136,7 @@ public function addCommand(string $name, $handler = null)
153
136
* @return static
154
137
* @throws \InvalidArgumentException
155
138
*/
156
- public function addGroup (string $ name , string $ controller = null )
139
+ public function addGroup ($ name , $ controller = null )
157
140
{
158
141
return $ this ->controller ($ name , $ controller );
159
142
}
@@ -165,11 +148,10 @@ public function addGroup(string $name, string $controller = null)
165
148
* @return $this
166
149
* @throws \InvalidArgumentException
167
150
*/
168
- public function registerCommands (string $ namespace , string $ basePath )
151
+ public function registerCommands ($ namespace , $ basePath )
169
152
{
170
153
$ length = \strlen ($ basePath ) + 1 ;
171
154
$ iterator = Helper::recursiveDirectoryIterator ($ basePath , $ this ->getFileFilter ());
172
-
173
155
foreach ($ iterator as $ file ) {
174
156
$ class = $ namespace . '\\' . \substr ($ file , $ length , -4 );
175
157
$ this ->addCommand ($ class );
@@ -185,11 +167,10 @@ public function registerCommands(string $namespace, string $basePath)
185
167
* @return $this
186
168
* @throws \InvalidArgumentException
187
169
*/
188
- public function registerGroups (string $ namespace , string $ basePath )
170
+ public function registerGroups ($ namespace , $ basePath )
189
171
{
190
172
$ length = \strlen ($ basePath ) + 1 ;
191
173
$ iterator = Helper::recursiveDirectoryIterator ($ basePath , $ this ->getFileFilter ());
192
-
193
174
foreach ($ iterator as $ file ) {
194
175
$ class = $ namespace . '\\' . \substr ($ file , $ length , -4 );
195
176
$ this ->addController ($ class );
@@ -205,12 +186,10 @@ protected function getFileFilter()
205
186
{
206
187
return function (\SplFileInfo $ f ) {
207
188
$ name = $ f ->getFilename ();
208
-
209
189
// Skip hidden files and directories.
210
190
if ($ name [0 ] === '. ' ) {
211
191
return false ;
212
192
}
213
-
214
193
// go on read sub-dir
215
194
if ($ f ->isDir ()) {
216
195
return true ;
@@ -220,55 +199,42 @@ protected function getFileFilter()
220
199
return $ f ->isFile () && \substr ($ name , -4 ) === '.php ' ;
221
200
};
222
201
}
223
-
224
202
/****************************************************************************
225
203
* dispatch and run console controller/command
226
204
****************************************************************************/
227
-
228
205
/**
229
206
* @inheritdoc
230
207
* @throws \InvalidArgumentException
231
208
*/
232
209
protected function dispatch ($ name )
233
210
{
234
211
$ sep = $ this ->delimiter ?: '/ ' ;
235
-
236
212
//// is a command name
237
-
238
213
if ($ this ->isCommand ($ name )) {
239
214
return $ this ->runCommand ($ name , true );
240
215
}
241
-
242
216
//// is a controller name
243
-
244
217
$ action = '' ;
245
-
246
218
// like 'home/index'
247
219
if (strpos ($ name , $ sep ) > 0 ) {
248
220
$ input = array_filter (explode ($ sep , $ name ));
249
221
list ($ name , $ action ) = \count ($ input ) > 2 ? array_splice ($ input , 2 ) : $ input ;
250
222
}
251
-
252
223
if ($ this ->isController ($ name )) {
253
224
return $ this ->runAction ($ name , $ action , true );
254
225
}
255
-
256
226
// command not found
257
227
if (true !== self ::fire (self ::ON_NOT_FOUND , [$ this ])) {
258
228
$ this ->output ->liteError ("The console command ' {$ name }' not exists! " );
259
-
260
229
// find similar command names by similar_text()
261
230
$ similar = [];
262
231
$ commands = array_merge ($ this ->getControllerNames (), $ this ->getCommandNames ());
263
-
264
232
foreach ($ commands as $ command ) {
265
233
similar_text ($ name , $ command , $ percent );
266
-
267
234
if (45 <= (int )$ percent ) {
268
235
$ similar [] = $ command ;
269
236
}
270
237
}
271
-
272
238
if ($ similar ) {
273
239
$ this ->write (sprintf ("\nMaybe what you mean is: \n <info>%s</info> " , implode (', ' , $ similar )));
274
240
} else {
@@ -290,26 +256,21 @@ public function runCommand($name, $believable = false)
290
256
{
291
257
// if $believable = true, will skip check.
292
258
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! " );
294
260
}
295
-
296
261
/** @var \Closure|string $handler Command class */
297
262
$ handler = $ this ->commands [$ name ];
298
-
299
263
if (\is_object ($ handler ) && method_exists ($ handler , '__invoke ' )) {
300
264
$ status = $ handler ($ this ->input , $ this ->output );
301
265
} else {
302
266
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! " );
304
268
}
305
-
306
269
/** @var Command $object */
307
270
$ 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);
311
273
}
312
-
313
274
$ object ::setName ($ name );
314
275
$ object ->setApp ($ this );
315
276
$ status = $ object ->run ();
@@ -330,28 +291,23 @@ public function runAction($name, $action, $believable = false, $standAlone = fal
330
291
{
331
292
// if $believable = true, will skip check.
332
293
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! " );
334
295
}
335
-
336
296
// Controller class
337
297
$ controller = $ this ->controllers [$ name ];
338
-
339
298
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! " );
341
300
}
342
-
343
301
/** @var Controller $object */
344
302
$ 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);
348
305
}
349
-
350
306
$ object ::setName ($ name );
351
307
$ object ->setApp ($ this );
352
308
$ object ->setDelimiter ($ this ->delimiter );
353
309
$ object ->setStandAlone ($ standAlone );
354
310
355
311
return $ object ->run ($ action );
356
312
}
357
- }
313
+ }
0 commit comments