/
YamlDocumentationController.php
42 lines (35 loc) · 1.23 KB
/
YamlDocumentationController.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
<?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\Controller;
use Nelmio\ApiDocBundle\Render\RenderOpenApi;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
final class YamlDocumentationController
{
private RenderOpenApi $renderOpenApi;
public function __construct(RenderOpenApi $renderOpenApi)
{
$this->renderOpenApi = $renderOpenApi;
}
public function __invoke(Request $request, string $area = 'default'): Response
{
try {
$response = new Response(
$this->renderOpenApi->renderFromRequest($request, RenderOpenApi::YAML, $area),
Response::HTTP_OK,
['Content-Type' => 'text/x-yaml']
);
return $response->setCharset('UTF-8');
} catch (\InvalidArgumentException $e) {
throw new BadRequestHttpException(sprintf('Area "%s" is not supported as it isn\'t defined in config.', $area));
}
}
}