Skip to content

Commit dbfea82

Browse files
committed
some logic update, provide command tips
1 parent 1b23a24 commit dbfea82

File tree

4 files changed

+77
-30
lines changed

4 files changed

+77
-30
lines changed

src/Application.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ public function runCommand($name, $believable = false)
222222

223223
/**
224224
* @param string $name Controller name
225-
* @param string $action
225+
* @param string $action Command
226226
* @param bool $believable The `$name` is believable
227227
* @param bool $standAlone
228228
* @return mixed
@@ -253,7 +253,7 @@ public function runAction($name, $action, $believable = false, $standAlone = fal
253253
$object->setDelimiter($this->delimiter);
254254
$object->setStandAlone($standAlone);
255255

256-
return $object->setAction($action)->run();
256+
return $object->run($action);
257257
}
258258

259259
}

src/Base/AbstractCommand.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
namespace Inhere\Console\Base;
1010

11+
use Inhere\Console\Application;
1112
use Inhere\Console\IO\Input;
1213
use Inhere\Console\IO\InputDefinition;
1314
use Inhere\Console\IO\Output;
@@ -52,7 +53,7 @@ abstract class AbstractCommand implements CommandInterface
5253
'example' => true,
5354
];
5455

55-
/** @var ApplicationInterface */
56+
/** @var Application */
5657
protected $app;
5758

5859
/** @var InputDefinition|null */
@@ -110,9 +111,10 @@ protected function createDefinition()
110111

111112
/**
112113
* run command
114+
* @param string $command
113115
* @return int
114116
*/
115-
public function run()
117+
public function run($command = '')
116118
{
117119
// load input definition configure
118120
$this->configure();
@@ -312,6 +314,16 @@ protected function replaceAnnotationVars($str)
312314
return $map ? strtr($str, $map) : $str;
313315
}
314316

317+
/**
318+
* get All Command Methods for a group(controller)
319+
* @param \ReflectionClass|null $ref
320+
* @return \Generator
321+
*/
322+
protected function getAllCommandMethods(\ReflectionClass $ref = null)
323+
{
324+
// ...
325+
}
326+
315327
/**
316328
* show help by parse method annotation
317329
* @param string $method
@@ -324,7 +336,7 @@ protected function showHelpByMethodAnnotation($method, $action = null)
324336
$name = $this->input->getCommand();
325337

326338
if (!$ref->hasMethod($method)) {
327-
$this->write("The command [<info>$name</info>] don't exist in the class.");
339+
$this->write("The command [<info>$name</info>] don't exist in the group: " . static::getName());
328340

329341
return 0;
330342
}

src/Base/CommandInterface.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ interface CommandInterface
1818
{
1919
/**
2020
* run command
21+
* @param string $command
2122
* @return int
2223
*/
23-
public function run();
24+
public function run($command = '');
2425

2526
/**
2627
* @return InputDefinition

src/Controller.php

Lines changed: 58 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,19 @@ abstract class Controller extends AbstractCommand implements ControllerInterface
3939
/** @var bool */
4040
private $standAlone = false;
4141

42+
/**
43+
* @param string $command
44+
* @return int
45+
*/
46+
public function run($command = '')
47+
{
48+
if (!$this->action = trim($command)) {
49+
return $this->showHelp();
50+
}
51+
52+
return parent::run($command);
53+
}
54+
4255
/**
4356
* load command configure
4457
*/
@@ -76,7 +89,23 @@ protected function execute($input, $output)
7689
} else {
7790
$status = -1;
7891
$this->output->liteError("Sorry, the console controller command [$action] not exist!");
79-
$this->showCommandList();
92+
93+
// find similar command names by similar_text()
94+
$similares = [];
95+
96+
foreach ($this->getAllCommandMethods() as $cmd => $refM) {
97+
similar_text($action, $cmd, $percent);
98+
99+
if (45 <= (int)$percent) {
100+
$similares[] = $cmd;
101+
}
102+
}
103+
104+
if ($similares) {
105+
$this->write(sprintf('Maybe what you mean is: <info>%s</info>', implode(', ', $similares)));
106+
} else {
107+
$this->showCommandList();
108+
}
80109
}
81110

82111
return $status;
@@ -119,6 +148,7 @@ final public function helpCommand()
119148
$action = Helper::camelCase($action);
120149
$method = $this->actionSuffix ? $action . ucfirst($this->actionSuffix) : $action;
121150

151+
// show help info for a command.
122152
return $this->showHelpByMethodAnnotation($method, $action);
123153
}
124154

@@ -134,31 +164,12 @@ final protected function showCommandList()
134164
$classDes = Annotation::description($ref->getDocComment()) ?: 'No Description for the console controller';
135165
}
136166

137-
$suffix = $this->actionSuffix;
138-
$suffixLen = Helper::strLen($suffix);
139-
140167
$commands = [];
141-
foreach ($ref->getMethods() as $m) {
142-
$mName = $m->getName();
168+
foreach ($this->getAllCommandMethods($ref) as $cmd => $m) {
169+
$desc = Annotation::firstLine($m->getDocComment());
143170

144-
if ($m->isPublic() && substr($mName, -$suffixLen) === $suffix) {
145-
$desc = Annotation::firstLine($m->getDocComment());
146-
$length = strlen($this->actionSuffix);
147-
$cmd = '';
148-
149-
if ($length) {
150-
if (substr($mName, -$length) === $this->actionSuffix) {
151-
$cmd = substr($mName, 0, -$length);
152-
}
153-
154-
} else {
155-
$cmd = $mName;
156-
}
157-
158-
if ($cmd) {
159-
//$this->write(" <info>$cmd</info> $desc");
160-
$commands[$cmd] = $desc;
161-
}
171+
if ($cmd) {
172+
$commands[$cmd] = $desc;
162173
}
163174
}
164175

@@ -198,6 +209,29 @@ final protected function showCommandList()
198209
));
199210
}
200211

212+
/**
213+
* @param \ReflectionClass|null $ref
214+
* @return \Generator
215+
*/
216+
protected function getAllCommandMethods(\ReflectionClass $ref = null)
217+
{
218+
$ref = $ref ?: new \ReflectionObject($this);
219+
220+
$suffix = $this->actionSuffix;
221+
$suffixLen = Helper::strLen($suffix);
222+
223+
foreach ($ref->getMethods() as $m) {
224+
$mName = $m->getName();
225+
226+
if ($m->isPublic() && substr($mName, - $suffixLen) === $suffix) {
227+
// suffix is empty ?
228+
$cmd = $suffix ? substr($mName, 0, -$suffixLen) : $mName;
229+
230+
yield $cmd => $m;
231+
}
232+
}
233+
}
234+
201235
/**************************************************************************
202236
* getter/setter methods
203237
**************************************************************************/

0 commit comments

Comments
 (0)