Skip to content

Commit 4d46499

Browse files
committed
update, some modify, more annotation tag support
1 parent a15bec5 commit 4d46499

16 files changed

+237
-161
lines changed

Diff for: examples/HomeController.php

+43-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace inhere\console\examples;
44

55
use inhere\console\Controller;
6+
use inhere\console\utils\AnsiCode;
67
use inhere\console\utils\Download;
78
use inhere\console\utils\Show;
89
use inhere\console\utils\Interact;
@@ -21,6 +22,12 @@ class HomeController extends Controller
2122
* this is a command's description message
2223
* the second line text
2324
* @usage usage message
25+
* @arguments
26+
* arg1 argument description 1
27+
* arg2 argument description 2
28+
* @options
29+
* --long,-s option description 1
30+
* --opt option description 2
2431
* @example example text one
2532
* the second line example
2633
*/
@@ -198,7 +205,7 @@ public function downCommand()
198205
$url = $this->input->getArg('url');
199206

200207
if (!$url) {
201-
\inhere\console\utils\Show::error('Please input you want to downloaded file url, use: url=[url]', 1);
208+
Show::error('Please input you want to downloaded file url, use: url=[url]', 1);
202209
}
203210

204211
$saveAs = $this->input->getArg('saveAs');
@@ -211,7 +218,7 @@ public function downCommand()
211218
$goon = Interact::confirm("Now, will download $url to $saveAs, go on");
212219

213220
if (!$goon) {
214-
\inhere\console\utils\Show::notice('Quit download, Bye!');
221+
Show::notice('Quit download, Bye!');
215222

216223
return 0;
217224
}
@@ -222,4 +229,38 @@ public function downCommand()
222229

223230
return 0;
224231
}
232+
233+
public function cursorCommand()
234+
{
235+
$this->write('hello, this in ' . __METHOD__);
236+
237+
// $this->output->panel($_SERVER, 'Server information', '');
238+
239+
$this->write('this is a message text.', false);
240+
241+
sleep(1);
242+
AnsiCode::make()->cursor(AnsiCode::CURSOR_BACKWARD, 6);
243+
244+
sleep(1);
245+
AnsiCode::make()->cursor(AnsiCode::CURSOR_FORWARD, 3);
246+
247+
sleep(1);
248+
AnsiCode::make()->cursor(AnsiCode::CURSOR_BACKWARD, 2);
249+
250+
sleep(2);
251+
252+
AnsiCode::make()->screen(AnsiCode::CLEAR_LINE, 3);
253+
254+
$this->write('after 2s scroll down 3 row.');
255+
256+
sleep(2);
257+
258+
AnsiCode::make()->screen(AnsiCode::SCROLL_DOWN, 3);
259+
260+
$this->write('after 3s clear screen.');
261+
262+
sleep(3);
263+
264+
AnsiCode::make()->screen(AnsiCode::CLEAR);
265+
}
225266
}

Diff for: examples/TestCommand.php

+2-31
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
namespace inhere\console\examples;
1010

1111
use inhere\console\Command;
12-
use inhere\console\utils\AnsiCode;
1312

1413
/**
1514
* Class Test
@@ -18,39 +17,11 @@
1817
class TestCommand extends Command
1918
{
2019
/**
21-
* {@inheritdoc}
20+
* test text
21+
* @usage {$name} test message
2222
*/
2323
public function execute($input, $output)
2424
{
2525
$output->write('hello, this in ' . __METHOD__);
26-
27-
// $this->output->panel($_SERVER, 'Server information', '');
28-
29-
$this->write('this is a message text.', false);
30-
31-
sleep(1);
32-
AnsiCode::make()->cursor(AnsiCode::CURSOR_BACKWARD, 6);
33-
34-
sleep(1);
35-
AnsiCode::make()->cursor(AnsiCode::CURSOR_FORWARD, 3);
36-
37-
sleep(1);
38-
AnsiCode::make()->cursor(AnsiCode::CURSOR_BACKWARD, 2);
39-
40-
sleep(2);
41-
42-
AnsiCode::make()->screen(AnsiCode::CLEAR_LINE, 3);
43-
44-
$this->write('after 2s scroll down 3 row.');
45-
46-
sleep(2);
47-
48-
AnsiCode::make()->screen(AnsiCode::SCROLL_DOWN, 3);
49-
50-
$this->write('after 3s clear screen.');
51-
52-
sleep(3);
53-
54-
AnsiCode::make()->screen(AnsiCode::CLEAR);
5526
}
5627
}

Diff for: src/AbstractApp.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
use inhere\console\io\Input;
1212
use inhere\console\io\Output;
13-
use inhere\console\utils\TraitInputOutput;
13+
use inhere\console\traits\TraitInputOutput;
1414

