vendor/nelmio/api-doc-bundle/Render/RenderOpenApi.php line 51

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\Render;
  11. use Nelmio\ApiDocBundle\Exception\RenderInvalidArgumentException;
  12. use OpenApi\Annotations\OpenApi;
  13. use OpenApi\Annotations\Server;
  14. use OpenApi\Context;
  15. use OpenApi\Generator;
  16. use Psr\Container\ContainerInterface;
  17. use Symfony\Component\HttpFoundation\Request;
  18. class RenderOpenApi
  19. {
  20.     public const HTML 'html';
  21.     public const JSON 'json';
  22.     public const YAML 'yaml';
  23.     /** @var ContainerInterface */
  24.     private $generatorLocator;
  25.     /** @var array<string, OpenApiRenderer|null> */
  26.     private $openApiRenderers = [];
  27.     public function __construct(ContainerInterface $generatorLocator, ?OpenApiRenderer ...$openApiRenderers)
  28.     {
  29.         $this->generatorLocator $generatorLocator;
  30.         foreach ($openApiRenderers as $openApiRenderer) {
  31.             if (null === $openApiRenderer) {
  32.                 continue;
  33.             }
  34.             $this->openApiRenderers[$openApiRenderer->getFormat()] = $openApiRenderer;
  35.         }
  36.     }
  37.     public function getAvailableFormats(): array
  38.     {
  39.         return array_keys($this->openApiRenderers);
  40.     }
  41.     public function renderFromRequest(Request $requeststring $format$area, array $extraOptions = [])
  42.     {
  43.         $options = [];
  44.         if ('' !== $request->getBaseUrl()) {
  45.             $options += [
  46.                 'fallback_url' => $request->getSchemeAndHttpHost().$request->getBaseUrl(),
  47.             ];
  48.         }
  49.         $options += $extraOptions;
  50.         return $this->render($format$area$options);
  51.     }
  52.     /**
  53.      * @throws InvalidArgumentException If the area to dump is not valid
  54.      */
  55.     public function render(string $formatstring $area, array $options = []): string
  56.     {
  57.         if (!$this->generatorLocator->has($area)) {
  58.             throw new RenderInvalidArgumentException(sprintf('Area "%s" is not supported.'$area));
  59.         } elseif (!array_key_exists($format$this->openApiRenderers)) {
  60.             throw new RenderInvalidArgumentException(sprintf('Format "%s" is not supported.'$format));
  61.         }
  62.         /** @var OpenApi $spec */
  63.         $spec $this->generatorLocator->get($area)->generate();
  64.         $tmpServers $spec->servers;
  65.         try {
  66.             $spec->servers $this->getServersFromOptions($spec$options);
  67.             return $this->openApiRenderers[$format]->render($spec$options);
  68.         } finally {
  69.             $spec->servers $tmpServers// Restore original value as we should not modify OpenApi object from the generator
  70.         }
  71.     }
  72.     private function getServersFromOptions(OpenApi $spec, array $options)
  73.     {
  74.         if (array_key_exists('server_url'$options) && $options['server_url']) {
  75.             return [new Server(['url' => $options['server_url'], '_context' => new Context()])];
  76.         }
  77.         if (Generator::UNDEFINED !== $spec->servers) {
  78.             return $spec->servers;
  79.         }
  80.         if (array_key_exists('fallback_url'$options) && $options['fallback_url']) {
  81.             return [new Server(['url' => $options['fallback_url'], '_context' => new Context()])];
  82.         }
  83.         return Generator::UNDEFINED;
  84.     }
  85. }