18
18
use Inhere \Console \Util \Helper ;
19
19
use InvalidArgumentException ;
20
20
use RuntimeException ;
21
- use SplFileInfo ;
22
21
use Throwable ;
22
+ use Toolkit \FsUtil \Dir ;
23
23
use Toolkit \PFlag \SFlags ;
24
+ use Toolkit \Stdlib \Helper \Assert ;
24
25
use Toolkit \Stdlib \Helper \DataHelper ;
25
26
use function array_unshift ;
26
27
use function class_exists ;
@@ -60,7 +61,11 @@ public function __construct(array $config = [], Input $input = null, Output $out
60
61
****************************************************************************/
61
62
62
63
/**
63
- * {@inheritdoc}
64
+ * @param string $name
65
+ * @param ControllerInterface|string|null $class
66
+ * @param array $config
67
+ *
68
+ * @return $this
64
69
*/
65
70
public function controller (string $ name , ControllerInterface |string $ class = null , array $ config = []): static
66
71
{
@@ -100,8 +105,6 @@ public function addController(string $name, ControllerInterface|string $class =
100
105
101
106
/**
102
107
* @param array $controllers
103
- *
104
- * @throws InvalidArgumentException
105
108
*/
106
109
public function controllers (array $ controllers ): void
107
110
{
@@ -110,27 +113,18 @@ public function controllers(array $controllers): void
110
113
111
114
/**
112
115
* @param array $controllers
113
- *
114
- * @throws InvalidArgumentException
115
- * @deprecated please use addControllers() instead it.
116
- */
117
- public function setControllers (array $ controllers ): void
118
- {
119
- $ this ->addControllers ($ controllers );
120
- }
121
-
122
- /**
123
- * @param array $controllers
124
- *
125
- * @throws InvalidArgumentException
126
116
*/
127
117
public function addControllers (array $ controllers ): void
128
118
{
129
119
$ this ->router ->addControllers ($ controllers );
130
120
}
131
121
132
122
/**
133
- * {@inheritdoc}
123
+ * @param string $name
124
+ * @param class-string|CommandInterface|null|Closure():void $handler
125
+ * @param array{desc: string, aliases: array, options: array, arguments: array} $config config the command.
126
+ *
127
+ * @return Application
134
128
*/
135
129
public function command (string $ name , string |Closure |CommandInterface $ handler = null , array $ config = []): static
136
130
{
@@ -144,31 +138,27 @@ public function command(string $name, string|Closure|CommandInterface $handler =
144
138
* add command
145
139
*
146
140
* @param string $name
147
- * @param mixed| null $handler
148
- * @param array $config
141
+ * @param class-string|CommandInterface| null|Closure():void $handler
142
+ * @param array{desc: string, aliases: array, options: array, arguments: array} $config config the command.
149
143
*
150
144
* @return Application
151
145
* @see command()
152
146
*/
153
- public function addCommand (string $ name , mixed $ handler = null , array $ config = []): self
147
+ public function addCommand (string $ name , string | Closure | CommandInterface $ handler = null , array $ config = []): static
154
148
{
155
149
return $ this ->command ($ name , $ handler , $ config );
156
150
}
157
151
158
152
/**
159
- * @param array $commands
160
- *
161
- * @throws InvalidArgumentException
153
+ * @param array{string, mixed} $commands
162
154
*/
163
155
public function addCommands (array $ commands ): void
164
156
{
165
157
$ this ->router ->addCommands ($ commands );
166
158
}
167
159
168
160
/**
169
- * @param array $commands
170
- *
171
- * @throws InvalidArgumentException
161
+ * @param array{string, mixed} $commands
172
162
*/
173
163
public function commands (array $ commands ): void
174
164
{
@@ -186,14 +176,14 @@ public function commands(array $commands): void
186
176
* @param string $basePath
187
177
*
188
178
* @return $this
189
- * @throws InvalidArgumentException
190
179
*/
191
- public function registerCommands (string $ namespace , string $ basePath ): self
180
+ public function registerCommands (string $ namespace , string $ basePath ): static
192
181
{
193
- $ length = strlen ($ basePath ) + 1 ;
194
- $ iterator = Helper::directoryIterator ($ basePath , $ this ->getFileFilter ());
182
+ $ length = strlen ($ basePath ) + 1 ;
183
+ // $iterator = Helper::directoryIterator($basePath, $this->getFileFilter());
184
+ $ iter = Dir::getIterator ($ basePath , Dir::getPhpFileFilter ());
195
185
196
- foreach ($ iterator as $ file ) {
186
+ foreach ($ iter as $ file ) {
197
187
$ class = $ namespace . '\\' . substr ($ file ->getPathName (), $ length , -4 );
198
188
$ this ->addCommand ($ class );
199
189
}
@@ -212,40 +202,18 @@ public function registerCommands(string $namespace, string $basePath): self
212
202
*/
213
203
public function registerGroups (string $ namespace , string $ basePath ): self
214
204
{
215
- $ length = strlen ($ basePath ) + 1 ;
216
- $ iterator = Helper::directoryIterator ($ basePath , $ this ->getFileFilter ());
205
+ $ length = strlen ($ basePath ) + 1 ;
206
+ // $iterator = Helper::directoryIterator($basePath, $this->getFileFilter());
207
+ $ iter = Dir::getIterator ($ basePath , Dir::getPhpFileFilter ());
217
208
218
- foreach ($ iterator as $ file ) {
209
+ foreach ($ iter as $ file ) {
219
210
$ class = $ namespace . '\\' . substr ($ file ->getPathName (), $ length , -4 );
220
211
$ this ->addController ($ class );
221
212
}
222
213
223
214
return $ this ;
224
215
}
225
216
226
- /**
227
- * @return Closure
228
- */
229
- protected function getFileFilter (): callable
230
- {
231
- return static function (SplFileInfo $ f ) {
232
- $ name = $ f ->getFilename ();
233
-
234
- // Skip hidden files and directories.
235
- if (str_starts_with ($ name , '. ' )) {
236
- return false ;
237
- }
238
-
239
- // go on read sub-dir
240
- if ($ f ->isDir ()) {
241
- return true ;
242
- }
243
-
244
- // php file
245
- return $ f ->isFile () && str_ends_with ($ name , '.php ' );
246
- };
247
- }
248
-
249
217
/****************************************************************************
250
218
* Dispatch and run console controller/command
251
219
****************************************************************************/
@@ -298,39 +266,49 @@ public function dispatch(string $name, array $args = []): mixed
298
266
}
299
267
300
268
// save command ID
301
- $ cmdOptions = $ info ['options ' ];
302
- unset($ info ['options ' ]);
269
+ $ cmdConf = $ info ['config ' ];
270
+ unset($ info ['config ' ]);
303
271
$ this ->input ->setCommandId ($ info ['cmdId ' ]);
304
272
305
273
// is command
306
274
if ($ info ['type ' ] === Router::TYPE_SINGLE ) {
307
- return $ this ->runCommand ($ info , $ cmdOptions , $ args );
275
+ return $ this ->runCommand ($ info , $ cmdConf , $ args );
308
276
}
309
277
310
278
// is controller/group
311
- return $ this ->runAction ($ info , $ cmdOptions , $ args );
279
+ return $ this ->runAction ($ info , $ cmdConf , $ args );
312
280
}
313
281
314
282
/**
315
283
* run a independent command
316
284
*
317
285
* @param array{name: string, handler: mixed, realName: string} $info
318
- * @param array $ options
286
+ * @param array{aliases: array, desc: string, options: array, arguments: array} $config The config.
319
287
* @param array $args
320
288
*
321
289
* @return mixed
322
290
* @throws Throwable
323
291
*/
324
- protected function runCommand (array $ info , array $ options , array $ args ): mixed
292
+ protected function runCommand (array $ info , array $ config , array $ args ): mixed
325
293
{
326
- /** @var Closure|string $handler Command class or handler func */
294
+ /** @var Closure|class- string{Command} $handler Command class or handler func */
327
295
$ handler = $ info ['handler ' ];
296
+ $ iptName = $ info ['name ' ];
328
297
329
298
if (is_object ($ handler ) && method_exists ($ handler , '__invoke ' )) {
330
299
$ fs = SFlags::new ();
300
+ $ fs ->setName ($ iptName );
331
301
$ fs ->addOptsByRules (GlobalOption::getAloneOptions ());
332
- $ desc = $ options ['desc ' ] ?? 'No command description message ' ;
333
- $ fs ->setDesc ($ desc );
302
+
303
+ // command flags load
304
+ if ($ cmdOpts = $ config ['options ' ] ?? null ) {
305
+ $ fs ->addOptsByRules ($ cmdOpts );
306
+ }
307
+ if ($ cmdArgs = $ config ['arguments ' ] ?? null ) {
308
+ $ fs ->addArgsByRules ($ cmdArgs );
309
+ }
310
+
311
+ $ fs ->setDesc ($ config ['desc ' ] ?? 'No command description message ' );
334
312
335
313
// save to input object
336
314
$ this ->input ->setFs ($ fs );
@@ -339,21 +317,17 @@ protected function runCommand(array $info, array $options, array $args): mixed
339
317
return 0 ; // render help
340
318
}
341
319
342
- $ result = $ handler ($ this -> input , $ this ->output );
320
+ $ result = $ handler ($ fs , $ this ->output );
343
321
} else {
344
- if (!class_exists ($ handler )) {
345
- Helper::throwInvalidArgument ("The console command class [ $ handler] not exists! " );
346
- }
322
+ Assert::isTrue (class_exists ($ handler ), "The console command class [ $ handler] not exists! " );
347
323
348
- /** @var Command $object */
349
324
$ object = new $ handler ($ this ->input , $ this ->output );
350
- if (!($ object instanceof Command)) {
351
- Helper::throwInvalidArgument ("The console command class [ $ handler] must instanceof the " . Command::class);
352
- }
325
+ Assert::isTrue ($ object instanceof Command, "Command class [ $ handler] must instanceof the " . Command::class);
353
326
327
+ /** @var Command $object */
354
328
$ object ::setName ($ info ['cmdId ' ]); // real command name.
355
329
$ object ->setApp ($ this );
356
- $ object ->setCommandName ($ info [ ' name ' ] );
330
+ $ object ->setCommandName ($ iptName );
357
331
$ result = $ object ->run ($ args );
358
332
}
359
333
@@ -363,21 +337,18 @@ protected function runCommand(array $info, array $options, array $args): mixed
363
337
/**
364
338
* Execute an action in a group command(controller)
365
339
*
366
- * @param array $info Matched route info
367
- *
368
- * @psalm-param array{action: string} $info Matched route info
369
- *
370
- * @param array $options
340
+ * @param array{action: string} $info Matched route info
341
+ * @param array $config
371
342
* @param array $args
372
343
* @param bool $detachedRun
373
344
*
374
345
* @return mixed
375
346
* @throws Throwable
376
347
*/
377
- protected function runAction (array $ info , array $ options , array $ args , bool $ detachedRun = false ): mixed
348
+ protected function runAction (array $ info , array $ config , array $ args , bool $ detachedRun = false ): mixed
378
349
{
379
350
$ controller = $ this ->createController ($ info );
380
- $ controller ::setDesc ($ options ['desc ' ] ?? '' );
351
+ $ controller ::setDesc ($ config ['desc ' ] ?? '' );
381
352
382
353
if ($ detachedRun ) {
383
354
$ controller ->setDetached ();
@@ -408,7 +379,7 @@ public function getController(string $name): Controller
408
379
}
409
380
410
381
/**
411
- * @param array $info
382
+ * @param array{name: string, group: string, handler: mixed} $info
412
383
*
413
384
* @return Controller
414
385
*/
0 commit comments