-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.php
More file actions
138 lines (112 loc) · 3.56 KB
/
Copy pathcli.php
File metadata and controls
138 lines (112 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
/**
* Created by PhpStorm.
* User: alex
* Date: 18-2-5
* Time: 上午10:18
*/
require_once("base.php");
function load_command($func){
if(function_exists($func)){
$ref = new ReflectionFunction($func);
return str_replace("\n", ";", preg_replace("[\*\n|\/|\*|\ ]","",$ref->getDocComment()));
}else{
return "No Function Existed";
}
}
function get_cli_functions($files){
$res = json_encode(array_map(function($i){
if(is_array($i)){
return get_cli_functions($i);
}else{
if(strpos($i, ".php") <=0 ){
return [];
}else if(preg_match_all("/cli_\w+/", file_get_contents($i), $matches)){
@$file_name = array_pop(explode("/",$i));
@$identifier = array_shift(explode(".",$file_name));
$funcs = array_map(
function($func) use ($identifier){return str_replace("cli", $identifier,$func);}, $matches[0]);
return $funcs;
}
}
}, $files));
$reg = preg_match_all("/[a-z]+?_\w+/", $res, $matches);
return $matches[0];
}
$cmd = load_files(COMMANDS);
function load_functions($argv = [], $cmd=[]){
$COMMAND_PROMPT= [
"balance"=>"余额相关函数,计算收入等",
"crawler"=>"爬虫函数,用于抓取/处理数据",
"general"=>"杂项",
"cron"=>"定时脚本相关",
"ssh"=>"构造SSH隧道,实现免密登陆等",
"text"=>"文字处理相关",
"practise"=>"测试用"
];
$func_list = array_map(function($i){return explode("_", $i);},get_cli_functions($cmd));
$matches = [];
$arguments = $argv;
$arguments[0] = "cli";
$find_flag = $i = 0;
$total = [];
$preg="";
for($i=0;$i< count($arguments); $i++){
$col = array_column($func_list, $i);
$new_func = $total = [];
foreach($col as $k=>$v){
if($i == 0){
$new_func[]= $k;
}
else if(preg_match("/^".$arguments[$i]."[a-zA-Z0-9]*$/", $v)){
$new_func[]= $k;
}
}
$preg = "/".$arguments[$i]."\w*/";
foreach($new_func as $k){
$total[] = $func_list[$k];
}
if(count($total) > 1){
$find_flag = 1;
$func_list = $total;
continue;
}
if(count($total) == 1){
$find_flag = 1;
$func_list = $total;
break;
}else{
$i-=1;
break;
}
}
if($find_flag == 1 && count($func_list)==1) {
$ar = array_splice($arguments, $i+1);
$func_list[0][0] = "cli";
call_user_func(implode("_", $func_list[0]), $ar);
}else if($find_flag == 1){
$func_type_list = [];
foreach ($func_list as $f){
$func_type_list[$f[0]][]= $f;
}
$pad_string = str_pad("-", 80, "-");
exec("clear");
printf("%s\n", $pad_string,$pad_string,$pad_string);
foreach($func_type_list as $key=>$value){
printf("%s\t%s\n", str_pad("", 30, "-", STR_PAD_LEFT), $COMMAND_PROMPT[$key]."|".$key);
foreach($value as $f){
$f[0] = "cli";
$func_info = implode("_", $f);
printf("%s\t%s\n", str_pad($func_info, 30), str_pad(load_command($func_info), 30));
}
}
printf("%s\n", $pad_string,$pad_string,$pad_string);
}else{
echo "Not found\n";
}
}
try{
load_functions($argv, $cmd);
}catch (Exception $e){
print($e->getMessage());
}