-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse-class.php
More file actions
executable file
·269 lines (216 loc) · 6.74 KB
/
parse-class.php
File metadata and controls
executable file
·269 lines (216 loc) · 6.74 KB
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#!/usr/bin/php
<?php
/**
* PARSE CLASS AND GENERATE DOC PAGE
*
* @author axelhahn
* @license GNU GPL 3.0
*
* @source <https://github.com/axelhahn/php-classdoc>
*
* 2024-07-15 v0.1 axelhahn initial version
* 2025-07-21 axelhahn
*/
// ------------------------------------------------------------
// CONFIG
// ------------------------------------------------------------
global $_bDebug; $_bDebug=false;
$outtype='md';
// $sourceurl='';
// ------------------------------------------------------------
// FNUCTTIONS
// ------------------------------------------------------------
/**
* Write given debug text to STDERR
* @param string $s
* @return void
*/
function _wd(string $s): void
{
global $_bDebug;
if ($_bDebug) {
// echo "DEBUG: $s".PHP_EOL;
file_put_contents('php://stderr', "DEBUG: 🔹 $s".PHP_EOL,FILE_APPEND);
}
}
/**
* Show help
* @return void
*/
function showHelp(){
echo "_______________________________________________________________________________
AXELs docpage generator for php class files.
_______________________________________________________________________________
This tool creates a documentation of a given php class file with all methods,
and its parameters.
👤 Author: Axel Hahn
📄 Source: https://github.com/axelhahn/php-classdoc
📜 License: GNU GPL 3.0
📗 Docs: https://www.axel-hahn.de/docs/php-classdoc/
✨ USAGE: parse-class.php [OPTIONS] <classfile.php> [<classname>]
🔸 OPTIONS:
-h, --help Show this help
-d, --debug Enable debug output (written on STDERR)
-o, --out <type> Set output type: 'md' (default).
It is a subdir below folder './config/'
-s, --source <url> Set url of source file in main branch; default: none
This url is used to create a link to the source file
and line number of a method.
Examples:
https://github.com/<user>/<project>/blob/main
https://gitlab.<domain>/<group>/<project>/-/tree/main
🔹 PARAMETERS:
<classfile.php> path to class file
<classname> optional: if classname is not detected you can set
it manually
";
}
// ------------------------------------------------------------
// MAIN
// ------------------------------------------------------------
if (!isset($argv[1])){
echo "ERROR: no class file given".PHP_EOL;
showHelp();
exit(1);
}
array_shift($argv);
foreach ($argv as $argument) {
_wd("Processing param: $argument");
if(preg_match('/^\-/', $argument)){
switch ($argument) {
case '-h':
case '--help':
showHelp();
exit(0);
break;
case '-d':
case '--debug':
$_bDebug=true;
_wd("Enable debugging");
array_shift($argv);
break;
case '-o':
case '--out':
_wd("Set output type to '".$argv[1]."'");
$outtype=$argv[1];
array_shift($argv);
array_shift($argv);
break;
case '-s':
case '--source':
_wd("Set source url to '".$argv[1]."'");
$sourceurl=$argv[1];
array_shift($argv);
array_shift($argv);
break;
}
}
}
$sClassfile=$argv[0] ?? "";
$sClass=$argv[1] ?? "";
_wd("Info: class file is $sClassfile");
_wd("Check existence of file [$sClassfile]");
if(!file_exists($sClassfile)){
_wd("File Not found");
echo "ERROR: file not found: $sClassfile".PHP_EOL;
exit(2);
}
_wd("Loading src/phpclass-parser.class.php");
require "src/phpclass-parser.class.php";
// ---------- load class and parse it
$oParser=new axelhahn\phpclassparser();
if(!$sClass){
_wd("Detect classname and init it: $sClassfile");
if(!$sClass=$oParser->setClassFile($sClassfile)){
echo "ERROR: class not detected in file: $sClassfile".PHP_EOL;
echo "Use a 2nd parameter with the classname".PHP_EOL;
exit(1);
}
} else {
_wd("Load file $sClassfile");
require_once $sClassfile;
_wd("Init parser with param 2: $sClass");
$oParser->setClassname($sClass);
}
if($sourceurl??false){
$oParser->setSourceUrl($sourceurl);
}
// ---------- generate output
$sOut="";
// print_r($oParser->getProperties()); die();
// ----- properties
_wd("Processing properties");
$sOutProperties='';
$aPReplace=[];
foreach($oParser->getProperties() as $aProperty){
foreach($aProperty as $sKey => $value){
if(!is_array($value)){
$aPReplace['{{'.$sKey.'}}']=$value;
}
}
// print_r($aPReplace);
$sOutProperties.=str_replace(
array_keys($aPReplace),
array_values($aPReplace),
file_get_contents("config/$outtype/properties.tpl")
);
}
// ----- methods
_wd("Processing methods");
$sOutMethods="";
foreach($oParser->getMethods() as $sMethodname){
_wd("Method: $sMethodname");
$aMReplace=[];
$aMethod=$oParser->getMethod($sMethodname);
foreach($aMethod as $sKey => $value){
if(!is_array($value)){
$aMReplace['{{'.$sKey.'}}']=$value;
}
}
$aPReplace=[];
$sOutParams="";
foreach($aMethod['parameters'] as $aParam){
foreach ($aParam as $sKeyP => $valueP) {
if(!is_array($valueP)){
$aPReplace['{{'.$sKeyP.'}}']=$valueP;
}
}
$sOutParams.=str_replace(
array_keys($aPReplace),
array_values($aPReplace),
file_get_contents("config/$outtype/parameter.tpl")
);
// _wd($sOutParams);
}
if($sOutParams){
$sOutParams=str_replace(
'{{parameter.tpl}}',
$sOutParams,
file_get_contents("config/$outtype/parameters.tpl")
);
}
$aMReplace['{{parameters.tpl}}']=$sOutParams;
$sOutMethods.=str_replace(
array_keys($aMReplace),
array_values($aMReplace),
file_get_contents("config/$outtype/methods.tpl")
);
// echo $sOut; print_r($aMReplace); die();
}
// ----- docpage
_wd("Merging docpage for $sClass...");
$aPageReplace=[];
foreach($oParser->getClassInfos() as $sKey => $value){
if(!is_array($value)){
$aPageReplace['{{'.$sKey.'}}']=$value;
}
}
$aPageReplace['{{name}}']=$sClass;
$aPageReplace['{{properties.tpl}}']=$sOutProperties ? $sOutProperties : '(none)';
$aPageReplace['{{methods.tpl}}']=$sOutMethods ? $sOutMethods : '(none)';
$sOut=str_replace(
array_keys($aPageReplace),
array_values($aPageReplace),
file_get_contents("config/$outtype/index.tpl")
);
echo $sOut;