1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* (c) Anton Medvedev <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Deployer\Documentation; |
12
|
|
|
|
13
|
|
|
class ApiGen |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var array |
17
|
|
|
*/ |
18
|
|
|
private $fns = []; |
19
|
|
|
|
20
|
|
|
public function parse(string $source): void |
21
|
|
|
{ |
22
|
|
|
$comment = ''; |
23
|
|
|
$params = ''; |
24
|
|
|
$signature = ''; |
25
|
|
|
|
26
|
|
|
$source = str_replace("\r\n", "\n", $source); |
27
|
|
|
|
28
|
|
|
$state = 'root'; |
29
|
|
|
foreach (explode("\n", $source) as $lineNumber => $line) { |
30
|
|
|
switch ($state) { |
31
|
|
|
case 'root': |
32
|
|
|
if (str_starts_with($line, '/**')) { |
33
|
|
|
$state = 'comment'; |
34
|
|
|
break; |
35
|
|
|
} |
36
|
|
|
if (str_starts_with($line, 'function')) { |
37
|
|
|
$signature = preg_replace('/^function\s+/', '', $line); |
38
|
|
|
$funcName = preg_replace('/\(.*$/', '', $signature); |
39
|
|
|
$this->fns[] = [ |
40
|
|
|
'comment' => $comment, |
41
|
|
|
'params' => $params, |
42
|
|
|
'funcName' => $funcName, |
43
|
|
|
'signature' => $signature, |
44
|
|
|
]; |
45
|
|
|
$comment = ''; |
46
|
|
|
$params = ''; |
47
|
|
|
|
48
|
|
|
if (str_ends_with($signature, '(')) { |
49
|
|
|
$state = 'params'; |
50
|
|
|
} else { |
51
|
|
|
$signature = ''; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
break; |
55
|
|
|
|
56
|
|
|
case 'comment': |
57
|
|
|
if (str_ends_with($line, '*/')) { |
58
|
|
|
$state = 'root'; |
59
|
|
|
break; |
60
|
|
|
} |
61
|
|
|
if (preg_match('/^\s\*\s@param\s(?<type>.+?)\$(?<name>.+?)\s(?<comment>.+)$/', $line, $matches)) { |
62
|
|
|
if (empty($params)) { |
63
|
|
|
$params = "| Argument | Type | Comment |\n|---|---|---|\n"; |
64
|
|
|
} |
65
|
|
|
$type = implode(' or ', array_map(function ($t) { |
66
|
|
|
$t = trim($t, ' '); |
67
|
|
|
return "`$t`"; |
68
|
|
|
}, explode('|', $matches['type']))); |
69
|
|
|
$params .= "| `\${$matches['name']}` | $type | {$matches['comment']} |\n"; |
70
|
|
|
break; |
71
|
|
|
} |
72
|
|
|
if (str_starts_with($line, ' * @')) { |
73
|
|
|
break; |
74
|
|
|
} |
75
|
|
|
$comment .= preg_replace('/^\s\*\s?/', '', $line) . "\n"; |
76
|
|
|
break; |
77
|
|
|
|
78
|
|
|
case 'params': |
79
|
|
|
if (preg_match('/^\).+\{$/', $line, $matches)) { |
80
|
|
|
$signature .= "\n" . preg_replace('/\{$/', '', $line); |
81
|
|
|
$this->fns[count($this->fns) - 1]['signature'] = $signature; |
82
|
|
|
$state = 'root'; |
83
|
|
|
} else { |
84
|
|
|
$signature .= "\n" . $line; |
85
|
|
|
} |
86
|
|
|
break; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function markdown(): string |
92
|
|
|
{ |
93
|
|
|
$output = <<<MD |
94
|
|
|
<!-- DO NOT EDIT THIS FILE! --> |
95
|
|
|
<!-- Instead edit src/functions.php --> |
96
|
|
|
<!-- Then run bin/docgen --> |
97
|
|
|
|
98
|
|
|
# API Reference |
99
|
|
|
|
100
|
|
|
|
101
|
|
|
MD; |
102
|
|
|
|
103
|
|
|
foreach ($this->fns as $fn) { |
104
|
|
|
[ |
105
|
|
|
'comment' => $comment, |
106
|
|
|
'params' => $params, |
107
|
|
|
'funcName' => $funcName, |
108
|
|
|
'signature' => $signature, |
109
|
|
|
] = $fn; |
110
|
|
|
|
111
|
|
|
if (!empty($params)) { |
112
|
|
|
$params = "\n$params"; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$output .= <<<MD |
116
|
|
|
## $funcName() |
117
|
|
|
|
118
|
|
|
```php |
119
|
|
|
$signature |
120
|
|
|
``` |
121
|
|
|
|
122
|
|
|
$comment |
123
|
|
|
$params |
124
|
|
|
|
125
|
|
|
MD; |
126
|
|
|
} |
127
|
|
|
return $output; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|