forked from nelmio/NelmioApiDocBundle
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathApiDocGenerator.php
164 lines (132 loc) · 4.92 KB
/
ApiDocGenerator.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
<?php
/*
* This file is part of the NelmioApiDocBundle package.
*
* (c) Nelmio
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Nelmio\ApiDocBundle;
use Nelmio\ApiDocBundle\Describer\DescriberInterface;
use Nelmio\ApiDocBundle\Describer\ModelRegistryAwareInterface;
use Nelmio\ApiDocBundle\Model\ModelRegistry;
use Nelmio\ApiDocBundle\ModelDescriber\ModelDescriberInterface;
use Nelmio\ApiDocBundle\OpenApiPhp\ModelRegister;
use Nelmio\ApiDocBundle\OpenApiPhp\Util;
use OpenApi\Analysis;
use OpenApi\Annotations\OpenApi;
use OpenApi\Generator;
use OpenApi\Processors\ProcessorInterface;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Log\LoggerAwareTrait;
final class ApiDocGenerator
{
use LoggerAwareTrait;
/** @var OpenApi */
private $openApi;
/** @var iterable|DescriberInterface[] */
private $describers;
/** @var iterable|ModelDescriberInterface[] */
private $modelDescribers;
/** @var CacheItemPoolInterface|null */
private $cacheItemPool;
/** @var string|null */
private $cacheItemId;
/** @var string[] */
private $alternativeNames = [];
/** @var string[] */
private $mediaTypes = ['json'];
/**
* @var ?string
*/
private $openApiVersion = null;
/** @var Generator */
private $generator;
/**
* @param DescriberInterface[]|iterable $describers
* @param ModelDescriberInterface[]|iterable $modelDescribers
*/
public function __construct($describers, $modelDescribers, CacheItemPoolInterface $cacheItemPool = null, string $cacheItemId = null, Generator $generator = null)
{
$this->describers = $describers;
$this->modelDescribers = $modelDescribers;
$this->cacheItemPool = $cacheItemPool;
$this->cacheItemId = $cacheItemId;
$this->generator = $generator ?? new Generator($this->logger);
}
public function setAlternativeNames(array $alternativeNames)
{
$this->alternativeNames = $alternativeNames;
}
public function setMediaTypes(array $mediaTypes)
{
$this->mediaTypes = $mediaTypes;
}
public function setOpenApiVersion(?string $openApiVersion)
{
$this->openApiVersion = $openApiVersion;
}
public function generate(): OpenApi
{
if (null !== $this->openApi) {
return $this->openApi;
}
if ($this->cacheItemPool) {
$item = $this->cacheItemPool->getItem($this->cacheItemId ?? 'openapi_doc');
if ($item->isHit()) {
return $this->openApi = $item->get();
}
}
if ($this->openApiVersion) {
$this->generator->setVersion($this->openApiVersion);
}
$this->generator->setProcessors($this->getProcessors($this->generator));
$context = Util::createContext(['version' => $this->generator->getVersion()]);
$this->openApi = new OpenApi(['_context' => $context]);
$modelRegistry = new ModelRegistry($this->modelDescribers, $this->openApi, $this->alternativeNames);
if (null !== $this->logger) {
$modelRegistry->setLogger($this->logger);
}
foreach ($this->describers as $describer) {
if ($describer instanceof ModelRegistryAwareInterface) {
$describer->setModelRegistry($modelRegistry);
}
$describer->describe($this->openApi);
}
$analysis = new Analysis([], $context);
$analysis->addAnnotation($this->openApi, $context);
// Register model annotations
$modelRegister = new ModelRegister($modelRegistry, $this->mediaTypes);
$modelRegister($analysis);
// Calculate the associated schemas
$modelRegistry->registerSchemas();
$analysis->process($this->generator->getProcessors());
$analysis->validate();
if (isset($item)) {
$this->cacheItemPool->save($item->set($this->openApi));
}
return $this->openApi;
}
/**
* Get an array of processors that will be used to process the OpenApi object.
*
* @param Generator $generator The generator instance to get the standard processors from
*
* @return array<ProcessorInterface|callable> The array of processors
*/
private function getProcessors(Generator $generator): array
{
// Get the standard processors from the generator.
$processors = $generator->getProcessors();
// Remove OperationId processor as we use a lot of generated annotations which do not have enough information in their context
// to generate these ids properly.
// @see \Nelmio\ApiDocBundle\OpenApiPhp\Util::createContext
foreach ($processors as $key => $processor) {
if ($processor instanceof \OpenApi\Processors\OperationId) {
unset($processors[$key]);
}
}
return $processors;
}
}