vendor/api-platform/core/src/Symfony/Bundle/SwaggerUi/SwaggerUiAction.php line 32

  1. <?php
  2. /*
  3.  * This file is part of the API Platform project.
  4.  *
  5.  * (c) Kévin Dunglas <dunglas@gmail.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace ApiPlatform\Symfony\Bundle\SwaggerUi;
  12. use ApiPlatform\Exception\RuntimeException;
  13. use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
  14. use ApiPlatform\OpenApi\Factory\OpenApiFactoryInterface;
  15. use ApiPlatform\OpenApi\Options;
  16. use ApiPlatform\OpenApi\Serializer\NormalizeOperationNameTrait;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Component\HttpFoundation\Response;
  19. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  20. use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
  21. use Twig\Environment as TwigEnvironment;
  22. /**
  23.  * Displays the swaggerui interface.
  24.  *
  25.  * @author Antoine Bluchet <soyuka@gmail.com>
  26.  */
  27. final class SwaggerUiAction
  28. {
  29.     use NormalizeOperationNameTrait;
  30.     public function __construct(private readonly ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory, private readonly ?TwigEnvironment $twig, private readonly UrlGeneratorInterface $urlGenerator, private readonly NormalizerInterface $normalizer, private readonly OpenApiFactoryInterface $openApiFactory, private readonly Options $openApiOptions, private readonly SwaggerUiContext $swaggerUiContext, private readonly array $formats = [], private readonly ?string $oauthClientId null, private readonly ?string $oauthClientSecret null, private readonly bool $oauthPkce false)
  31.     {
  32.         if (null === $this->twig) {
  33.             throw new \RuntimeException('The documentation cannot be displayed since the Twig bundle is not installed. Try running "composer require symfony/twig-bundle".');
  34.         }
  35.     }
  36.     public function __invoke(Request $request): Response
  37.     {
  38.         $openApi $this->openApiFactory->__invoke(['base_url' => $request->getBaseUrl() ?: '/']);
  39.         $swaggerContext = [
  40.             'formats' => $this->formats,
  41.             'title' => $openApi->getInfo()->getTitle(),
  42.             'description' => $openApi->getInfo()->getDescription(),
  43.             'showWebby' => $this->swaggerUiContext->isWebbyShown(),
  44.             'swaggerUiEnabled' => $this->swaggerUiContext->isSwaggerUiEnabled(),
  45.             'reDocEnabled' => $this->swaggerUiContext->isRedocEnabled(),
  46.             'graphQlEnabled' => $this->swaggerUiContext->isGraphQlEnabled(),
  47.             'graphiQlEnabled' => $this->swaggerUiContext->isGraphiQlEnabled(),
  48.             'graphQlPlaygroundEnabled' => $this->swaggerUiContext->isGraphQlPlaygroundEnabled(),
  49.             'assetPackage' => $this->swaggerUiContext->getAssetPackage(),
  50.         ];
  51.         $swaggerData = [
  52.             'url' => $this->urlGenerator->generate('api_doc', ['format' => 'json']),
  53.             'spec' => $this->normalizer->normalize($openApi'json', []),
  54.             'oauth' => [
  55.                 'enabled' => $this->openApiOptions->getOAuthEnabled(),
  56.                 'type' => $this->openApiOptions->getOAuthType(),
  57.                 'flow' => $this->openApiOptions->getOAuthFlow(),
  58.                 'tokenUrl' => $this->openApiOptions->getOAuthTokenUrl(),
  59.                 'authorizationUrl' => $this->openApiOptions->getOAuthAuthorizationUrl(),
  60.                 'scopes' => $this->openApiOptions->getOAuthScopes(),
  61.                 'clientId' => $this->oauthClientId,
  62.                 'clientSecret' => $this->oauthClientSecret,
  63.                 'pkce' => $this->oauthPkce,
  64.             ],
  65.             'extraConfiguration' => $this->swaggerUiContext->getExtraConfiguration(),
  66.         ];
  67.         if ($request->isMethodSafe() && null !== $resourceClass $request->attributes->get('_api_resource_class')) {
  68.             $swaggerData['id'] = $request->attributes->get('id');
  69.             $swaggerData['queryParameters'] = $request->query->all();
  70.             $metadata $this->resourceMetadataFactory->create($resourceClass)->getOperation($request->attributes->get('_api_operation_name'));
  71.             $swaggerData['shortName'] = $metadata->getShortName();
  72.             $swaggerData['operationId'] = $this->normalizeOperationName($metadata->getName());
  73.             [$swaggerData['path'], $swaggerData['method']] = $this->getPathAndMethod($swaggerData);
  74.         }
  75.         return new Response($this->twig->render('@ApiPlatform/SwaggerUi/index.html.twig'$swaggerContext + ['swagger_data' => $swaggerData]));
  76.     }
  77.     private function getPathAndMethod(array $swaggerData): array
  78.     {
  79.         foreach ($swaggerData['spec']['paths'] as $path => $operations) {
  80.             foreach ($operations as $method => $operation) {
  81.                 if (($operation['operationId'] ?? null) === $swaggerData['operationId']) {
  82.                     return [$path$method];
  83.                 }
  84.             }
  85.         }
  86.         throw new RuntimeException(sprintf('The operation "%s" cannot be found in the Swagger specification.'$swaggerData['operationId']));
  87.     }
  88. }