forked from janephp/open-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JaneOpenApi.php
157 lines (127 loc) · 5.46 KB
/
JaneOpenApi.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
<?php
namespace Jane\OpenApi;
use Jane\JsonSchema\Generator\ChainGenerator;
use Jane\JsonSchema\Generator\Context\Context;
use Jane\OpenApi\Generator\ModelGenerator;
use Jane\JsonSchema\Generator\Naming;
use Jane\OpenApi\Generator\NormalizerGenerator;
use Jane\JsonSchema\Guesser\ChainGuesser;
use Jane\OpenApi\Generator\GeneratorFactory;
use Jane\OpenApi\Guesser\OpenApiSchema\GuesserFactory;
use Jane\OpenApi\SchemaParser\Converter;
use Jane\OpenApi\SchemaParser\SchemaParser;
use Jane\JsonSchema\Registry;
use Jane\JsonSchema\Schema;
use PhpParser\ParserFactory;
use Symfony\Component\Serializer\Encoder\JsonDecode;
use Symfony\Component\Serializer\Encoder\JsonEncode;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Encoder\YamlEncoder;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Yaml\Dumper;
use Symfony\Component\Yaml\Parser;
use Jane\OpenApi\Guesser\Guess\MultipleClass;
use Jane\OpenApi\Guesser\Guess\ClassGuess;
class JaneOpenApi extends ChainGenerator
{
public const VERSION = '4.x-dev';
private $schemaParser;
private $chainGuesser;
private $strict;
private $naming;
public function __construct(
SchemaParser $schemaParser,
ChainGuesser $chainGuesser,
Naming $naming,
bool $strict = true
) {
$this->schemaParser = $schemaParser;
$this->chainGuesser = $chainGuesser;
$this->strict = $strict;
$this->naming = $naming;
}
public function createContext(Registry $registry): Context
{
$schemas = array_values($registry->getSchemas());
/** @var Schema $schema */
foreach ($schemas as $schema) {
$openApiSpec = $this->schemaParser->parseSchema($schema->getOrigin());
$this->chainGuesser->guessClass($openApiSpec, $schema->getRootName(), $schema->getOrigin() . '#', $registry);
$schema->setParsed($openApiSpec);
}
foreach ($registry->getSchemas() as $schema) {
foreach ($schema->getClasses() as $class) {
$properties = $this->chainGuesser->guessProperties($class->getObject(), $schema->getRootName(), $class->getReference(), $registry);
$names = [];
foreach ($properties as $property) {
$property->setPhpName($this->naming->getPropertyName($property->getName()));
$i = 2;
$newName = $property->getPhpName();
while (\in_array(strtolower($newName), $names, true)) {
$newName = $property->getPhpName() . $i;
++$i;
}
if ($newName !== $property->getPhpName()) {
$property->setPhpName($newName);
}
$names[] = strtolower($property->getPhpName());
$property->setType($this->chainGuesser->guessType($property->getObject(), $property->getName(), $property->getReference(), $registry));
}
$class->setProperties($properties);
$extensionsTypes = [];
foreach ($class->getExtensionsObject() as $pattern => $extensionData) {
$extensionsTypes[$pattern] = $this->chainGuesser->guessType($extensionData['object'], $class->getName(), $extensionData['reference'], $registry);
}
$class->setExtensionsType($extensionsTypes);
}
$this->hydrateDiscriminatedClasses($schema, $registry);
}
return new Context($registry, $this->strict);
}
protected function hydrateDiscriminatedClasses(Schema $schema, Registry $registry)
{
foreach ($schema->getClasses() as $class) {
if ($class instanceof MultipleClass) { // is parent class
foreach ($class->getReferences() as $reference) {
$guess = $registry->getClass($reference);
if ($guess instanceof ClassGuess) { // is child class
$guess->setMultipleClass($class);
}
}
}
}
}
public static function build(array $options = [])
{
$encoders = [
new JsonEncoder(
new JsonEncode(),
new JsonDecode(false)
),
new YamlEncoder(
new Dumper(),
new Parser()
),
];
$normalizers = array_merge(JsonSchema\Version2\Normalizer\NormalizerFactory::create(), JsonSchema\Version3\Normalizer\NormalizerFactory::create());
$serializer = new Serializer($normalizers, $encoders);
$schemaParser = new SchemaParser($serializer, new Converter());
$generators = GeneratorFactory::build($serializer, $options);
$naming = new Naming();
$parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7);
$modelGenerator = new ModelGenerator($naming, $parser);
$normGenerator = new NormalizerGenerator($naming, $options['reference'] ?? false, $options['use-cacheable-supports-method'] ?? false);
$self = new self(
$schemaParser,
GuesserFactory::create($serializer, $options),
$naming,
$options['strict'] ?? true
);
$self->addGenerator($modelGenerator);
$self->addGenerator($normGenerator);
foreach ($generators as $generator) {
$self->addGenerator($generator);
}
return $self;
}
}