Skip to content

Commit a09383d

Browse files
committed
fix: use 'top sub' access command error
1 parent 41067a5 commit a09383d

File tree

2 files changed

+174
-169
lines changed

2 files changed

+174
-169
lines changed

src/Application.php

Lines changed: 172 additions & 167 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
use Toolkit\PFlag\SFlags;
2525
use Toolkit\Stdlib\Helper\Assert;
2626
use Toolkit\Stdlib\Helper\DataHelper;
27+
use Toolkit\Stdlib\Str;
28+
use function array_shift;
2729
use function array_unshift;
2830
use function class_exists;
2931
use function implode;
@@ -56,170 +58,6 @@ public function __construct(array $config = [], Input $input = null, Output $out
5658
parent::__construct($config, $input, $output);
5759
}
5860

59-
/****************************************************************************
60-
* register console controller/command
61-
****************************************************************************/
62-
63-
/**
64-
* @param string $name
65-
* @param ControllerInterface|string|null $class
66-
* @param array $config
67-
*
68-
* @return $this
69-
*/
70-
public function controller(string $name, ControllerInterface|string $class = null, array $config = []): static
71-
{
72-
$this->logf(Console::VERB_CRAZY, 'register group controller: %s', $name);
73-
$this->router->addGroup($name, $class, $config);
74-
75-
return $this;
76-
}
77-
78-
/**
79-
* Add group/controller
80-
*
81-
* @param string|class-string $name
82-
* @param string|ControllerInterface|null $class The controller class
83-
* @param array $config
84-
*
85-
* @return static
86-
* @see controller()
87-
*/
88-
public function addGroup(string $name, ControllerInterface|string $class = null, array $config = []): static
89-
{
90-
return $this->controller($name, $class, $config);
91-
}
92-
93-
/**
94-
* @param string $name
95-
* @param string|ControllerInterface|null $class The controller class
96-
* @param array $config
97-
*
98-
* @return $this
99-
* @see controller()
100-
*/
101-
public function addController(string $name, ControllerInterface|string $class = null, array $config = []): static
102-
{
103-
return $this->controller($name, $class, $config);
104-
}
105-
106-
/**
107-
* @param array $controllers
108-
*/
109-
public function controllers(array $controllers): void
110-
{
111-
$this->addControllers($controllers);
112-
}
113-
114-
/**
115-
* @param array $controllers
116-
*/
117-
public function addControllers(array $controllers): void
118-
{
119-
$this->router->addControllers($controllers);
120-
}
121-
122-
/**
123-
* @param string $name
124-
* @param class-string|CommandInterface|null|Closure(FlagsParser, Output):void $handler
125-
* @param array{desc: string, aliases: array, options: array, arguments: array} $config config the command.
126-
*
127-
* @return Application
128-
*/
129-
public function command(string $name, string|Closure|CommandInterface $handler = null, array $config = []): static
130-
{
131-
$this->logf(Console::VERB_CRAZY, 'register alone command: %s', $name);
132-
$this->router->addCommand($name, $handler, $config);
133-
134-
return $this;
135-
}
136-
137-
/**
138-
* add command
139-
*
140-
* @param string $name
141-
* @param class-string|CommandInterface|null|Closure(FlagsParser, Output):void $handler
142-
* @param array{desc: string, aliases: array, options: array, arguments: array} $config config the command.
143-
*
144-
* @return Application
145-
* @see command()
146-
*/
147-
public function addCommand(string $name, string|Closure|CommandInterface $handler = null, array $config = []): static
148-
{
149-
return $this->command($name, $handler, $config);
150-
}
151-
152-
/**
153-
* @param array{string, mixed} $commands
154-
*/
155-
public function addCommands(array $commands): void
156-
{
157-
$this->router->addCommands($commands);
158-
}
159-
160-
/**
161-
* @param array{string, mixed} $commands
162-
*/
163-
public function commands(array $commands): void
164-
{
165-
$this->addCommands($commands);
166-
}
167-
168-
/**
169-
* auto register commands from a dir.
170-
*
171-
* ```php
172-
* $app->registerCommands('SwagPhp\Command', dirname(__DIR__) . '/src/Command');
173-
* ```
174-
*
175-
* @param string $namespace
176-
* @param string $basePath
177-
*
178-
* @return $this
179-
*/
180-
public function registerCommands(string $namespace, string $basePath): static
181-
{
182-
$this->debugf('register commands from the namespace: %s', $namespace);
183-
184-
$length = strlen($basePath) + 1;
185-
// $iterator = Helper::directoryIterator($basePath, $this->getFileFilter());
186-
$iter = Dir::getIterator($basePath, Dir::getPhpFileFilter());
187-
188-
foreach ($iter as $file) {
189-
$subPath = substr($file->getPathName(), $length, -4);
190-
$fullClass = $namespace . '\\' . str_replace('/', '\\', $subPath);
191-
$this->addCommand($fullClass);
192-
}
193-
194-
return $this;
195-
}
196-
197-
/**
198-
* auto register controllers from a dir.
199-
*
200-
* @param string $namespace
201-
* @param string $basePath
202-
*
203-
* @return $this
204-
* @throws InvalidArgumentException
205-
*/
206-
public function registerGroups(string $namespace, string $basePath): self
207-
{
208-
$this->debugf('register groups from the namespace: %s', $namespace);
209-
210-
$length = strlen($basePath) + 1;
211-
// $iterator = Helper::directoryIterator($basePath, $this->getFileFilter());
212-
$iter = Dir::getIterator($basePath, Dir::getPhpFileFilter());
213-
214-
foreach ($iter as $file) {
215-
$subPath = substr($file->getPathName(), $length, -4);
216-
$fullClass = $namespace . '\\' . str_replace('/', '\\', $subPath);
217-
$this->addController($fullClass);
218-
}
219-
220-
return $this;
221-
}
222-
22361
/****************************************************************************
22462
* Dispatch and run console controller/command
22563
****************************************************************************/
@@ -238,11 +76,15 @@ public function dispatch(string $name, array $args = []): mixed
23876
}
23977

