-
-
Notifications
You must be signed in to change notification settings - Fork 133
Expand file tree
/
Copy pathConfigSchemaDumper.php
More file actions
109 lines (92 loc) · 3.13 KB
/
Copy pathConfigSchemaDumper.php
File metadata and controls
109 lines (92 loc) · 3.13 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
<?php
namespace PhpBench\Development;
use PhpBench\DependencyInjection\ExtensionInterface;
use Symfony\Component\OptionsResolver\Debug\OptionsResolverIntrospector;
use Symfony\Component\OptionsResolver\Exception\NoConfigurationException;
use Symfony\Component\OptionsResolver\OptionsResolver;
use function json_encode;
use function method_exists;
class ConfigSchemaDumper
{
/**
* @var class-string[]
*/
private $extensions;
/**
* @param class-string[] $extensions
*/
public function __construct(array $extensions)
{
$this->extensions = $extensions;
}
public function dump(): string
{
if (!method_exists(OptionsResolver::class, 'getInfo')) {
return 'Config reference generation requires Symfony Options Resolver ^5.0';
}
$schema = [
'$schema' => 'https =>//json-schema.org/draft/2020-12/schema',
'title' => 'PHPBench configuration',
'type' => 'object',
'properties' => [
'$include' => [
'description' => 'Include another config file relative to this one',
'type' => ['string', 'array'],
],
'$include-glob' => [
'description' => 'Include config files using a glob pattern. Paths are relative to the config file',
'type' => ['string', 'array'],
],
],
];
foreach ($this->extensions as $extensionClass) {
$optionsResolver = new OptionsResolver();
$extension = new $extensionClass();
assert($extension instanceof ExtensionInterface);
$extension->configure($optionsResolver);
if (!$optionsResolver->getDefinedOptions()) {
continue;
}
$inspector = new OptionsResolverIntrospector($optionsResolver);
foreach ($optionsResolver->getDefinedOptions() as $option) {
$meta = [
'description' => $optionsResolver->getInfo($option),
'type' => $this->mapTypes($inspector->getAllowedTypes($option)),
];
try {
$values = $inspector->getAllowedValues($option);
$meta['enum'] = $values;
} catch (NoConfigurationException $e) {
}
$schema['properties'][$option] = $meta;
}
}
return (string)json_encode($schema, JSON_PRETTY_PRINT);
}
/**
* @param string[] $types
*
* @return string[]
*/
private function mapTypes(array $types): array
{
return array_map(function (string $type) {
if ($type === 'array') {
return 'object';
}
if ($type === 'bool') {
return 'boolean';
}
if ($type === 'int') {
return 'integer';
}
if ($type === 'float') {
return 'number';
}
if (substr($type, -2) === '[]') {
return 'array';
}
return $type;
}, $types);
}
}