Skip to content

Commit 7d486a1

Browse files
committed
first up
1 parent 9c86dd3 commit 7d486a1

File tree

9 files changed

+1144
-2
lines changed

9 files changed

+1144
-2
lines changed

‎.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/vendor/
2+
composer.lock
3+
tests/runtime
4+

‎README.md

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,54 @@
1-
# cli
2-
PhalApi 2.x 扩展类库 - CLI命令行,可用于开发命令行应用,基于GetOpt,主要作用是将命令参数进行解析和处理。
1+
##1、 CLI扩展类库
2+
此类库可用于开发命令行应用,基于GetOpt,主要作用是将命令参数进行解析和处理。
3+
4+
##2、安装
5+
[PhalApi-Library](http://git.oschina.net/dogstar/PhalApi-Library) 扩展库中下载获取 **CLI** 扩展包,如使用:
6+
```javascript
7+
git clone https://git.oschina.net/dogstar/PhalApi-Library.git
8+
```
9+
10+
然后把 **CLI** 目录复制到 **./PhalApi/Library/** 下,即:
11+
```javascript
12+
cp ./PhalApi-Library/CLI/ ./PhalApi/Library/ -R
13+
```
14+
15+
到此安装完毕!不需要配置。
16+
17+
##3、编写命令行入口文件
18+
参考原来的入口文件index.php,编写以下的CLI入口文件,保存到:./Public/demo/cli 文件:
19+
```
20+
#!/usr/bin/env php
21+
<?php
22+
require_once dirname(__FILE__) . '/../init.php';
23+
24+
//装载你的接口
25+
DI()->loader->addDirs('Demo');
26+
27+
$cli = new CLI_Lite();
28+
$cli->response();
29+
```
30+
31+
##4、运行和使用
32+
###(1) 正常运行
33+
默认接口服务使用```service```名称,缩写为```s```,如运行命令:
34+
```
35+
$ ./cli -s Default.Index --username dogstar
36+
{"ret":200,"data":{"title":"Default Api","content":"dogstar\u60a8\u597d\uff0c\u6b22\u8fce\u4f7f\u7528PhalApi\uff01","version":"1.3.5","time":1486291429},"msg":""}
37+
```
38+
39+
###(2) 获取帮助
40+
指定接口服务service后,即可使用 --help 参数以查看接口帮助信息,如:
41+
```
42+
$ ./cli -s User.GetBaseInfo --help
43+
Usage: ./cli [options] [operands]
44+
Options:
45+
-s, --service <arg> 接口服务
46+
--help
47+
--user_id <arg> 用户ID
48+
```
49+
50+
###(3) 异常情况
51+
异常时,将显示异常错误提示信息,以及帮助信息。
52+
53+
##5、接口开发
54+
接口开发与原来保持不变。注意:目前此扩展还在开发当中,尚未正式使用,有问题欢迎反馈、一起完善,谢谢!

‎composer.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "phalapi/cli",
3+
"description": "PhalApi 2.x 扩展类库 - CLI命令行,可用于开发命令行应用,基于GetOpt,主要作用是将命令参数进行解析和处理。",
4+
"keywords": [
5+
"phalapi", "phalapi-cli", "cli"
6+
],
7+
"license": "GPL-3.0+",
8+
"homepage": "https://www.phalapi.net/",
9+
"type": "library",
10+
"authors": [
11+
{
12+
"name": "dogstar huang",
13+
"email": "chanzonghuang@gmail.com",
14+
"homepage": "http://my.oschina.net/dogstar",
15+
"role": "Developer"
16+
}
17+
],
18+
"autoload": {
19+
"psr-4": {
20+
"PhalApi\\CLI\\": "src"
21+
}
22+
}
23+
}

‎src/Lite.php

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<?php
2+
namespace PhalApi\CLI;
3+
4+
/**
5+
* 用于开发命令行应用的扩展类库
6+
*
7+
* - Example
8+
```
9+
$cli = new PhalApi\CLI\Lite();
10+
$cli->response();
11+
```
12+
*
13+
* - Usage
14+
```
15+
./cli -s Site.Index --username dogstar
16+
```
17+
*
18+
* @author dogstar <chanzonghuang@gmail.com> 20170205
19+
*/
20+
21+
require_once dirname(__FILE__) . '/Ulrichsg/Getopt/Getopt.php';
22+
require_once dirname(__FILE__) . '/Ulrichsg/Getopt/Option.php';
23+
require_once dirname(__FILE__) . '/Ulrichsg/Getopt/Argument.php';
24+
require_once dirname(__FILE__) . '/Ulrichsg/Getopt/CommandLineParser.php';
25+
require_once dirname(__FILE__) . '/Ulrichsg/Getopt/OptionParser.php';
26+
27+
use Ulrichsg\Getopt\Getopt;
28+
use Ulrichsg\Getopt\Option;
29+
use Ulrichsg\Getopt\Argument;
30+
31+
use PhalApi\PhalApi;
32+
use PhalApi\Request;
33+
use PhalApi\ApiFactory;
34+
use PhalApi\Exception;
35+
36+
class Lite {
37+
38+
public function response() {
39+
// 解析获取service参数
40+
$serviceOpt = new Option('s', 'service', Getopt::REQUIRED_ARGUMENT);
41+
$serviceOpt->setDescription('接口服务');
42+
43+
$helpOpt = new Option('h', 'help');
44+
$helpOpt->setDescription('查看帮助信息');
45+
$getopt = new Getopt(array(
46+
$serviceOpt,
47+
$helpOpt
48+
));
49+
50+
$service = NULL;
51+
try {
52+
$getopt->parse();
53+
54+
$service = $getopt['service'];
55+
if ($service === NULL) {
56+
echo $getopt->getHelpText();
57+
echo "\n\nError: 缺少service参数'\n";
58+
exit(1);
59+
}
60+
} catch (UnexpectedValueException $e) {
61+
// just go ahead ...
62+
}
63+
64+
// 再转换处理 。。。
65+
try{
66+
// 获取接口实例
67+
$rules = array();
68+
try {
69+
\PhalApi\DI()->request = new Request(array('service' => $service));
70+
$api = ApiFactory::generateService(false);
71+
$rules = $api->getApiRules();
72+
} catch (Exception $ex){
73+
throw new UnexpectedValueException($ex->getMessage());
74+
}
75+
76+
// PhalApi接口参数转换为命令行参数
77+
$rule2opts = array();
78+
foreach ($rules as $ruleKey => $ruleItem) {
79+
$opt = new Option(null, $ruleItem['name'], !empty($ruleItem['require']) ? Getopt::REQUIRED_ARGUMENT : Getopt::OPTIONAL_ARGUMENT);
80+
81+
if (isset($ruleItem['default'])) {
82+
$opt->setArgument(new Argument($ruleItem['default']));
83+
}
84+
85+
if (isset($ruleItem['desc'])) {
86+
$opt->setDescription($ruleItem['desc']);
87+
}
88+
89+
$rule2opts[] = $opt;
90+
}
91+
92+
// 添加参数选项,提取命令行参数并重新注册请求
93+
$getopt->addOptions($rule2opts);
94+
95+
$getopt->parse();
96+
97+
if ($getopt['help']) {
98+
echo $getopt->getHelpText();
99+
exit(1);
100+
}
101+
102+
\PhalApi\DI()->request = new Request($getopt->getOptions());
103+
104+
// 转交PhalApi重新响应处理
105+
$api = new PhalApi();
106+
$rs = $api->response();
107+
$rs->output();
108+
} catch (UnexpectedValueException $e) {
109+
echo $getopt->getHelpText();
110+
echo "\n\nError: ".$e->getMessage()."\n";
111+
exit(1);
112+
}
113+
}
114+
}

‎src/Ulrichsg/Getopt/Argument.php

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
3+
namespace Ulrichsg\Getopt;
4+
5+
class Argument
6+
{
7+
/** @var string */
8+
private $default;
9+
/** @var callable */
10+
private $validation;
11+
/** @var string */
12+
private $name;
13+
14+
/**
15+
* Creates a new argument.
16+
*
17+
* @param scalar|null $default Default value or NULL
18+
* @param callable|null $validation a validation function (optional)
19+
* @throws \InvalidArgumentException
20+
*/
21+
public function __construct($default = null, $validation = null, $name = "arg")
22+
{
23+
if (!is_null($default)) {
24+
$this->setDefaultValue($default);
25+
}
26+
if (!is_null($validation)) {
27+
$this->setValidation($validation);
28+
}
29+
$this->name = $name;
30+
}
31+
32+
/**
33+
* Set the default value
34+
*
35+
* @param scalar $value
36+
* @return Argument this object (for chaining calls)
37+
* @throws \InvalidArgumentException
38+
*/
39+
public function setDefaultValue($value)
40+
{
41+
if (!is_scalar($value)) {
42+
throw new \InvalidArgumentException("Default value must be scalar");
43+
}
44+
$this->default = $value;
45+
return $this;
46+
}
47+
48+
/**
49+
* Set a validation function.
50+
* The function must take a string and return true if it is valid, false otherwise.
51+
*
52+
* @param callable $callable
53+
* @return Argument this object (for chaining calls)
54+
* @throws \InvalidArgumentException
55+
*/
56+
public function setValidation($callable)
57+
{
58+
if (!is_callable($callable)) {
59+
throw new \InvalidArgumentException("Validation must be a callable");
60+
}
61+
$this->validation = $callable;
62+
return $this;
63+
}
64+
65+
/**
66+
* Check if an argument validates according to the specification.
67+
*
68+
* @param string $arg
69+
* @return bool
70+
*/
71+
public function validates($arg)
72+
{
73+
return (bool)call_user_func($this->validation, $arg);
74+
}
75+
76+
/**
77+
* Check if the argument has a validation function
78+
*
79+
* @return bool
80+
*/
81+
public function hasValidation()
82+
{
83+
return isset($this->validation);
84+
}
85+
86+
/**
87+
* Check whether the argument has a default value
88+
*
89+
* @return boolean
90+
*/
91+
public function hasDefaultValue()
92+
{
93+
return !empty($this->default);
94+
}
95+
96+
/**
97+
* Retrieve the default value
98+
*
99+
* @return scalar|null
100+
*/
101+
public function getDefaultValue()
102+
{
103+
return $this->default;
104+
}
105+
106+
/**
107+
* Retrieve the argument name
108+
*
109+
* @return string
110+
*/
111+
public function getName()
112+
{
113+
return $this->name;
114+
}
115+
}

0 commit comments

Comments
 (0)
close