Skip to content

Commit 6a4b640

Browse files
committed
feat: support add flags config for add alone Closure command
1 parent d94eeee commit 6a4b640

File tree

6 files changed

+138
-165
lines changed

6 files changed

+138
-165
lines changed

src/Application.php

Lines changed: 55 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@
1818
use Inhere\Console\Util\Helper;
1919
use InvalidArgumentException;
2020
use RuntimeException;
21-
use SplFileInfo;
2221
use Throwable;
22+
use Toolkit\FsUtil\Dir;
2323
use Toolkit\PFlag\SFlags;
24+
use Toolkit\Stdlib\Helper\Assert;
2425
use Toolkit\Stdlib\Helper\DataHelper;
2526
use function array_unshift;
2627
use function class_exists;
@@ -60,7 +61,11 @@ public function __construct(array $config = [], Input $input = null, Output $out
6061
****************************************************************************/
6162

6263
/**
63-
* {@inheritdoc}
64+
* @param string $name
65+
* @param ControllerInterface|string|null $class
66+
* @param array $config
67+
*
68+
* @return $this
6469
*/
6570
public function controller(string $name, ControllerInterface|string $class = null, array $config = []): static
6671
{
@@ -100,8 +105,6 @@ public function addController(string $name, ControllerInterface|string $class =
100105

101106
/**
102107
* @param array $controllers
103-
*
104-
* @throws InvalidArgumentException
105108
*/
106109
public function controllers(array $controllers): void
107110
{
@@ -110,27 +113,18 @@ public function controllers(array $controllers): void
110113

111114
/**
112115
* @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
126116
*/
127117
public function addControllers(array $controllers): void
128118
{
129119
$this->router->addControllers($controllers);
130120
}
131121

132122
/**
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
134128
*/
135129
public function command(string $name, string|Closure|CommandInterface $handler = null, array $config = []): static
136130
{
@@ -144,31 +138,27 @@ public function command(string $name, string|Closure|CommandInterface $handler =
144138
* add command
145139
*
146140
* @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.
149143
*
150144
* @return Application
151145
* @see command()
152146
*/
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
154148
{
155149
return $this->command($name, $handler, $config);
156150
}
157151

158152
/**
159-
* @param array $commands
160-
*
161-
* @throws InvalidArgumentException
153+
* @param array{string, mixed} $commands
162154
*/
163155
public function addCommands(array $commands): void
164156
{
165157
$this->router->addCommands($commands);
166158
}
167159

168160
/**
169-
* @param array $commands
170-
*
171-
* @throws InvalidArgumentException
161+
* @param array{string, mixed} $commands
172162
*/
173163
public function commands(array $commands): void
174164
{
@@ -186,14 +176,14 @@ public function commands(array $commands): void
186176
* @param string $basePath
187177
*
188178
* @return $this
189-
* @throws InvalidArgumentException
190179
*/
191-
public function registerCommands(string $namespace, string $basePath): self
180+
public function registerCommands(string $namespace, string $basePath): static
192181
{
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());
195185

196-
foreach ($iterator as $file) {
186+
foreach ($iter as $file) {
197187
$class = $namespace . '\\' . substr($file->getPathName(), $length, -4);
198188
$this->addCommand($class);
199189
}
@@ -212,40 +202,18 @@ public function registerCommands(string $namespace, string $basePath): self
212202
*/
213203
public function registerGroups(string $namespace, string $basePath): self
214204
{
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());
217208

218-
foreach ($iterator as $file) {
209+
foreach ($iter as $file) {
219210
$class = $namespace . '\\' . substr($file->getPathName(), $length, -4);
220211
$this->addController($class);
221212
}
222213

223214
return $this;
224215
}
225216

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-
249217
/****************************************************************************
250218
* Dispatch and run console controller/command
251219
****************************************************************************/
@@ -298,39 +266,49 @@ public function dispatch(string $name, array $args = []): mixed
298266
}
299267

300268
// save command ID
301-
$cmdOptions = $info['options'];
302-
unset($info['options']);
269+
$cmdConf = $info['config'];
270+
unset($info['config']);
303271
$this->input->setCommandId($info['cmdId']);
304272

305273
// is command
306274
if ($info['type'] === Router::TYPE_SINGLE) {
307-
return $this->runCommand($info, $cmdOptions, $args);
275+
return $this->runCommand($info, $cmdConf, $args);
308276
}
309277

310278
// is controller/group
311-
return $this->runAction($info, $cmdOptions, $args);
279+
return $this->runAction($info, $cmdConf, $args);
312280
}
313281

314282
/**
315283
* run a independent command
316284
*
317285
* @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.
319287
* @param array $args
320288
*
321289
* @return mixed
322290
* @throws Throwable
323291
*/
324-
protected function runCommand(array $info, array $options, array $args): mixed
292+
protected function runCommand(array $info, array $config, array $args): mixed
325293
{
326-
/** @var Closure|string $handler Command class or handler func */
294+
/** @var Closure|class-string{Command} $handler Command class or handler func */
327295
$handler = $info['handler'];
296+
$iptName = $info['name'];
328297

329298
if (is_object($handler) && method_exists($handler, '__invoke')) {
330299
$fs = SFlags::new();
300+
$fs->setName($iptName);
331301
$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');
334312

335313
// save to input object
336314
$this->input->setFs($fs);
@@ -339,21 +317,17 @@ protected function runCommand(array $info, array $options, array $args): mixed
339317
return 0; // render help
340318
}
341319

342-
$result = $handler($this->input, $this->output);
320+
$result = $handler($fs, $this->output);
343321
} 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!");
347323

348-
/** @var Command $object */
349324
$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);
353326

327+
/** @var Command $object */
354328
$object::setName($info['cmdId']); // real command name.
355329
$object->setApp($this);
356-
$object->setCommandName($info['name']);
330+
$object->setCommandName($iptName);
357331
$result = $object->run($args);
358332
}
359333

@@ -363,21 +337,18 @@ protected function runCommand(array $info, array $options, array $args): mixed
363337
/**
364338
* Execute an action in a group command(controller)
365339
*
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
371342
* @param array $args
372343
* @param bool $detachedRun
373344
*
374345
* @return mixed
375346
* @throws Throwable
376347
*/
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
378349
{
379350
$controller = $this->createController($info);
380-
$controller::setDesc($options['desc'] ?? '');
351+
$controller::setDesc($config['desc'] ?? '');
381352

382353
if ($detachedRun) {
383354
$controller->setDetached();
@@ -408,7 +379,7 @@ public function getController(string $name): Controller
408379
}
409380

410381
/**
411-
* @param array $info
382+
* @param array{name: string, group: string, handler: mixed} $info
412383
*
413384
* @return Controller
414385
*/

src/Component/Formatter/HelpPanel.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,17 +89,13 @@ public static function show(array $config): void
8989
$config = array_merge([
9090
'desc' => '',
9191
'usage' => '',
92-
9392
'commands' => [],
9493
'arguments' => [],
9594
'options' => [],
96-
97-
'examples' => [],
98-
95+
'examples' => [],
9996
// extra
100-
'extras' => [],
101-
102-
'_opts' => [],
97+
'extras' => [],
98+
'_opts' => [],
10399
], $config);
104100

105101
// some option for show.

src/Component/PharCompiler.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use Seld\PharUtils\Timestamps;
2525
use SplFileInfo;
2626
use SplQueue;
27+
use Toolkit\FsUtil\Dir;
2728
use Toolkit\FsUtil\File;
2829
use Toolkit\Sys\Sys;
2930
use Traversable;
@@ -637,11 +638,10 @@ public function findChangedByGit(): ?Generator
637638
* @param string $directory
638639
*
639640
* @return Iterator
640-
* @throws InvalidArgumentException
641641
*/
642642
protected function findFiles(string $directory): Iterator
643643
{
644-
return Helper::directoryIterator(
644+
return Dir::getIterator(
645645
$directory,
646646
$this->getIteratorFilter(),
647647
FilesystemIterator::KEY_AS_PATHNAME | FilesystemIterator::CURRENT_AS_FILEINFO | FilesystemIterator::SKIP_DOTS

0 commit comments

Comments
 (0)