-
Notifications
You must be signed in to change notification settings - Fork 20
/
OpenApiControllerService.php
67 lines (58 loc) · 1.87 KB
/
OpenApiControllerService.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
<?php
declare(strict_types=1);
namespace SwaggerBake\Lib\Service;
use Cake\Http\ServerRequest;
use SwaggerBake\Lib\Configuration;
use SwaggerBake\Lib\Swagger;
use SwaggerBake\Lib\SwaggerFactory;
class OpenApiControllerService
{
private Configuration $config;
private Swagger $swagger;
/**
* @param \SwaggerBake\Lib\Configuration|null $config Swagger Configuration, created if null
* @param \SwaggerBake\Lib\Swagger|null $swagger Swagger, created if null
* @throws \ReflectionException
*/
public function __construct(
?Configuration $config = null,
?Swagger $swagger = null,
) {
$this->config = $config ?? new Configuration();
$this->swagger = $swagger ?? (new SwaggerFactory($this->config))->create();
}
/**
* Rebuilds OpenAPI if hot reload is enabled and logs warnings if debug is enabled
*
* @return void
*/
public function build(): void
{
if ($this->config->isHotReload()) {
$output = $this->config->getJson();
$this->swagger->writeFile($output);
}
}
/**
* @return \SwaggerBake\Lib\Configuration
*/
public function getConfig(): Configuration
{
return $this->config;
}
/**
* Get the requested document type (swagger or redoc). If none is present in the docType query parameter use
* the value from swagger_bake config.
*
* @param \Cake\Http\ServerRequest $request CakePHP ServerRequest
* @return string
*/
public function getDocType(ServerRequest $request): string
{
if (empty($request->getQuery('doctype')) || !is_string($request->getQuery('doctype'))) {
return $this->config->getDocType();
}
$docType = h(strtolower($request->getQuery('doctype')));
return in_array($docType, ['swagger','redoc']) ? $docType : 'swagger';
}
}