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

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\ApiDocGenerator;
  12. use Psr\Container\ContainerInterface;
  13. use Symfony\Component\DependencyInjection\ServiceLocator;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
  17. final class SwaggerUiController
  18. {
  19.     private $generatorLocator;
  20.     private $twig;
  21.     /**
  22.      * @param ContainerInterface $generatorLocator
  23.      */
  24.     public function __construct($generatorLocator, \Twig_Environment $twig)
  25.     {
  26.         if (!$generatorLocator instanceof ContainerInterface) {
  27.             if (!$generatorLocator instanceof ApiDocGenerator) {
  28.                 throw new \InvalidArgumentException(sprintf('Providing an instance of "%s" to "%s" is not supported.'get_class($generatorLocator), __METHOD__));
  29.             }
  30.             @trigger_error(sprintf('Providing an instance of "%s" to "%s()" is deprecated since version 3.1. Provide it an instance of "%s" instead.'ApiDocGenerator::class, __METHOD__ContainerInterface::class), E_USER_DEPRECATED);
  31.             $generatorLocator = new ServiceLocator(['default' => function () use ($generatorLocator): ApiDocGenerator {
  32.                 return $generatorLocator;
  33.             }]);
  34.         }
  35.         $this->generatorLocator $generatorLocator;
  36.         $this->twig $twig;
  37.     }
  38.     public function __invoke(Request $request$area 'default')
  39.     {
  40.         if (!$this->generatorLocator->has($area)) {
  41.             throw new BadRequestHttpException(sprintf('Area "%s" is not supported.'$area));
  42.         }
  43.         $spec $this->generatorLocator->get($area)->generate()->toArray();
  44.         if ('' !== $request->getBaseUrl()) {
  45.             $spec['basePath'] = $request->getBaseUrl();
  46.         }
  47.         return new Response(
  48.             $this->twig->render('@NelmioApiDoc/SwaggerUi/index.html.twig', ['swagger_data' => ['spec' => $spec]]),
  49.             Response::HTTP_OK,
  50.             ['Content-Type' => 'text/html']
  51.         );
  52.     }
  53. }