-
Notifications
You must be signed in to change notification settings - Fork 2
/
Parser.php
119 lines (106 loc) · 3.53 KB
/
Parser.php
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
<?php
class Parser {
public function parse ($file) {
if ($this->checkCache($file)) {
return $this->cacheRead($file);
}
$doc = $this->fromFile($file);
$this->cacheSave($file, $doc);
return $doc;
}
public function fromFile ($file) {
$content = file_get_contents($file);
$comments = $this->getComments($content);
$doc = [];
foreach ($comments as $comment) {
if (empty($comment) || preg_match("/\*\ \@api/", $comment) == 0) {
continue;
}
$doc[] = [
'params' => $this->getParams($comment),
'columns' => $this->getColumns($comment),
'url' => $this->getField($comment, 'url'),
'name' => $this->getField($comment, 'name'),
'category' => $this->getField($comment, 'category'),
'desc' => $this->getField($comment, 'desc'),
'method' => $this->getField($comment, 'method'),
];
}
return $doc;
}
public function getCacheDir () {
return __DIR__.'/cache';
}
protected function getCachePath ($file) {
if (strpos($file, '..') === 0) {
$file = dirname(__DIR__).substr($file, 2);
}
$log = exec('cd '.dirname(__DIR__).' && git log -1 --oneline '.$file);
$commitId = explode(' ', $log)[0];
return $this->getCacheDir().'/'.$commitId.'-'.md5($file).'.json';
}
protected function checkCache ($file) {
$path = $this->getCachePath($file);
return file_exists($path);
}
protected function cacheRead ($file) {
$path = $this->getCachePath($file);
$doc = json_decode(file_get_contents($path), true);
return $doc;
}
protected function cacheSave ($file, $data) {
$cacheDir = $this->getCacheDir();
if (!file_exists($cacheDir)) {
mkdir($cacheDir);
}
$path = $this->getCachePath($file);
file_put_contents($path, json_encode($data));
}
public function getComments ($str) {
$matches = [];
if (preg_match_all('/\/\*([^\*^\/]*|[\*^\/*]*|[^\**\/]*)*\*\//', $str, $matches)) {
return $matches[0] ?: [];
}
return [];
}
public function getField ($str, $field) {
$matches = [];
if (preg_match("/\*\ \@{$field}\ (.*)/", $str, $matches)) {
return $matches[1] ?: '';
}
return '';
}
public function getFields ($str, $field) {
$matches = [];
if (preg_match_all("/\*\ \@{$field}\ *(.*)/", $str, $matches)) {
return $matches[1] ?: [];
}
return [];
}
public function getParams ($str) {
$params = [];
$matches = $this->getFields($str, 'param');
foreach ($matches as $match) {
$f = array_merge(array_filter(explode(' ', $match)));
$params[] = [
'name' => $f[0] ?: '',
'class' => $f[1] ?: '',
'needle' => $f[2] ?: '',
'desc' => join(' ', array_slice($f, 3)),
];
}
return $params;
}
public function getColumns ($str) {
$rets = [];
$matches = $this->getFields($str, 'column');
foreach ($matches as $match) {
$f = array_merge(array_filter(explode(' ', $match)));
$rets[] = [
'name' => $f[0] ?: '',
'desc' => join(' ', array_slice($f, 1)),
];
}
return $rets;
}
}