Skip to content

Commit 0eb1dfe

Browse files
committed
:octocat: auto generate options doc from the QROptionsTrait source
1 parent cf0648e commit 0eb1dfe

File tree

3 files changed

+224
-55
lines changed

3 files changed

+224
-55
lines changed

.github/workflows/ci.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,18 @@ jobs:
135135
- name: "Checkout sources"
136136
uses: actions/checkout@v3
137137

138+
- name: "Install PHP"
139+
uses: shivammathur/setup-php@v2
140+
with:
141+
php-version: "8.1"
142+
coverage: none
143+
138144
- name: "Install Sphinx"
139145
run: pip install sphinx myst-parser sphinx-rtd-theme
140146

147+
- name: "Build QROptions doc"
148+
run: php docs/qroptions-doc.php
149+
141150
- name: "Build manual"
142151
run: |
143152
cd docs

docs/qroptions-doc.php

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
/**
3+
* Auto generates the configuration settings markdown source
4+
*
5+
* @created 03.10.2023
6+
* @author smiley <[email protected]>
7+
* @copyright 2023 smiley
8+
* @license MIT
9+
*/
10+
11+
use chillerlan\QRCode\QROptionsTrait;
12+
13+
require_once __DIR__.'/../vendor/autoload.php';
14+
15+
$file = 'Usage/Configuration-settings.md';
16+
$content = [
17+
'# Configuration settings',
18+
'<!-- This file is auto generated from the source of QROptionsTrait.php -->',
19+
];
20+
21+
$reflectionClass = new ReflectionClass(QROptionsTrait::class);
22+
23+
foreach($reflectionClass->getProperties(ReflectionProperty::IS_PROTECTED) as $reflectionProperty){
24+
$docblock = $reflectionProperty->getDocComment();
25+
26+
// don't document deprecated settings
27+
if(str_contains($docblock, '@deprecated')){
28+
continue;
29+
}
30+
31+
$content[] = sprintf("## %s\n", $reflectionProperty->getName());
32+
33+
$lines = array_map(fn($l) => trim($l, "\ \t\n\r\0\x0B*"), explode("\n", $docblock));
34+
35+
array_shift($lines);
36+
array_pop($lines);
37+
38+
$see = [];
39+
40+
foreach($lines as $line){
41+
42+
// skip @todo and @var
43+
if(str_contains($line, '@todo') || str_contains($line, '@var')){
44+
continue;
45+
}
46+
47+
// collect links for "see also"
48+
if(str_starts_with($line, '@see')){
49+
$see[] = $line;
50+
51+
continue;
52+
}
53+
54+
$content[] = $line;
55+
}
56+
57+
// add a "see also" section
58+
if(!empty($see)){
59+
$content[] = "**See also:**\n";
60+
61+
foreach($see as $line){
62+
$line = substr($line, 5); // cut off the "@see "
63+
64+
// normal links
65+
if(str_starts_with($line, 'http')){
66+
$content[] = sprintf('- [%s](%s)', explode('://', $line)[1], $line);
67+
}
68+
// php.net documentation
69+
elseif(str_starts_with($line, '\\') && !str_contains($line, 'chillerlan')){
70+
$path = str_replace(['\\', '::', '()'], ['', '.', ''], strtolower($line));
71+
72+
if(!str_contains($line, '::')){
73+
$path = 'function.'.$path;
74+
}
75+
76+
$content[] = sprintf('- [php.net: `%s`](https://www.php.net/manual/%s)', $line, $path);
77+
}
78+
// FQCN
79+
else{
80+
$content[] = sprintf('- `%s`', $line);
81+
}
82+
83+
}
84+
85+
}
86+
87+
$content[] = "\n";
88+
}
89+
90+
file_put_contents(__DIR__.'/'.$file, implode("\n", $content));
91+
92+
printf('Built "%s" from "%s"', $file, QROptionsTrait::class);
93+
94+
exit(0);

0 commit comments

Comments
 (0)