colly_wyx
2018-05-18 50a53f7db63c5729b0ddbc93367117cf35ccf03c
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
<?php
defined('D_S') || define('D_S', DIRECTORY_SEPARATOR);
 
class PhalApi_Helper_ApiList extends PhalApi_Helper_ApiOnline {
 
    public function render($apiDirName, $libraryPaths) {
        // 处理项目
        DI()->loader->addDirs($apiDirName);
        $files = listDir(API_ROOT . D_S . $apiDirName. D_S . 'Api');
 
        // 追加处理扩展类库
        foreach ($libraryPaths as $aPath) {
            $toAddDir = str_replace('/', D_S, $aPath);
            DI()->loader->addDirs($toAddDir);
 
            $toListDir = API_ROOT . D_S . $toAddDir . D_S . 'Api';
            $aLibFiles = listDir($toListDir);
 
            $files = array_merge($files, $aLibFiles);
        }
 
        // 待排除的方法
        $allPhalApiApiMethods = get_class_methods('PhalApi_Api');
 
        // 扫描接口文件
        $allApiS = array();
        $errorMessage = '';
 
        foreach ($files as $value) {
            $value    = realpath($value);
            $subValue = substr($value, strpos($value, D_S . 'Api' . D_S) + 1);
            //支持多层嵌套,不限级
            $arr                = explode(D_S, $subValue);
            $subValue           = implode(D_S, $arr);
            $apiServer          = str_replace(array(D_S, '.php'), array('_', ''), $subValue);
            $apiServerShortName = substr($apiServer, 4);
 
            if (!class_exists($apiServer)) {
                continue;
            }
 
            // 检测文件路径的合法性
            if (ucfirst(substr($apiServer, 4)) != substr($apiServer, 4)) {
                $errorMessage .= $apiServer . ' 类文件首字母必须大写!<br/>';
            }
 
 
            //  左菜单的标题
            $ref        = new ReflectionClass($apiServer);
            $title      = "//请检测接口服务注释($apiServer)";
            $desc       = '//请使用@desc 注释';
            $docComment = $ref->getDocComment();
            if ($docComment !== false) {
                $docCommentArr = explode("\n", $docComment);
                $comment       = trim($docCommentArr[1]);
                $title         = trim(substr($comment, strpos($comment, '*') + 1));
                foreach ($docCommentArr as $comment) {
                    $pos = stripos($comment, '@desc');
                    if ($pos !== false) {
                        $desc = substr($comment, $pos + 5);
                    }
                }
            }
            $allApiS[$apiServerShortName]['title'] = $title;
            $allApiS[$apiServerShortName]['desc']  = $desc;
 
            $method = array_diff(get_class_methods($apiServer), $allPhalApiApiMethods);
            sort($method);
            foreach ($method as $mValue) {
                $rMethod = new Reflectionmethod($apiServer, $mValue);
                if (!$rMethod->isPublic() || strpos($mValue, '__') === 0) {
                    continue;
                }
 
                $title      = '//请检测函数注释';
                $desc       = '//请使用@desc 注释';
                $docComment = $rMethod->getDocComment();
                if ($docComment !== false) {
                    $docCommentArr = explode("\n", $docComment);
                    $comment       = trim($docCommentArr[1]);
                    $title         = trim(substr($comment, strpos($comment, '*') + 1));
 
                    foreach ($docCommentArr as $comment) {
                        $pos = stripos($comment, '@desc');
                        if ($pos !== false) {
                            $desc = substr($comment, $pos + 5);
                        }
                    }
                }
                $service                                           = $apiServerShortName . '.' . ucfirst($mValue);
                $allApiS[$apiServerShortName]['methods'][$service] = array(
                    'service' => $service,
                    'title'   => $title,
                    'desc'    => $desc,
                );
            }
        }
 
        // 运行模式
        $env = (PHP_SAPI == 'cli') ? TRUE : FALSE;
        $webRoot = '';
        if ($env) {
            $trace = debug_backtrace();
            $listFilePath = $trace[0]['file'];
            $webRoot = substr($listFilePath, 0, strrpos($listFilePath, D_S));
        }
 
        // 主题风格,fold = 折叠,expand = 展开
        $theme = isset($_GET['type']) ? $_GET['type'] : 'fold';
        global $argv;
        if ($env) {
            $theme = isset($argv[1]) ? $argv[1] : 'fold';
        }
        if (!in_array($theme, array('fold', 'expand'))) {
            $theme = 'fold';
        }
 
        //echo json_encode($allApiS) ;
        //字典排列
        ksort($allApiS);
 
        $projectName = $this->projectName;
 
        include dirname(__FILE__) . '/api_list_tpl.php';
    }
}
 
function listDir($dir) {
    $dir .= substr($dir, -1) == D_S ? '' : D_S;
    $dirInfo = array();
    foreach (glob($dir . '*') as $v) {
        if (is_dir($v)) {
            $dirInfo = array_merge($dirInfo, listDir($v));
        } else {
            $dirInfo[] = $v;
        }
    }
    return $dirInfo;
}
 
function saveHtml($webRoot, $name, $string){
    $dir = $webRoot . D_S . 'doc';
    if (!is_dir ( $dir)){
        mkdir ( $dir);
    }
    $handle = fopen ( $dir . DIRECTORY_SEPARATOR . $name . '.html', 'wb');
    fwrite ( $handle, $string);
    fclose ( $handle);
}