MethodService.class.php
6.53 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<?php
/**
* MethodService.class.php
* $author$
*/
namespace Common\Service;
use Common\Common\ApiDoc;
use Common\Common\FileSystem;
class MethodService extends AbstractService
{
protected $_types = array();
/**
* 构造方法
*/
public function __construct()
{
$this->_types = array("boolean", "integer", "double", "float", "string", "array", "object");
parent::__construct();
}
/**
* 清除类方法缓存
* @param string $dir 目录
* @return array|bool
*/
public function clearMethodCache($dir)
{
// 如果文件目录为空, 则不显示接口
if (empty($dir)) {
return array();
}
$root = dirname(APP_PATH);
$cacheDir = str_replace($root, '', get_sitedir());
$files = FileSystem::instance()->scanFile(substr($cacheDir, 1, -1), 'php', 1);
$filename = $this->_getCacheFileName($dir);
foreach ($files as $_file) {
if (!preg_match('/' . $filename . '(_|\.)/i', $_file)) {
continue;
}
@unlink($root . '/' . $_file);
}
return true;
}
/**
* 获取指定目录的接口列表
* @param string $dir 当前文件/目录
* @return array
*/
public function listMethodByDir($dir)
{
// 如果文件目录为空, 则不显示接口
if (empty($dir)) {
return array();
}
$methods = array();
$root = dirname(APP_PATH);
$cacheDir = str_replace($root, '', get_sitedir());
$files = FileSystem::instance()->scanFile(substr($cacheDir, 1, -1), 'php', 1);
$filename = $this->_getCacheFileName($dir);
foreach ($files as $_file) {
if (!preg_match('/' . $filename . '(_|\.)/i', $_file)) {
continue;
}
$fileMethods = include $root . '/' . $_file;
$fileMethods = array_values($fileMethods);
if (empty($fileMethods)) {
continue;
}
$methods = array_merge($methods, $fileMethods);
}
return $methods;
}
/**
* 获取类中的方法数据
* @param string $class 类名
* @param string $file 文件路径
* @param array $fileMethods 类方法列表
* @return mixed
*/
public function getMethodData($class, $file, $fileMethods = array())
{
// 获取类中所有方法
$methods = get_class_methods($class);
$apiMethods = array();
if (empty($methods)) {
return $apiMethods;
}
// 遍历所有方法
foreach ($methods as $_method) {
$rflMethod = new \Reflectionmethod($class, $_method);
if (!$rflMethod->isPublic() || strpos($_method, '__') === 0 || in_array($_method, array('getRules', 'before_action'))) {
continue;
}
$title = '//请检测函数注释';
$desc = '//请使用 @desc 注释';
$docComment = $rflMethod->getDocComment(); // 获取评论
$comments = ApiDoc::instance()->parseComment($docComment);
$service = $class . '.' . $_method;
$apiMethods[$service] = array(
'service' => $this->file2api($file, $_method),
'title' => empty($comments['description']) ? $title : $comments['description'],
'desc' => empty($comments['desc']) ? $desc : $comments['desc'],
'file' => $file,
'method' => $service,
'shortMethod' => substr($class, strrpos($class, '_') + 1) . '.' . $_method
);
}
$cacheFile = get_sitedir() . $this->_getCacheFileName($file);
$content = file_get_contents($cacheFile);
if (!empty($fileMethods)) {
$content = str_replace("\n\nreturn " . var_export($fileMethods, true) . ";", '', $content);
}
$content .= "\n\nreturn " . var_export($apiMethods, true) . ";";
file_put_contents($cacheFile, $content);
return $apiMethods;
}
/**
* 文件路径和方法名转换成api地址
* @param string $file 文件名称
* @param string $method 方法名称
* @return string
*/
public function file2api($file, $method)
{
static $file2api = array();
if (!isset($file2api[$file])) {
$file2api[$file] = str_replace(array('Controller.class.php', '/Controller'), '', $file);
}
$method = preg_replace('/_(get|post|delete|put)$/i', '', $method);
return $file2api[$file] . ('index' == strtolower($method) ? '' : '/' . $method);
}
/**
* 获取数组的维度
* @param array $array 数组
* @return int
*/
protected function _array_depth($array)
{
if (!is_array($array)) return 0;
$max_depth = 1;
foreach ($array as $value) {
if (is_array($value)) {
$depth = $this->_array_depth($value) + 1;
if ($depth > $max_depth) {
$max_depth = $depth;
}
}
}
return $max_depth;
}
/**
* 生成文件
* @param string $file 文件名
* @return array
*/
public function makeFile($file = '')
{
if (!preg_match('/([0-9a-z\_\-]\/(api|apicp|open|rpc)\/)/i', $file)) {
E('_ERR_NOT_API_FILE');
return false;
}
// 步骤:分析原来的类文件,将继承去掉并获取类名
$path = FileSystem::instance()->getRealPath($file);
$phpContent = file_get_contents($path);
if (empty($phpContent)) {
die('empty:' . $path);
}
$filename = $this->_getCacheFileName($file);
$className = preg_replace('/\.(.*?)php$/i', '', $filename);
// 生成新文件
$newFileName = get_sitedir() . $filename;
$phpContent = ApiDoc::instance()->extractClass($className, $phpContent);
if (file_exists($newFileName)) {
unlink($newFileName) or die ('删除文件:' . $newFileName . '失败');
}
file_put_contents($newFileName, $phpContent) or die ('写入文件:' . $newFileName . '失败');
return array(
'fileName' => $newFileName,
'className' => $className
);
}
/**
* 生成缓存文件路径
* @param string $file 文件相对路径
* @return mixed
*/
protected function _getCacheFileName($file)
{
return str_replace('/', '_', FileSystem::instance()->amendPath($file));
}
}