sunrise-php /
http-router
| 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\Helper\TemplateRenderer; |
||
| 25 | use Sunrise\Http\Router\OpenApi\SwaggerConfiguration; |
||
| 26 | use Throwable; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @since 3.0.0 |
||
| 30 | */ |
||
| 31 | #[GetRoute(self::class, self::ROUTE_PATH)] |
||
| 32 | #[Priority(-1)] |
||
| 33 | final class SwaggerController implements RequestHandlerInterface |
||
| 34 | { |
||
| 35 | public const ROUTE_PATH = '/swagger.html'; |
||
| 36 | |||
| 37 | public const CSS_URLS_VAR_NAME = 'css_urls'; |
||
| 38 | public const JS_URLS_VAR_NAME = 'js_urls'; |
||
| 39 | public const OPENAPI_URI_VAR_NAME = 'openapi_uri'; |
||
| 40 | |||
| 41 | 1 | public function __construct( |
|
| 42 | private readonly SwaggerConfiguration $swaggerConfiguration, |
||
| 43 | private readonly ResponseFactoryInterface $responseFactory, |
||
| 44 | private readonly StreamFactoryInterface $streamFactory, |
||
| 45 | ) { |
||
| 46 | 1 | } |
|
| 47 | |||
| 48 | /** |
||
| 49 | * @inheritDoc |
||
| 50 | * |
||
| 51 | * @throws Throwable |
||
| 52 | */ |
||
| 53 | 1 | public function handle(ServerRequestInterface $request): ResponseInterface |
|
| 54 | { |
||
| 55 | 1 | $responseBody = $this->streamFactory->createStream( |
|
| 56 | 1 | TemplateRenderer::renderTemplate( |
|
| 57 | 1 | filename: $this->swaggerConfiguration->templateFilename, |
|
| 58 | 1 | variables: [ |
|
| 59 | 1 | ...$this->swaggerConfiguration->templateVariables, |
|
| 60 | 1 | self::CSS_URLS_VAR_NAME => $this->swaggerConfiguration->cssUrls, |
|
| 61 | 1 | self::JS_URLS_VAR_NAME => $this->swaggerConfiguration->jsUrls, |
|
| 62 | 1 | self::OPENAPI_URI_VAR_NAME => $this->swaggerConfiguration->openapiUri, |
|
| 63 | 1 | ], |
|
| 64 | 1 | ), |
|
| 65 | 1 | ); |
|
| 66 | |||
| 67 | 1 | $responseContentType = 'text/html; charset=UTF-8'; |
|
| 68 | |||
| 69 | 1 | return $this->responseFactory->createResponse() |
|
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 70 | 1 | ->withHeader(HeaderName::CONTENT_TYPE, $responseContentType) |
|
| 71 | 1 | ->withBody($responseBody); |
|
| 72 | } |
||
| 73 | } |
||
| 74 |