1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* It's free open-source software released under the MIT License. |
5
|
|
|
* |
6
|
|
|
* @author Anatoly Nekhay <[email protected]> |
7
|
|
|
* @copyright Copyright (c) 2018, Anatoly Nekhay |
8
|
|
|
* @license https://github.com/sunrise-php/http-router/blob/master/LICENSE |
9
|
|
|
* @link https://github.com/sunrise-php/http-router |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Sunrise\Http\Router\OpenApi\Controller; |
15
|
|
|
|
16
|
|
|
use Psr\Http\Message\ResponseFactoryInterface; |
17
|
|
|
use Psr\Http\Message\ResponseInterface; |
18
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
19
|
|
|
use Psr\Http\Message\StreamFactoryInterface; |
20
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
21
|
|
|
use Sunrise\Http\Router\Annotation\GetRoute; |
22
|
|
|
use Sunrise\Http\Router\Annotation\Priority; |
23
|
|
|
use Sunrise\Http\Router\Dictionary\HeaderName; |
24
|
|
|
use Sunrise\Http\Router\OpenApi\OpenApiConfiguration; |
25
|
|
|
use Sunrise\Http\Router\OpenApi\OpenApiDocumentManagerInterface; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @since 3.0.0 |
29
|
|
|
*/ |
30
|
|
|
#[GetRoute(self::class, self::ROUTE_PATH)] |
31
|
|
|
#[Priority(-1)] |
32
|
|
|
final class OpenApiController implements RequestHandlerInterface |
33
|
|
|
{ |
34
|
|
|
public const ROUTE_PATH = '/openapi'; |
35
|
|
|
|
36
|
2 |
|
public function __construct( |
37
|
|
|
private readonly OpenApiConfiguration $openApiConfiguration, |
38
|
|
|
private readonly OpenApiDocumentManagerInterface $openApiDocumentManager, |
39
|
|
|
private readonly ResponseFactoryInterface $responseFactory, |
40
|
|
|
private readonly StreamFactoryInterface $streamFactory, |
41
|
|
|
) { |
42
|
2 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @inheritDoc |
46
|
|
|
*/ |
47
|
2 |
|
public function handle(ServerRequestInterface $request): ResponseInterface |
48
|
|
|
{ |
49
|
2 |
|
$responseBody = $this->streamFactory->createStreamFromResource( |
50
|
2 |
|
$this->openApiDocumentManager->openDocument(), |
51
|
2 |
|
); |
52
|
|
|
|
53
|
1 |
|
$responseContentType = $this->openApiConfiguration->documentMediaType->getIdentifier(); |
54
|
1 |
|
$responseContentType .= '; charset=UTF-8'; // It should be a text data type... |
55
|
|
|
|
56
|
1 |
|
return $this->responseFactory->createResponse() |
|
|
|
|
57
|
1 |
|
->withHeader(HeaderName::CONTENT_TYPE, $responseContentType) |
58
|
1 |
|
->withBody($responseBody); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|