vendor/nelmio/api-doc-bundle/src/Controller/SwaggerUiController.php line 20

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the NelmioApiDocBundle package.
  4.  *
  5.  * (c) Nelmio
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Nelmio\ApiDocBundle\Controller;
  11. use Nelmio\ApiDocBundle\Exception\RenderInvalidArgumentException;
  12. use Nelmio\ApiDocBundle\Render\RenderOpenApi;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
  16. final class SwaggerUiController
  17. {
  18.     private RenderOpenApi $renderOpenApi;
  19.     private string $uiRenderer;
  20.     public function __construct(RenderOpenApi $renderOpenApistring $uiRenderer)
  21.     {
  22.         $this->renderOpenApi $renderOpenApi;
  23.         $this->uiRenderer $uiRenderer;
  24.     }
  25.     public function __invoke(Request $requeststring $area 'default'): Response
  26.     {
  27.         try {
  28.             $response = new Response(
  29.                 $this->renderOpenApi->renderFromRequest($requestRenderOpenApi::HTML$area, [
  30.                     'ui_renderer' => $this->uiRenderer,
  31.                 ]),
  32.                 Response::HTTP_OK,
  33.                 ['Content-Type' => 'text/html']
  34.             );
  35.             return $response->setCharset('UTF-8');
  36.         } catch (RenderInvalidArgumentException $e) {
  37.             $advice '';
  38.             if (false !== strpos($area'.json')) {
  39.                 $advice ' Since the area provided contains `.json`, the issue is likely caused by route priorities. Try switching the Swagger UI / the json documentation routes order.';
  40.             }
  41.             throw new BadRequestHttpException(sprintf('Area "%s" is not supported as it isn\'t defined in config.%s'$area$advice), $e);
  42.         }
  43.     }
  44. }