-
Notifications
You must be signed in to change notification settings - Fork 11
/
generate_docs.php
210 lines (163 loc) · 5.02 KB
/
generate_docs.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
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
<?php
/** @noinspection AutoloadingIssuesInspection */
/** @noinspection PhpUnhandledExceptionInspection */
require __DIR__.'/vendor/autoload.php';
$factory = \phpDocumentor\Reflection\DocBlockFactory::createInstance();
$reflection = new ReflectionClass(\Str\Str::make(''));
// -------------------------------------
$templateDocument = <<<RAW
%about%
---------------------
%install%
%features%
---------------------
## Functions Index:
%__functions_index__%
## Functions List:
%__functions_list__%
---------------------
%benchmark%
%development%
RAW;
$templateMethodParam = <<<RAW
- %param%
RAW;
$templateMethodReturn = <<<RAW
- %return%
RAW;
$templateMethod = <<<RAW
## %name%
%description%
**Parameters:**
%params%
**Return:**
%return%
--------
RAW;
$templateIndexLink = <<<RAW
- <a href="%href%">%title%</a>
RAW;
// -------------------------------------
class TemplateFormatter
{
/** @var array */
private $vars = [];
/** @var string */
private $template;
/**
* @param string $template
*/
public function __construct(string $template)
{
$this->template = $template;
}
/**
* @param string $var
* @param string $value
*
* @return mixed
*/
public function set(string $var, string $value): self
{
$this->vars[$var] = $value;
return $this;
}
/**
* @return string
*/
public function format(): string
{
$s = \Str\Str::make($this->template);
foreach ($this->vars as $name => $value) {
$s->replace(sprintf('%%%s%%', $name), $value);
}
return $s->getString();
}
}
// -------------------------------------
$functionsDocumentation = [];
$functionsIndex = [];
foreach ($reflection->getMethods() as $method) {
if (!$method->isPublic()) {
continue;
}
if (\Str\Str::make($method->getShortName())->startsWith('_')) {
continue;
}
$doc = $factory->create($method->getDocComment());
$methodIndexTemplate = new TemplateFormatter($templateIndexLink);
$methodIndexTemplate->set('title', $method->getShortName());
$methodIndexTemplate->set('href', \Str\Str::make((string) $method->getShortName())
->toLowerCase()
->trim()
->stripWhitespace()
->prepend('#')
->getString()
);
$methodTemplate = new TemplateFormatter($templateMethod);
$methodTemplate->set('name', $method->getShortName());
$methodTemplate->set('description', $doc->getDescription());
$methodTemplate->set('code', '```php echo ```');
// -- params
$params = [];
foreach ($tagsInput = $doc->getTagsByName('param') as $tagParam) {
$paramsTemplate = new TemplateFormatter($templateMethodParam);
$paramsTemplate->set('param', (string) $tagParam);
$params[] = $paramsTemplate->format();
}
if (0 !== \count($params)) {
$methodTemplate->set('params', implode("\n", $params));
} else {
$methodTemplate->set('params', '__nothing__');
}
// -- return
$returns = [];
foreach ($tagsInput = $doc->getTagsByName('return') as $tagReturn) {
$returnTemplate = new TemplateFormatter($templateMethodReturn);
$returnTemplate->set('return', (string) $tagReturn);
$returns[] = $returnTemplate->format();
}
if (0 !== \count($returns)) {
$methodTemplate->set('return', implode("\n", $returns));
} else {
$methodTemplate->set('return', '__void__');
}
$functionsDocumentation[$method->getShortName()] = $methodTemplate->format();
$functionsIndex[$method->getShortName()] = $methodIndexTemplate->format();
}
ksort($functionsDocumentation);
$functionsDocumentation = array_values($functionsDocumentation);
ksort($functionsIndex);
// -------------------------------------
$documentTemplate = new TemplateFormatter($templateDocument);
$documentTemplate->set('__functions_list__', implode("\n", $functionsDocumentation));
$indexLastChar = null;
$indexStrResult = '';
foreach ($functionsIndex as $_index => $_template) {
$char = \Str\Str::make((string) $_index)
->first(1)
->toUpperCase();
if ($indexLastChar !== $char->getString()) {
$indexLastChar = $char->getString();
$indexStrResult .= sprintf("\n%s\n", $indexLastChar);
}
$indexStrResult .= sprintf("%s\n", $_template);
}
$documentTemplate->set('__functions_index__', $indexStrResult);
$directoryIterator = new RecursiveDirectoryIterator(__DIR__.'/docs/index');
$iterator = new IteratorIterator($directoryIterator);
foreach ($iterator as $docFile) {
/* @var SplFileInfo $docFile */
if ('md' === $docFile->getExtension()) {
$sName = \Str\Str::make((string) $docFile->getFilename())->shift('.');
$documentTemplate->set(
$sName->toLowerCase()->getString(),
sprintf(
"## %s\n\n%s",
$sName->titleize()->getString(),
file_get_contents($docFile->getRealPath())
)
);
}
}
file_put_contents(__DIR__.'/README.md', $documentTemplate->format());