Skip to content

Commit f837631

Browse files
committed
update some logic for dispatch. add more logs
1 parent c1515b2 commit f837631

File tree

4 files changed

+28
-11
lines changed

4 files changed

+28
-11
lines changed

src/AbstractApplication.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ protected function init(): void
149149
}
150150

151151
$this->registerErrorHandle();
152+
153+
$this->logf(Console::VERB_DEBUG, 'console application init completed');
152154
}
153155

154156
/**
@@ -209,6 +211,8 @@ public function run(bool $exit = true)
209211
{
210212
$command = trim($this->input->getCommand(), $this->delimiter);
211213

214+
$this->logf(Console::VERB_DEBUG, 'begin run the application, command is: %s', $command);
215+
212216
try {
213217
$this->prepareRun();
214218

@@ -537,7 +541,7 @@ public function getConfig(): array
537541
/**
538542
* Get config param value
539543
*
540-
* @param null|string $name
544+
* @param string $name
541545
* @param null|string $default
542546
*
543547
* @return array|string

src/Application.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@
2121
use function is_object;
2222
use function is_string;
2323
use function method_exists;
24+
use function str_replace;
2425
use function strlen;
2526
use function strpos;
2627
use function substr;
28+
use function trim;
2729

2830
/**
2931
* Class App
@@ -255,25 +257,33 @@ protected function getFileFilter(): callable
255257
/**
256258
* @inheritdoc
257259
* @throws ReflectionException
258-
* @throws InvalidArgumentException
259260
*/
260261
public function dispatch(string $name, bool $detachedRun = false)
261262
{
262-
$this->logf(Console::VERB_DEBUG, 'begin dispatch command: %s', $name);
263+
if (!$name = trim($name)) {
264+
throw new InvalidArgumentException('cannot dispatch an empty command');
265+
}
266+
267+
$cmdId = $name;
268+
$this->logf(Console::VERB_DEBUG, 'begin dispatch the input command: %s', $name);
269+
270+
// format is: `group action`
271+
if (strpos($name, ' ') > 0) {
272+
$cmdId = str_replace(' ', $this->delimiter, $name);
273+
}
263274

264275
// match handler by input name
265-
$info = $this->router->match($name);
276+
$info = $this->router->match($cmdId);
266277

267278
// command not found
268279
if (!$info) {
269-
if (true === $this->fire(self::ON_NOT_FOUND, $name, $this)) {
280+
if (true === $this->fire(self::ON_NOT_FOUND, $cmdId, $this)) {
270281
$this->logf(Console::VERB_DEBUG, 'not found handle by user, command: %s', $name);
271282
return 0;
272283
}
273284

274-
$this->output->error("The command '{$name}' is not exists!");
275-
276285
$commands = $this->router->getAllNames();
286+
$this->output->error("The command '{$name}' is not exists!");
277287

278288
// find similar command names by similar_text()
279289
if ($similar = Helper::findSimilar($name, $commands)) {
@@ -287,9 +297,8 @@ public function dispatch(string $name, bool $detachedRun = false)
287297
return 2;
288298
}
289299

290-
$cmdOptions = $info['options'];
291-
292300
// save command ID
301+
$cmdOptions = $info['options'];
293302
$this->input->setCommandId($info['cmdId']);
294303

295304
// is command

src/Console.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,9 @@ private static function formatBacktrace(array $traces, int $index): string
171171
if (isset($traces[$index+1])) {
172172
$tInfo = $traces[$index];
173173
$prev = $traces[$index+1];
174+
$type = $prev['type'];
174175

175-
$position = sprintf('%s.%s:%d', $prev['class'], $prev['function'] ?? 'UNKNOWN', $tInfo['line']);
176+
$position = sprintf('%s%s%s(),L%d', $prev['class'], $type, $prev['function'] ?? 'UNKNOWN', $tInfo['line']);
176177
}
177178

178179
return ColorTag::add($position, 'green');

src/Contract/ApplicationInterface.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ public function run(bool $exit = true);
3939
/**
4040
* Dispatch input command, exec found command handler.
4141
*
42-
* @param string $name Inputted command name
42+
* @param string $name Inputted command name. allow:
43+
* - 'command'
44+
* - 'group:action'
45+
* - 'group action'
4346
* @param bool $detachedRun Use for an group commands execution alone
4447
*
4548
* @return int|mixed

0 commit comments

Comments
 (0)