forked from webonyx/graphql-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-class-reference.php
193 lines (168 loc) · 5.54 KB
/
generate-class-reference.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php declare(strict_types=1);
require __DIR__ . '/vendor/autoload.php';
use GraphQL\Error\ClientAware;
use GraphQL\Error\DebugFlag;
use GraphQL\Error\Error;
use GraphQL\Error\FormattedError;
use GraphQL\Error\Warning;
use GraphQL\Executor\ExecutionResult;
use GraphQL\Executor\Executor;
use GraphQL\Executor\Promise\PromiseAdapter;
use GraphQL\GraphQL;
use GraphQL\Language\AST\NodeKind;
use GraphQL\Language\DirectiveLocation;
use GraphQL\Language\Parser;
use GraphQL\Language\Printer;
use GraphQL\Language\Visitor;
use GraphQL\Server\Helper;
use GraphQL\Server\OperationParams;
use GraphQL\Server\ServerConfig;
use GraphQL\Server\StandardServer;
use GraphQL\Type\Definition\ResolveInfo;
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Schema;
use GraphQL\Type\SchemaConfig;
use GraphQL\Utils\AST;
use GraphQL\Utils\BuildSchema;
use GraphQL\Utils\PhpDoc;
use GraphQL\Utils\SchemaPrinter;
use GraphQL\Validator\DocumentValidator;
use Symfony\Component\VarExporter\Exception\ExceptionInterface;
use Symfony\Component\VarExporter\VarExporter;
const OUTPUT_FILE = __DIR__ . '/docs/class-reference.md';
const ENTRIES = [
GraphQL::class => [],
Type::class => [],
ResolveInfo::class => [],
DirectiveLocation::class => ['constants' => true],
SchemaConfig::class => [],
Schema::class => [],
Parser::class => [],
Printer::class => [],
Visitor::class => [],
NodeKind::class => ['constants' => true],
Executor::class => [],
ExecutionResult::class => [],
PromiseAdapter::class => [],
DocumentValidator::class => [],
Error::class => ['constants' => true],
Warning::class => ['constants' => true],
ClientAware::class => [],
DebugFlag::class => ['constants' => true],
FormattedError::class => [],
StandardServer::class => [],
ServerConfig::class => [],
Helper::class => [],
OperationParams::class => [],
BuildSchema::class => [],
AST::class => [],
SchemaPrinter::class => [],
];
/**
* @param ReflectionClass<object> $class
* @param array{constants?: bool, props?: bool, methods?: bool} $options
*
* @throws ExceptionInterface
*/
function renderClass(ReflectionClass $class, array $options): string
{
$classDocs = PhpDoc::unwrap(PhpDoc::unpad($class->getDocComment()));
$content = '';
$className = $class->getName();
if ($options['constants'] ?? false) {
$constants = [];
foreach ($class->getConstants(/* TODO enable with PHP 8: ReflectionClassConstant::IS_PUBLIC */) as $name => $value) {
$constants[] = "const {$name} = " . VarExporter::export($value) . ';';
}
if ($constants !== []) {
$constants = "```php\n" . implode("\n", $constants) . "\n```";
$content .= "### {$className} Constants\n\n{$constants}\n\n";
}
}
if ($options['props'] ?? true) {
$props = [];
foreach ($class->getProperties(ReflectionProperty::IS_PUBLIC) as $property) {
if (isApi($property)) {
$props[] = renderProp($property);
}
}
if ($props !== []) {
$props = "```php\n" . implode("\n\n", $props) . "\n```";
$content .= "### {$className} Props\n\n{$props}\n\n";
}
}
if ($options['methods'] ?? true) {
$methods = [];
foreach ($class->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
if (isApi($method)) {
$methods[] = renderMethod($method);
}
}
if ($methods !== []) {
$methods = implode("\n\n", $methods);
$content .= "### {$className} Methods\n\n{$methods}\n\n";
}
}
return <<<TEMPLATE
## {$className}
{$classDocs}
{$content}
TEMPLATE;
}
function renderMethod(ReflectionMethod $method): string
{
$args = array_map(
static function (ReflectionParameter $p): string {
$type = ltrim($p->getType() . ' ');
$def = $type . '$' . $p->getName();
if ($p->isDefaultValueAvailable()) {
$val = $p->isDefaultValueConstant()
? $p->getDefaultValueConstantName()
: $p->getDefaultValue();
$def .= ' = ' . VarExporter::export($val);
}
return $def;
},
$method->getParameters()
);
$argsStr = implode(', ', $args);
if (strlen($argsStr) >= 80) {
$argsStr = "\n " . implode(",\n ", $args) . "\n";
}
$returnType = $method->getReturnType();
$def = "function {$method->getName()}({$argsStr})";
$def = $method->isStatic()
? "static {$def}"
: $def;
$def = $returnType instanceof ReflectionType
? "{$def}: {$returnType}"
: $def;
$docBlock = PhpDoc::unpad($method->getDocComment());
return <<<TEMPLATE
```php
{$docBlock}
{$def}
```
TEMPLATE;
}
function renderProp(ReflectionProperty $prop): string
{
$signature = implode(' ', Reflection::getModifierNames($prop->getModifiers())) . ' $' . $prop->getName() . ';';
return PhpDoc::unpad($prop->getDocComment()) . "\n" . $signature;
}
/**
* @param ReflectionProperty|ReflectionMethod $reflector
*/
function isApi(Reflector $reflector): bool
{
$comment = $reflector->getDocComment();
if ($comment === false) {
return false;
}
return preg_match('~[\r\n ]+\* @api~', $comment) === 1;
}
file_put_contents(OUTPUT_FILE, '');
foreach (ENTRIES as $className => $options) {
$rendered = renderClass(new ReflectionClass($className), $options);
file_put_contents(OUTPUT_FILE, $rendered, FILE_APPEND);
}