-
Notifications
You must be signed in to change notification settings - Fork 7
/
AbstractSwaggerConfigurationLoader.php
188 lines (153 loc) · 5.89 KB
/
AbstractSwaggerConfigurationLoader.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
<?php
declare(strict_types=1);
/*
* This file is part of the SwaggerResolverBundle package.
*
* (c) Viktor Linkin <adrenalinkin@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Linkin\Bundle\SwaggerResolverBundle\Loader;
use EXSyst\Component\Swagger\Operation;
use EXSyst\Component\Swagger\Parameter;
use EXSyst\Component\Swagger\Path;
use EXSyst\Component\Swagger\Swagger;
use Linkin\Bundle\SwaggerResolverBundle\Collection\SchemaDefinitionCollection;
use Linkin\Bundle\SwaggerResolverBundle\Collection\SchemaOperationCollection;
use Linkin\Bundle\SwaggerResolverBundle\Exception\OperationNotFoundException;
use Linkin\Bundle\SwaggerResolverBundle\Merger\OperationParameterMerger;
use ReflectionClass;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\Routing\RouterInterface;
/**
* @author Viktor Linkin <adrenalinkin@gmail.com>
*/
abstract class AbstractSwaggerConfigurationLoader implements SwaggerConfigurationLoaderInterface
{
/**
* @var SchemaDefinitionCollection
*/
private $definitionCollection;
/**
* @var SchemaOperationCollection
*/
private $operationCollection;
/**
* @var OperationParameterMerger
*/
private $parameterMerger;
/**
* @var RouterInterface
*/
private $router;
/**
* @var string[][]
*/
private $mapPathToRouteName = [];
/**
* @var FileResource[]
*/
private $mapRouteNameToSourceFile = [];
public function __construct(OperationParameterMerger $parameterMerger, RouterInterface $router)
{
$this->parameterMerger = $parameterMerger;
$this->router = $router;
}
/**
* {@inheritdoc}
*/
public function getSchemaDefinitionCollection(): SchemaDefinitionCollection
{
if (!$this->definitionCollection) {
$this->registerCollections();
}
return $this->definitionCollection;
}
/**
* {@inheritdoc}
*/
public function getSchemaOperationCollection(): SchemaOperationCollection
{
if (!$this->operationCollection) {
$this->registerCollections();
}
return $this->operationCollection;
}
/**
* Load full configuration and returns Swagger object.
*/
abstract protected function loadConfiguration(): Swagger;
/**
* Add file resources for swagger definitions.
*/
abstract protected function registerDefinitionResources(SchemaDefinitionCollection $definitionCollection): void;
/**
* Add file resources for swagger operations.
*/
protected function registerOperationResources(SchemaOperationCollection $operationCollection): void
{
foreach ($operationCollection->getIterator() as $routeName => $methodList) {
$operationCollection->addSchemaResource($routeName, $this->mapRouteNameToSourceFile[$routeName]);
}
}
private function normalizeMethod(string $method): string
{
return strtolower($method);
}
/**
* Register collection according to loaded Swagger object.
*/
private function registerCollections(): void
{
$this->initRouteMaps();
$swaggerConfiguration = $this->loadConfiguration();
$this->definitionCollection = new SchemaDefinitionCollection();
$this->operationCollection = new SchemaOperationCollection();
foreach ($swaggerConfiguration->getDefinitions()->getIterator() as $definitionName => $definition) {
$this->definitionCollection->addSchema($definitionName, $definition);
}
$this->registerDefinitionResources($this->definitionCollection);
/** @var Path $pathObject */
foreach ($swaggerConfiguration->getPaths()->getIterator() as $path => $pathObject) {
/** @var Operation $operation */
foreach ($pathObject->getOperations() as $method => $operation) {
$method = $this->normalizeMethod($method);
$routeName = $this->mapPathToRouteName[$path][$method] ?? null;
if (null === $routeName) {
throw new OperationNotFoundException($path, $method);
}
$schema = $this->parameterMerger->merge($operation, $swaggerConfiguration->getDefinitions());
$this->operationCollection->addSchema($routeName, $method, $schema);
/** @var Parameter $parameter */
foreach ($operation->getParameters()->getIterator() as $parameter) {
$ref = $parameter->getSchema()->getRef();
if (!\is_string($ref)) {
continue;
}
$explodedName = explode('/', $ref);
$definitionName = end($explodedName);
foreach ($this->definitionCollection->getSchemaResources($definitionName) as $fileResource) {
$this->operationCollection->addSchemaResource($routeName, $fileResource);
}
}
}
}
$this->registerOperationResources($this->operationCollection);
}
private function initRouteMaps(): void
{
$this->mapPathToRouteName = [];
$this->mapRouteNameToSourceFile = [];
foreach ($this->router->getRouteCollection() as $routeName => $route) {
foreach ($route->getMethods() as $method) {
$defaults = $route->getDefaults();
$exploded = explode('::', $defaults['_controller']);
$controllerName = reset($exploded);
$fullClassName = (new ReflectionClass($controllerName))->getFileName();
$this->mapPathToRouteName[$route->getPath()][$this->normalizeMethod($method)] = $routeName;
$this->mapRouteNameToSourceFile[$routeName] = new FileResource($fullClassName);
}
}
}
}