-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.php
169 lines (137 loc) · 5.08 KB
/
generator.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
<?php
/*
* ATTENTION: This file is an insane mess, but it works.
* It's a quick and dirty way to generate the documentation template for the alterations.
* It's not meant to be used in any other way than to generate the docs when a new alteration is added.
* Please, stay away from this file. Or not, I'm not your mom.
* Feel free to refactor it if you want to.
*/
use SergiX44\ImageZen\DefaultAlterations;
use SergiX44\ImageZen\Image;
require __DIR__ . '/vendor/autoload.php';
$imageReflection = new ReflectionClass(Image::class);
$availableMethods = [
$imageReflection->getMethod('make'),
$imageReflection->getMethod('base64'),
$imageReflection->getMethod('canvas'),
$imageReflection->getMethod('register'),
$imageReflection->getMethod('width'),
$imageReflection->getMethod('height'),
$imageReflection->getMethod('basePath'),
$imageReflection->getMethod('filter'),
$imageReflection->getMethod('save'),
$imageReflection->getMethod('stream'),
$imageReflection->getMethod('response'),
...(new ReflectionClass(DefaultAlterations::class))?->getMethods(ReflectionMethod::IS_PUBLIC),
$imageReflection->getMethod('destroy'),
];
/** @var ReflectionMethod $method */
foreach ($availableMethods as $key => $method) {
$id = $method->getName();
$file = __DIR__ . "/docs/docs/alterations/$id.md";
if (file_exists($file)) {
$content = file_get_contents($file);
if (str_contains($content, '_modified_: true')) {
fwrite(STDOUT, "Skipping $file\n");
continue;
}
}
// parse docblock
$docComment = $method->getDocComment();
$description = null;
$paramDescriptions = [];
$returnDescription = null;
if ($docComment) {
$lines = explode("\n", $docComment);
foreach ($lines as $line) {
if ($description === null && preg_match('/^\s+\*\s+(.+)/', $line, $matches)) {
$description = trim($matches[1]);
} elseif (preg_match('/@param\s+([^$]+)\s+\$(\w+)\s+(.+)/', $line, $matches)) {
$paramDescriptions[trim($matches[2])] = trim($matches[3]);
} elseif (preg_match('/@return\s+(\S+)\s+(.+)/', $line, $matches)) {
$returnDescription = trim($matches[2]);
}
}
}
// parse params
$paramsList = '';
$params = '';
/** @var ReflectionParameter $parameter */
$parameters = $method->getParameters() ?? [];
foreach ($parameters as $parameter) {
$paramDesc = $paramDescriptions[$parameter->getName()] ?? '';
$paramsList .= "- `{$parameter->getType()} \${$parameter->getName()}`: $paramDesc\n";
if ($parameter->getType() instanceof ReflectionUnionType) {
foreach ($parameter->getType()->getTypes() as $type) {
$params .= $type->getName() . '|';
}
$params = rtrim($params, '|');
$params .= " \${$parameter->getName()}, ";
continue;
}
if ($parameter->isOptional()) {
if ($parameter->isVariadic()) {
$params .= "{$parameter->getType()} ...\${$parameter->getName()}";
} else {
$val = $parameter->getDefaultValue() ?? 'null';
if (enum_exists($parameter->getType()->getName())) {
$e = new ReflectionEnumBackedCase($parameter->getType()->getName(), $val->name);
$val = "{$e->getDeclaringClass()->getName()}::{$e->getName()}";
}
if (is_bool($val)) {
$val = $val ? 'true' : 'false';
}
$params .= "[{$parameter->getType()} \${$parameter->getName()} = {$val}]";
}
} else {
$params .= "{$parameter->getType()} \${$parameter->getName()}";
}
if ($parameter !== end($parameters)) {
$params .= ', ';
}
}
// parse return value
$returnValue = $method->getReturnType();
if ($returnValue instanceof ReflectionUnionType) {
$returnValue = '';
foreach ($method->getReturnType()->getTypes() as $type) {
$returnValue .= $type->getName() . '|';
}
$returnValue = rtrim($returnValue, '|');
} else {
$returnValue = $returnValue?->getName() ?? 'void';
}
if ($returnValue === 'SergiX44\ImageZen\Image' || $returnValue === 'self') {
$parsedReturnValue = 'Instance of `' . Image::class . '`.';
} elseif ($returnValue === 'void') {
$parsedReturnValue = '`void`, no return value.';
} else {
$parsedReturnValue = "`{$returnValue}`: $returnDescription";
}
if (empty($paramsList)) {
$paramsList = '<i>This method has no parameters.</i>';
}
$markdown = <<<MARKDOWN
---
sidebar_position: $key
_modified_: false
---
# `$id()`
```php
->$id($params): $returnValue
```
$description
## Parameters
$paramsList
## Returns
$parsedReturnValue
## Example
```php
use SergiX44\ImageZen\Image;
\$image = Image::make('path/to/image.jpg')
->$id($params);
```
MARKDOWN;
file_put_contents($file, $markdown);
fwrite(STDOUT, "Generated $file\n");
}