Skip to content

Commit acc87da

Browse files
author
dogstar
committed
提供:扩展接口命令列表、扩展公共命令参数
1 parent d3f5410 commit acc87da

2 files changed

Lines changed: 115 additions & 2 deletions

File tree

README.md

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,9 @@ Service: App.User_User.Register
103103
![20221208-174039](https://user-images.githubusercontent.com/12585518/206418256-59df5a90-8707-465f-b93d-f7e4db09d938.png)
104104

105105

106-
## 扩展帮助说明
106+
## 扩展
107+
108+
### 扩展帮助说明
107109

108110
如果需要定制你的命令脚本的帮助说明,可以重载```PhalApi\CLI\Lite::getHelpText($text)```方法。例如,修改```./bin/phalapi-cli```脚本,改为:
109111

@@ -128,6 +130,7 @@ $cli->response();
128130

129131
```
130132

133+
131134
执行后效果是:
132135
```bash
133136
$ php ./bin/phalapi-cli
@@ -144,6 +147,70 @@ Service:
144147
缺少service参数,请使用 -s 或 --service 指定需要调用的API接口
145148
```
146149

150+
### 扩展接口命令列表
151+
152+
可以重载扩展 ```PhalApi\CLI\Lite::getServiceList()```方法。返回一个数组,在里面配置:
153+
```
154+
array(
155+
编号 => array('service接口服务名称', '功能说明'),
156+
)
157+
```
158+
159+
例如,
160+
```php
161+
class MyCLI extends PhalApi\CLI\Lite {
162+
163+
// 提供接口列表,service -> 接口功能说明
164+
protected function getServiceList() {
165+
return array(
166+
1 => ['App.Hello.World', '演示接口'],
167+
);
168+
}
169+
}
170+
171+
```
172+
173+
运行效果是:
174+
```bash
175+
$ ./bin/phalapi-cli
176+
Usage: ./bin/phalapi-cli [options] [operands]
177+
178+
Options:
179+
-s, --service <arg> 接口服务
180+
-h, --help 查看帮助信息
181+
182+
183+
--- 自定义的帮助说明 ---
184+
185+
Service:
186+
1) App.Hello.World 演示接口
187+
188+
缺少service参数,请使用 -s 或 --service 指定需要调用的API接口。
189+
```
190+
191+
然后,可以使用快速编号执行对应的接口命令,如:
192+
```bash
193+
$ ./bin/phalapi-cli -s 1
194+
195+
Service: App.Hello.World
196+
{
197+
"ret": 200,
198+
"data": {
199+
"content": "Hello World!"
200+
},
201+
"msg": ""
202+
}
203+
```
204+
205+
### 扩展公共命令参数
206+
207+
可以加工处理以下方法:
208+
```
209+
// 完成命令行参数获取后的操作,方便追加公共参数
210+
protected function afterGetOptions($options) {
211+
return $options;
212+
}
213+
```
147214

148215
## 参考和依赖
149216

src/Lite.php

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ public function response() {
5050
$getOpt->process();
5151

5252
$service = $getOpt['service'];
53+
54+
// 转编号
55+
$serviceList = $this->getServiceList();
56+
if ($service !== NULL && isset($serviceList[$service])) {
57+
$service = $serviceList[$service][0];
58+
}
5359
} catch (\Exception $ex) {
5460
// 后续统一处理
5561
}
@@ -132,7 +138,14 @@ public function response() {
132138
}
133139

134140

135-
\PhalApi\DI()->request = new Request($getOpt->getOptions());
141+
$options = $getOpt->getOptions();
142+
// 同步转编号后的service
143+
$options['s'] = $options['service'] = $service;
144+
145+
$options = $this->afterGetOptions($options);
146+
147+
// 构建全部命令行参数
148+
\PhalApi\DI()->request = new Request($options);
136149

137150
// 转交PhalApi重新响应处理
138151
$api = new PhalApi();
@@ -144,11 +157,44 @@ public function response() {
144157
} catch (\Exception $ex) {
145158
echo $this->getHelpText($getOpt->getHelpText());
146159
echo PHP_EOL . $this->colorfulString('Service: ' . $service, 'NOTE');
160+
if ($service === NULL) {
161+
echo PHP_EOL . $this->getServiceListHelpText();
162+
}
147163
echo PHP_EOL . $this->colorfulString($ex->getMessage(), 'FAILURE') . PHP_EOL . PHP_EOL;
148164
exit(1);
149165
}
150166
}
151167

168+
// 完成命令行参数获取后的操作,方便追加公共参数
169+
protected function afterGetOptions($options) {
170+
return $options;
171+
}
172+
173+
// 提供接口列表,service -> 接口功能说明
174+
protected function getServiceList() {
175+
return array(
176+
// 1 => ['App.Hello.World', '演示接口'],
177+
);
178+
}
179+
180+
// 提示输出
181+
protected function getServiceListHelpText() {
182+
$list = $this->getServiceList();
183+
184+
$topLen = 20;
185+
foreach ($list as $pos => $it) {
186+
$topLen = max(strlen($it[0]), $topLen);
187+
}
188+
$topLen += 2;
189+
190+
$text = '';
191+
foreach ($list as $pos => $it) {
192+
$text .= $this->colorfulString($pos . ') ', 'NOTE') . $this->colorfulString(sprintf(" %-{$topLen}s", $it[0]), 'NOTE') . $it[1] . PHP_EOL;
193+
}
194+
195+
return $text;
196+
}
197+
152198
// 自定义帮助说明
153199
protected function getHelpText($text) {
154200
return $text;

0 commit comments

Comments
 (0)