1 | <?php |
||
23 | class Swagger |
||
24 | { |
||
25 | |||
26 | /** |
||
27 | * @var \Graviton\RestBundle\Service\RestUtils |
||
28 | */ |
||
29 | private $restUtils; |
||
30 | |||
31 | /** |
||
32 | * @var SchemaModel |
||
33 | */ |
||
34 | private $schemaModel; |
||
35 | |||
36 | /** |
||
37 | * @var SchemaUtils |
||
38 | */ |
||
39 | private $schemaUtils; |
||
40 | |||
41 | /** |
||
42 | * @var CoreUtils |
||
43 | */ |
||
44 | private $coreUtils; |
||
45 | |||
46 | /** |
||
47 | * Constructor |
||
48 | * |
||
49 | * @param RestUtils $restUtils rest utils |
||
50 | * @param SchemaModel $schemaModel schema model instance |
||
51 | * @param SchemaUtils $schemaUtils schema utils |
||
52 | * @param CoreUtils $coreUtils coreUtils |
||
53 | */ |
||
54 | 2 | public function __construct( |
|
65 | |||
66 | /** |
||
67 | * Returns the swagger spec as array |
||
68 | * |
||
69 | * @return array Swagger spec |
||
|
|||
70 | */ |
||
71 | public function getSwaggerSpec() |
||
72 | { |
||
73 | $ret = $this->getBasicStructure(); |
||
74 | $routingMap = $this->restUtils->getServiceRoutingMap(); |
||
75 | $paths = array(); |
||
76 | |||
77 | foreach ($routingMap as $contName => $routes) { |
||
78 | list(, $bundle,, $document) = explode('.', $contName); |
||
79 | |||
80 | /** @var Route $route */ |
||
81 | foreach ($routes as $routeName => $route) { |
||
82 | $routeMethod = strtolower($route->getMethods()[0]); |
||
83 | |||
84 | if ($routeMethod == 'options') { |
||
85 | continue; |
||
86 | } |
||
87 | |||
88 | // skip /schema/ stuff |
||
89 | if (strpos($route->getPath(), '/schema/') !== false) { |
||
90 | list($pattern, $method, $data) = $this->getSchemaRoutes($route); |
||
91 | $paths[$pattern][$method] = $data; |
||
92 | continue; |
||
93 | } |
||
94 | |||
95 | /** @var \Graviton\RestBundle\Model\DocumentModel $thisModel */ |
||
96 | $thisModel = $this->restUtils->getModelFromRoute($route); |
||
97 | if ($thisModel === false) { |
||
98 | throw new \LogicException( |
||
99 | sprintf( |
||
100 | 'Could not resolve route "%s" to model', |
||
101 | $routeName |
||
102 | ) |
||
103 | ); |
||
104 | } |
||
105 | |||
106 | $entityClassName = str_replace('\\', '', get_class($thisModel)); |
||
107 | |||
108 | $schema = $this->schemaUtils->getModelSchema($entityClassName, $thisModel, false); |
||
109 | |||
110 | $ret['definitions'][$entityClassName] = json_decode( |
||
111 | $this->restUtils->serializeContent($schema), |
||
112 | true |
||
113 | ); |
||
114 | |||
115 | $isCollectionRequest = true; |
||
116 | if (in_array('id', array_keys($route->getRequirements())) === true) { |
||
117 | $isCollectionRequest = false; |
||
118 | } |
||
119 | |||
120 | $thisPattern = $route->getPath(); |
||
121 | $entityName = ucfirst($document); |
||
122 | |||
123 | $thisPath = $this->getBasicPathStructure( |
||
124 | $isCollectionRequest, |
||
125 | $entityName, |
||
126 | $entityClassName, |
||
127 | (string) $schema->getProperty('id')->getType() |
||
128 | ); |
||
129 | |||
130 | $thisPath['tags'] = $this->getPathTags($route); |
||
131 | $thisPath['operationId'] = $routeName; |
||
132 | $thisPath['summary'] = $this->getSummary($routeMethod, $isCollectionRequest, $entityName); |
||
133 | |||
134 | // post body stuff |
||
135 | if ($routeMethod == 'put' || $routeMethod == 'post') { |
||
136 | // special handling for POST/PUT.. we need to have 2 schemas, one for response, one for request.. |
||
137 | // we don't want to have ID in the request body within those requests do we.. |
||
138 | // an exception is when id is required.. |
||
139 | $incomingEntitySchema = $entityClassName; |
||
140 | if (is_null($schema->getRequired()) || !in_array('id', $schema->getRequired())) { |
||
141 | $incomingEntitySchema = $incomingEntitySchema . 'Incoming'; |
||
142 | $incomingSchema = clone $schema; |
||
143 | $incomingSchema->removeProperty('id'); |
||
144 | $ret['definitions'][$incomingEntitySchema] = json_decode( |
||
145 | $this->restUtils->serializeContent($incomingSchema), |
||
146 | true |
||
147 | ); |
||
148 | } |
||
149 | |||
150 | $thisPath['parameters'][] = array( |
||
151 | 'name' => $bundle, |
||
152 | 'in' => 'body', |
||
153 | 'description' => 'Post', |
||
154 | 'required' => true, |
||
155 | 'schema' => array('$ref' => '#/definitions/' . $incomingEntitySchema) |
||
156 | ); |
||
157 | |||
158 | if ($routeMethod == 'post') { |
||
159 | $thisPath['responses'][201] = $thisPath['responses'][200]; |
||
160 | unset($thisPath['responses'][200]); |
||
161 | } |
||
162 | |||
163 | // add error responses.. |
||
164 | $thisPath['responses'][400] = array( |
||
165 | 'description' => 'Bad request', |
||
166 | 'schema' => array( |
||
167 | 'type' => 'object' |
||
168 | ) |
||
169 | ); |
||
170 | } |
||
171 | |||
172 | $paths[$thisPattern][$routeMethod] = $thisPath; |
||
173 | } |
||
174 | } |
||
175 | |||
176 | $ret['definitions']['SchemaModel'] = $this->schemaModel->getSchema(); |
||
177 | |||
178 | ksort($paths); |
||
179 | $ret['paths'] = $paths; |
||
180 | |||
181 | return $ret; |
||
182 | } |
||
183 | |||
184 | /** |
||
185 | * Basic structure of the spec |
||
186 | * |
||
187 | * @return array Basic structure |
||
188 | */ |
||
189 | private function getBasicStructure() |
||
205 | |||
206 | /** |
||
207 | * Return the basic structure of a path element |
||
208 | * |
||
209 | * @param bool $isCollectionRequest if collection request |
||
210 | * @param string $entityName entity name |
||
211 | * @param string $entityClassName class name |
||
212 | * @param string $idType type of id field |
||
213 | * |
||
214 | * @return array Path spec |
||
215 | */ |
||
216 | protected function getBasicPathStructure($isCollectionRequest, $entityName, $entityClassName, $idType) |
||
257 | |||
258 | /** |
||
259 | * Returns the tags (which influences the grouping visually) for a given route |
||
260 | * |
||
261 | * @param Route $route route |
||
262 | * @param int $part part of route to use for generating a tag |
||
263 | * |
||
264 | * @return array Array of tags.. |
||
265 | */ |
||
266 | protected function getPathTags(Route $route, $part = 1) |
||
275 | |||
276 | /** |
||
277 | * Returns a meaningful summary depending on certain conditions |
||
278 | * |
||
279 | * @param string $method Method |
||
280 | * @param bool $isCollectionRequest If collection request |
||
281 | * @param string $entityName Name of entity |
||
282 | * |
||
283 | * @return string summary |
||
284 | */ |
||
285 | protected function getSummary($method, $isCollectionRequest, $entityName) |
||
308 | |||
309 | /** |
||
310 | * @param Route $route route |
||
311 | * |
||
312 | * @return array |
||
313 | */ |
||
314 | protected function getSchemaRoutes(Route $route) |
||
340 | } |
||
341 |
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.