-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerateDocs.php
84 lines (73 loc) · 2.65 KB
/
generateDocs.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
<?php
require_once __DIR__ . '/vendor/autoload.php';
use ReflectionClass;
use ReflectionMethod;
// Generate README
function generateReadme($className, $readmeFile = 'DOCS.md', $htmlFile = 'docs/DOCS.html')
{
$reflection = new ReflectionClass($className);
$doc = "# {$reflection->getShortName()}\n\n";
// Add link to the HTML documentation
$doc .= "[Full Documentation](DOCS.html)\n\n";
$doc .= $reflection->getDocComment() . "\n\n";
$doc .= "## Methods\n\n";
foreach ($reflection->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
$doc .= "### {$method->getName()}\n\n";
$doc .= $method->getDocComment() . "\n\n";
// Example usage
$params = array_map(fn($param) => '$' . $param->getName(), $method->getParameters());
$doc .= "Example usage:\n";
$doc .= "```php\n";
$doc .= "\$instance->{$method->getName()}(" . implode(', ', $params) . ");\n";
$doc .= "```\n\n";
}
file_put_contents($readmeFile, $doc);
generateHtmlDocumentation($reflection, $htmlFile);
}
// Generate HTML documentation with Bootstrap 5
function generateHtmlDocumentation(ReflectionClass $reflection, $filePath)
{
$html = <<<HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{$reflection->getShortName()} Documentation</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container my-5">
<h1 class="mb-4">{$reflection->getShortName()} Documentation</h1>
<p><a href="../README.md" class="btn btn-primary">View README</a></p>
<div class="card mt-4">
<div class="card-body">
<h5 class="card-title">Class Overview</h5>
<p>{$reflection->getDocComment()}</p>
</div>
</div>
<h2 class="mt-5">Methods</h2>
HTML;
foreach ($reflection->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
$docComment = $method->getDocComment();
$params = implode(', ', array_map(fn($param) => '$' . $param->getName(), $method->getParameters()));
$html .= <<<HTML
<div class="card mt-3">
<div class="card-body">
<h5 class="card-title">{$method->getName()}</h5>
<p class="card-text">{$docComment}</p>
<h6>Example usage:</h6>
<pre><code>$instance->{$method->getName()}($params);</code></pre>
</div>
</div>
HTML;
}
$html .= <<<HTML
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
HTML;
file_put_contents($filePath, $html);
}
generateReadme(Joaojkuligowski\Mypersist\Base::class);