1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use voku\helper\UTF8; |
4
|
|
|
|
5
|
|
|
require __DIR__ . '/../vendor/autoload.php'; |
6
|
|
|
|
7
|
|
|
$factory = \phpDocumentor\Reflection\DocBlockFactory::createInstance(); |
8
|
|
|
$reflection = new ReflectionClass(UTF8::class); |
9
|
|
|
|
10
|
|
|
// ------------------------------------- |
11
|
|
|
|
12
|
|
|
$templateDocument = file_get_contents(__DIR__ . '/docs/base.md'); |
13
|
|
|
|
14
|
|
|
$templateMethodParam = <<<RAW |
15
|
|
|
- %param% |
16
|
|
|
RAW; |
17
|
|
|
|
18
|
|
|
$templateMethodReturn = <<<RAW |
19
|
|
|
- %return% |
20
|
|
|
RAW; |
21
|
|
|
|
22
|
|
|
$templateMethod = <<<RAW |
23
|
|
|
## %name% |
24
|
|
|
%description% |
25
|
|
|
|
26
|
|
|
**Parameters:** |
27
|
|
|
%params% |
28
|
|
|
|
29
|
|
|
**Return:** |
30
|
|
|
%return% |
31
|
|
|
-------- |
32
|
|
|
RAW; |
33
|
|
|
|
34
|
|
|
$templateIndexLink = <<<RAW |
35
|
|
|
<a href="%href%">%title%</a> |
36
|
|
|
RAW; |
37
|
|
|
|
38
|
|
|
// ------------------------------------- |
39
|
|
|
|
40
|
|
|
class TemplateFormatter { |
41
|
|
|
/** @var array */ |
42
|
|
|
private $vars = []; |
43
|
|
|
|
44
|
|
|
/** @var string */ |
45
|
|
|
private $template; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param string $template |
49
|
|
|
*/ |
50
|
|
|
public function __construct(string $template) { |
51
|
|
|
$this->template = $template; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param string $var |
56
|
|
|
* @param string $value |
57
|
|
|
* |
58
|
|
|
* @return mixed |
59
|
|
|
*/ |
60
|
|
|
public function set(string $var, string $value): self { |
61
|
|
|
$this->vars[$var] = $value; |
62
|
|
|
|
63
|
|
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
|
|
public function format(): string { |
70
|
|
|
$s = $this->template; |
71
|
|
|
|
72
|
|
|
foreach ($this->vars as $name => $value) { |
73
|
|
|
$s = UTF8::replace($s, sprintf('%%%s%%', $name), $value); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $s; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
// ------------------------------------- |
81
|
|
|
|
82
|
|
|
$functionsDocumentation = []; |
83
|
|
|
$functionsIndex = []; |
84
|
|
|
|
85
|
|
|
foreach ($reflection->getMethods() as $method) { |
86
|
|
|
if (!$method->isPublic()) { |
87
|
|
|
continue; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if (UTF8::str_starts_with($method->getShortName(), '_')) { |
91
|
|
|
continue; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$doc = $factory->create($method->getDocComment()); |
95
|
|
|
|
96
|
|
|
$methodIndexTemplate = new TemplateFormatter($templateIndexLink); |
97
|
|
|
|
98
|
|
|
$methodTemplate = new TemplateFormatter($templateMethod); |
99
|
|
|
|
100
|
|
|
// -- params |
101
|
|
|
$params = []; |
102
|
|
|
$paramsTypes = []; |
103
|
|
|
foreach ($tagsInput = $doc->getTagsByName('param') as $tagParam) { |
104
|
|
|
/** @var \phpDocumentor\Reflection\DocBlock\Tags\Param $tagParam */ |
105
|
|
|
$paramsTemplate = new TemplateFormatter($templateMethodParam); |
106
|
|
|
$paramsTemplate->set('param', (string)$tagParam); |
107
|
|
|
$params[] = $paramsTemplate->format(); |
108
|
|
|
$paramsTypes[] = $tagParam->getType() . ' ' . '$' . $tagParam->getVariableName(); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
if (count($params) !== 0) { |
112
|
|
|
$methodTemplate->set('params', implode("\n", $params)); |
113
|
|
|
} else { |
114
|
|
|
$methodTemplate->set('params', '__nothing__'); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
// -- return |
118
|
|
|
$returns = []; |
119
|
|
|
$returnsTypes = []; |
120
|
|
|
foreach ($tagsInput = $doc->getTagsByName('return') as $tagReturn) { |
121
|
|
|
/** @var \phpDocumentor\Reflection\DocBlock\Tags\Return_ $tagReturn */ |
122
|
|
|
$returnTemplate = new TemplateFormatter($templateMethodReturn); |
123
|
|
|
$returnTemplate->set('return', (string)$tagReturn); |
124
|
|
|
$returns[] = $returnTemplate->format(); |
125
|
|
|
$returnsTypes[] = $tagParam->getType(); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
if (count($returns) !== 0) { |
129
|
|
|
$methodTemplate->set('return', implode("\n", $returns)); |
130
|
|
|
} else { |
131
|
|
|
$methodTemplate->set('return', '__void__'); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$methodWithType = $method->getShortName() . '(' . implode(', ', $paramsTypes) . '): ' . implode('|', $returnsTypes); |
135
|
|
|
|
136
|
|
|
$methodTemplate->set('name', $methodWithType); |
137
|
|
|
$methodTemplate->set('description', $doc->getDescription()); |
138
|
|
|
$methodTemplate->set('code', '```php echo ```'); |
139
|
|
|
|
140
|
|
|
$methodIndexTemplate->set('title', $method->getShortName()); |
141
|
|
|
$methodIndexTemplate->set('href', '#' . UTF8::css_identifier( |
142
|
|
|
UTF8::strip_whitespace( |
143
|
|
|
UTF8::trim( |
144
|
|
|
UTF8::strtolower($methodWithType) |
145
|
|
|
) |
146
|
|
|
) |
147
|
|
|
)); |
148
|
|
|
|
149
|
|
|
$functionsDocumentation[$method->getShortName()] = $methodTemplate->format(); |
150
|
|
|
$functionsIndex[$method->getShortName()] = $methodIndexTemplate->format(); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
ksort($functionsDocumentation); |
154
|
|
|
$functionsDocumentation = array_values($functionsDocumentation); |
155
|
|
|
|
156
|
|
|
ksort($functionsIndex); |
157
|
|
|
|
158
|
|
|
// ------------------------------------- |
159
|
|
|
|
160
|
|
|
$documentTemplate = new TemplateFormatter($templateDocument); |
161
|
|
|
$documentTemplate->set('__functions_list__', implode("\n", $functionsDocumentation)); |
162
|
|
|
|
163
|
|
|
$indexLastChar = null; |
164
|
|
|
$indexStrResult = ''; |
165
|
|
|
$counterTmp = 0; |
166
|
|
|
foreach ($functionsIndex as $_index => $_template) { |
167
|
|
|
$counterTmp++; |
168
|
|
|
|
169
|
|
|
if ($counterTmp === 1) { |
170
|
|
|
$indexStrResult .= '<tr>'; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
$indexStrResult .= '<td>' . sprintf("%s\n", $_template) . '</td>'; |
174
|
|
|
|
175
|
|
|
if ($counterTmp === 4) { |
176
|
|
|
$counterTmp = 0; |
177
|
|
|
$indexStrResult .= '</tr>'; |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
if ($counterTmp > 0) { |
181
|
|
|
$indexStrResult .= '</tr>'; |
182
|
|
|
} |
183
|
|
|
$indexStrResult = ' |
184
|
|
|
<table> |
185
|
|
|
' . $indexStrResult . ' |
186
|
|
|
</table> |
187
|
|
|
'; |
188
|
|
|
|
189
|
|
|
$documentTemplate->set('__functions_index__', $indexStrResult); |
190
|
|
|
|
191
|
|
|
file_put_contents(__DIR__ . '/README_TEST.md', $documentTemplate->format()); |
192
|
|
|
|