1515
/**
1616
* Class AbstractApp

Diff for: src/AbstractCommand.php

+77-4
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88

99
namespace inhere\console;
1010

11+
use inhere\console\helpers\Annotation;
1112
use inhere\console\io\Input;
1213
use inhere\console\io\Output;
13-
use inhere\console\utils\TraitInputOutput;
14-
use inhere\console\utils\TraitInteract;
14+
use inhere\console\traits\TraitInputOutput;
15+
use inhere\console\traits\TraitInteract;
1516

1617
/**
1718
* Class AbstractCommand
@@ -26,6 +27,9 @@ abstract class AbstractCommand
2627
// please use the const setting current controller/command description
2728
const DESCRIPTION = '';
2829

30+
// name -> {$name}
31+
const ANNOTATION_VAR = '{$%s}';
32+
2933
/**
3034
* TODO ...
3135
* command description message
@@ -41,10 +45,17 @@ abstract class AbstractCommand
4145
public static $name = '';
4246

4347
/**
44-
* allow display message tags in the command
48+
* Allow display message tags in the command annotation
4549
* @var array
4650
*/
47-
protected static $allowTags = ['description', 'usage', 'example'];
51+
protected static $allowTags = [
52+
// tag name => multi line align
53+
'description' => false,
54+
'usage' => false,
55+
'arguments' => true,
56+
'options' => true,
57+
'example' => true,
58+
];
4859

4960
/**
5061
* Command constructor.
@@ -57,16 +68,78 @@ public function __construct(Input $input, Output $output)
5768
$this->output = $output;
5869
}
5970

71+
/**
72+
* @param string $arg
73+
*/
6074
abstract public function run($arg = '');
6175

76+
/**
77+
* @param string $action
78+
*/
6279
protected function beforeRun($action)
6380
{
6481
}
6582

83+
/**
84+
* @param string $action
85+
*/
6686
protected function afterRun($action)
6787
{
6888
}
6989

90+
/**
91+
* 为命令注解提供可解析解析变量. 可以在命令的注释中使用
92+
* @return array
93+
*/
94+
protected function annotationVars()
95+
{
96+
// e.g: `more info see {$name}/index`
97+
return [
98+
'command' => $this->input->getCommand(),
99+
'name' => static::$name,
100+
];
101+
}
102+
103+
/**
104+
* show help by parse method annotation
105+
* @param string $method
106+
* @param null|string $action
107+
* @return int
108+
*/
109+
protected function showHelpByMethodAnnotation($method, $action = null)
110+
{
111+
$ref = new \ReflectionClass($this);
112+
$cName = lcfirst(self::getName() ?: $ref->getShortName());
113+
114+
if (!$ref->hasMethod($method) || !$ref->getMethod($method)->isPublic()) {
115+
$name = $action ? "$cName/$action" : $cName;
116+
$this->write("Command [<info>$name</info>] don't exist or don't allow access in the class.");
117+
118+
return 0;
119+
}
120+
121+
$m = $ref->getMethod($method);
122+
$tags = Annotation::tagList($m->getDocComment());
123+
124+
foreach ($tags as $tag => $msg) {
125+
if (!is_string($msg)) {
126+
continue;
127+
}
128+
129+
if (isset(self::$allowTags[$tag])) {
130+
// need multi align
131+
if (self::$allowTags[$tag]) {
132+
$msg = implode("\n ", array_filter(explode("\n", $msg), 'trim'));
133+
}
134+
135+
$tag = ucfirst($tag);
136+
$this->write("<comment>$tag:</comment>\n $msg\n");
137+
}
138+
}
139+
140+
return 0;
141+
}
142+
70143
/**
71144
* handle action/command runtime exception
72145
*

Diff for: src/Command.php

+3-13
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ abstract class Command extends AbstractCommand
4848
*/
4949
public function run($name = '')
5050
{
51-
$this->setName($name);
51+
self::setName($name);
5252

5353
if ($this->input->sameOpt(['h','help'])) {
5454
return $this->showHelp();
@@ -61,7 +61,7 @@ public function run($name = '')
6161
$status = $this->execute($this->input, $this->output);
6262
$this->afterRun($name);
6363

64-
} catch (\Exception $e) {
64+
} catch (\Throwable $e) {
6565
$this->handleRuntimeException($e);
6666
}
6767

@@ -95,16 +95,6 @@ protected function configure()
9595
*/
9696
public function showHelp()
9797
{
98-
$configure = $this->configure();
99-
100-
if (!$configure) {
101-
return __LINE__;
102-
}
103-
104-
$configure['description'] = static::DESCRIPTION;
105-
106-
$this->output->helpPanel($configure, false);
107-
108-
return 0;
98+
return $this->showHelpByMethodAnnotation('execute');
10999
}
110100
}

0 commit comments

Comments
 (0)