Skip to content

Commit 84ee2a7

Browse files
committed
some update ...
1 parent a03fdbc commit 84ee2a7

File tree

8 files changed

+354
-27
lines changed

8 files changed

+354
-27
lines changed

README.md

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -84,37 +84,52 @@ $app->run();
8484

8585
> `Independent Commands` 中的 demo 就是我们上面添加的命令
8686
87-
8887
## 添加命令
8988

9089
添加命令的方式有三种
9190

92-
- 如上所示,使用闭包可以快速的添加一个简单的命令
91+
### 使用闭包
92+
93+
如上所示,使用闭包可以快速的添加一个简单的命令
9394

9495
```php
9596
$app->command('demo', function (Input $in, Output $out) {
9697
$cmd = $in->getCommand();
9798

9899
$out->info('hello, this is a test command: ' . $cmd);
99-
});
100+
}, 'this is message for the command');
100101
```
101102

102-
- 通过继承 `Inhere\Console\Command` 添加独立命令
103+
### 继承 `Inhere\Console\Command`
104+
105+
通过继承 `Inhere\Console\Command` 添加独立命令
106+
107+
> 独立命令 - 只有一个命令可执行,跟 `symfony/console` 的 command 一样
103108
104109
```php
105110
use Inhere\Console\Command;
106111

107112
/**
108-
* Class Test
113+
* Class TestCommand
109114
* @package app\console\commands
110115
*/
111116
class TestCommand extends Command
112117
{
118+
// 命令名称
113119
protected static $name = 'test';
120+
// 命令描述
114121
protected static $description = 'this is a test independent command';
115122

123+
// 注释中的 @usage @arguments @options 在使用 帮助命令时,会被解析并显示出来
124+
116125
/**
117-
* execute
126+
* @usage usage message
127+
* @arguments
128+
* arg some message ...
129+
*
130+
* @options
131+
* -o, --opt some message ...
132+
*
118133
* @param Inhere\Console\IO\Input $input
119134
* @param Inhere\Console\IO\Output $output
120135
* @return int
@@ -126,9 +141,19 @@ class TestCommand extends Command
126141
}
127142
```
128143

129-
注册命令,在 `$app->run()` 之前通过 `$app->command('test', TestCommand::class)` 注册独立命令。
144+
- 注册命令
145+
146+
`$app->run()` 之前通过 `$app->command('test', TestCommand::class)` 注册独立命令。
147+
148+
```php
149+
$app->command(TestCommand::class);
150+
// OR 设置了命令名称,将会覆盖类里面设置的
151+
// $app->command('test1', TestCommand::class);
152+
```
153+
154+
### 继承 `Inhere\Console\Controller`
130155

131-
- 通过继承 `Inhere\Console\Controller` 添加一组命令(命令行的控制器类)
156+
通过继承 `Inhere\Console\Controller` 添加一组命令. 即是命令行的控制器
132157

133158
```php
134159
use Inhere\Console\Controller;
@@ -138,10 +163,11 @@ use Inhere\Console\Controller;
138163
*/
139164
class HomeController extends Controller
140165
{
166+
protected static $name = 'home';
141167
protected static $description = 'default command controller. there are some command usage examples';
142168

143169
/**
144-
* this is a command's description message
170+
* this is a command's description message <info>a color text</info>
145171
* the second line text
146172
* @usage usage message
147173
* @example example text one
@@ -154,13 +180,14 @@ class HomeController extends Controller
154180
}
155181
```
156182

157-
注册命令,在 `$app->run()` 之前通过 `$app->controller('home', HomeController::class)` 注册命令组。
183+
注册命令,在 `$app->run()` 之前通过 `$app->controller(HomeController::class)` 注册命令组。
158184

159-
说明:
185+
**说明:**
160186

161187
命令组(eg `HomeController`) 中的命令(eg: `indexCommand`)上注释是可被解析的。
162188

163-
- 当你使用 `php examples/app home -h` 时,可以查看到 `HomeController` 的所有命令信息
189+
- 支持的tag有 `@usage` `@arguments` `@options` `@example`
190+
- 当你使用 `php examples/app home -h` 时,可以查看到 `HomeController` 的所有命令描述注释信息
164191
- 当使用 `php examples/app home:index -h` 时,可以查看到关于 `HomeController::indexCommand` 更详细的信息。包括描述注释文本、`@usage``@example`
165192

166193
> 小提示:注释里面同样支持带颜色的文本输出 `eg: this is a command's description <info>message</info>`

src/Application.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
*/
1717
class Application extends AbstractApplication
1818
{
19-
/**********************************************************
19+
/****************************************************************************
2020
* register console controller/command
21-
**********************************************************/
21+
****************************************************************************/
2222

2323
/**
2424
* Register a app group command(by controller)
@@ -57,8 +57,9 @@ public function controller(string $name, string $class = null)
5757
}
5858

5959
/**
60-
* @see Application::controller()
6160
* {@inheritdoc}
61+
* @see Application::controller()
62+
* @throws \InvalidArgumentException
6263
*/
6364
public function addController(string $name, string $class = null)
6465
{
@@ -156,9 +157,9 @@ public function addGroup(string $name, string $controller = null)
156157
return $this->controller($name, $controller);
157158
}
158159

159-
/**********************************************************
160+
/****************************************************************************
160161
* dispatch and run console controller/command
161-
**********************************************************/
162+
****************************************************************************/
162163

163164
/**
164165
* @inheritdoc

src/Base/AbstractCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ public function validateInput()
289289
*/
290290
protected function annotationVars()
291291
{
292-
// e.g: `more info see {$name}/index`
292+
// e.g: `more info see {name}/index`
293293
return [
294294
'script' => $this->input->getScript(),
295295
'command' => $this->input->getCommand(),

src/Components/AutoLoader.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: Inhere
5+
* Date: 2017/11/25 0025
6+
* Time: 11:31
7+
*/
8+
9+
namespace Inhere\Console\Components;
10+
11+
/**
12+
* Class AutoLoader - for auto load commands and controllers
13+
* @package Inhere\Console\Components
14+
*/
15+
class AutoLoader
16+
{
17+
/** @var string */
18+
protected $commandsNamespace;
19+
20+
/** @var string */
21+
protected $controllersNamespace;
22+
23+
public function scanCommands()
24+
{
25+
return [];
26+
}
27+
28+
public function scanControllers()
29+
{
30+
return [];
31+
}
32+
33+
/**
34+
* @return string
35+
*/
36+
public function getCommandsNamespace()
37+
{
38+
return $this->commandsNamespace;
39+
}
40+
41+
/**
42+
* @param string $commandsNamespace
43+
*/
44+
public function setCommandsNamespace(string $commandsNamespace)
45+
{
46+
$this->commandsNamespace = $commandsNamespace;
47+
}
48+
49+
/**
50+
* @return string
51+
*/
52+
public function getControllersNamespace()
53+
{
54+
return $this->controllersNamespace;
55+
}
56+
57+
/**
58+
* @param string $controllersNamespace
59+
*/
60+
public function setControllersNamespace(string $controllersNamespace)
61+
{
62+
$this->controllersNamespace = $controllersNamespace;
63+
}
64+
}

0 commit comments

Comments
 (0)