24078
$cmdId = $name;
241-
$this->debugf('app - begin dispatch the input command: %s, args: %s', $name, DataHelper::toString($args));
79+
$this->debugf('app - begin dispatch the input command: "%s", args: %s', $name, DataHelper::toString($args));
24280

243-
// format is: `group action`
81+
// format is: `group action` or `top sub sub2`
24482
if (strpos($name, ' ') > 0) {
245-
$cmdId = str_replace(' ', $this->delimiter, $name);
83+
$names = Str::splitTrimmed($name, ' ');
84+
$cmdId = array_shift($names);
85+
86+
// prepend elements to the beginning of $args
87+
array_unshift($args, ...$names);
24688
}
24789

24890
// match handler by input name
@@ -439,4 +281,167 @@ protected function createController(array $info): Controller
439281
$this->groupObjects[$group] = $handler;
440282
return $handler;
441283
}
284+
285+
/****************************************************************************
286+
* register console controller/command
287+
****************************************************************************/
288+
289+
/**
290+
* @param string $name
291+
* @param ControllerInterface|string|null $class
292+
* @param array $config
293+
*
294+
* @return $this
295+
*/
296+
public function controller(string $name, ControllerInterface|string $class = null, array $config = []): static
297+
{
298+
$this->logf(Console::VERB_CRAZY, 'register group controller: %s', $name);
299+
$this->router->addGroup($name, $class, $config);
300+
301+
return $this;
302+
}
303+
304+
/**
305+
* Add group/controller
306+
*
307+
* @param string|class-string $name
308+
* @param string|ControllerInterface|null $class The controller class
309+
* @param array $config
310+
*
311+
* @return static
312+
* @see controller()
313+
*/
314+
public function addGroup(string $name, ControllerInterface|string $class = null, array $config = []): static
315+
{
316+
return $this->controller($name, $class, $config);
317+
}
318+
319+
/**
320+
* @param string $name
321+
* @param string|ControllerInterface|null $class The controller class
322+
* @param array $config
323+
*
324+
* @return $this
325+
* @see controller()
326+
*/
327+
public function addController(string $name, ControllerInterface|string $class = null, array $config = []): static
328+
{
329+
return $this->controller($name, $class, $config);
330+
}
331+
332+
/**
333+
* @param array $controllers
334+
*/
335+
public function controllers(array $controllers): void
336+
{
337+
$this->addControllers($controllers);
338+
}
339+
340+
/**
341+
* @param array $controllers
342+
*/
343+
public function addControllers(array $controllers): void
344+
{
345+
$this->router->addControllers($controllers);
346+
}
347+
348+
/**
349+
* @param string $name
350+
* @param class-string|CommandInterface|null|Closure(FlagsParser, Output):void $handler
351+
* @param array{desc: string, aliases: array, options: array, arguments: array} $config config the command.
352+
*
353+
* @return Application
354+
*/
355+
public function command(string $name, string|Closure|CommandInterface $handler = null, array $config = []): static
356+
{
357+
$this->logf(Console::VERB_CRAZY, 'register alone command: %s', $name);
358+
$this->router->addCommand($name, $handler, $config);
359+
360+
return $this;
361+
}
362+
363+
/**
364+
* add command
365+
*
366+
* @param string $name
367+
* @param class-string|CommandInterface|null|Closure(FlagsParser, Output):void $handler
368+
* @param array{desc: string, aliases: array, options: array, arguments: array} $config config the command.
369+
*
370+
* @return Application
371+
* @see command()
372+
*/
373+
public function addCommand(string $name, string|Closure|CommandInterface $handler = null, array $config = []): static
374+
{
375+
return $this->command($name, $handler, $config);
376+
}
377+
378+
/**
379+
* @param array{string, mixed} $commands
380+
*/
381+
public function addCommands(array $commands): void
382+
{
383+
$this->router->addCommands($commands);
384+
}
385+
386+
/**
387+
* @param array{string, mixed} $commands
388+
*/
389+
public function commands(array $commands): void
390+
{
391+
$this->addCommands($commands);
392+
}
393+
394+
/**
395+
* auto register commands from a dir.
396+
*
397+
* ```php
398+
* $app->registerCommands('SwagPhp\Command', dirname(__DIR__) . '/src/Command');
399+
* ```
400+
*
401+
* @param string $namespace
402+
* @param string $basePath
403+
*
404+
* @return $this
405+
*/
406+
public function registerCommands(string $namespace, string $basePath): static
407+
{
408+
$this->debugf('register commands from the namespace: %s', $namespace);
409+
410+
$length = strlen($basePath) + 1;
411+
// $iterator = Helper::directoryIterator($basePath, $this->getFileFilter());
412+
$iter = Dir::getIterator($basePath, Dir::getPhpFileFilter());
413+
414+
foreach ($iter as $file) {
415+
$subPath = substr($file->getPathName(), $length, -4);
416+
$fullClass = $namespace . '\\' . str_replace('/', '\\', $subPath);
417+
$this->addCommand($fullClass);
418+
}
419+
420+
return $this;
421+
}
422+
423+
/**
424+
* auto register controllers from a dir.
425+
*
426+
* @param string $namespace
427+
* @param string $basePath
428+
*
429+
* @return $this
430+
*/
431+
public function registerGroups(string $namespace, string $basePath): self
432+
{
433+
$this->debugf('register groups from the namespace: %s', $namespace);
434+
435+
$length = strlen($basePath) + 1;
436+
// $iterator = Helper::directoryIterator($basePath, $this->getFileFilter());
437+
$iter = Dir::getIterator($basePath, Dir::getPhpFileFilter());
438+
439+
foreach ($iter as $file) {
440+
$subPath = substr($file->getPathName(), $length, -4);
441+
$fullClass = $namespace . '\\' . str_replace('/', '\\', $subPath);
442+
$this->addController($fullClass);
443+
}
444+
445+
return $this;
446+
}
442447
}

src/Handler/AbstractHandler.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,6 @@ public function __construct(Input $input = null, Output $output = null)
135135

136136
// init an flags object
137137
$this->flags = new SFlags();
138-
139138
$this->init();
140139
}
141140

@@ -296,6 +295,7 @@ protected function afterInitFlagsParser(FlagsParser $fs): void
296295
* @param array $args
297296
*
298297
* @return mixed
298+
* @throws Throwable
299299
*/
300300
public function run(array $args): mixed
301301
{
@@ -321,7 +321,7 @@ public function run(array $args): mixed
321321
if ($this->isDetached()) {
322322
ErrorHandler::new()->handle($e);
323323
} else {
324-
throw new RuntimeException('Run error - ' . $e->getMessage(), $e->getCode(), $e);
324+
throw $e;
325325
}
326326
}
327327

0 commit comments

Comments
 (0)