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

Open in your IDE?
  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\Core\Metadata\Resource\ResourceMetadata;
  13. use ApiPlatform\Exception\RuntimeException;
  14. use ApiPlatform\OpenApi\Factory\OpenApiFactoryInterface;
  15. use ApiPlatform\OpenApi\Options;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  19. use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
  20. use Twig\Environment as TwigEnvironment;
  21. /**
  22. * Displays the swaggerui interface.
  23. *
  24. * @author Antoine Bluchet <soyuka@gmail.com>
  25. */
  26. final class SwaggerUiAction
  27. {
  28. private $twig;
  29. private $urlGenerator;
  30. private $normalizer;
  31. private $openApiFactory;
  32. private $openApiOptions;
  33. private $swaggerUiContext;
  34. private $formats;
  35. private $resourceMetadataFactory;
  36. private $oauthClientId;
  37. private $oauthClientSecret;
  38. private $oauthPkce;
  39. public function __construct($resourceMetadataFactory, ?TwigEnvironment $twig, UrlGeneratorInterface $urlGenerator, NormalizerInterface $normalizer, OpenApiFactoryInterface $openApiFactory, Options $openApiOptions, SwaggerUiContext $swaggerUiContext, array $formats = [], string $oauthClientId = null, string $oauthClientSecret = null, bool $oauthPkce = false)
  40. {
  41. $this->resourceMetadataFactory = $resourceMetadataFactory;
  42. $this->twig = $twig;
  43. $this->urlGenerator = $urlGenerator;
  44. $this->normalizer = $normalizer;
  45. $this->openApiFactory = $openApiFactory;
  46. $this->openApiOptions = $openApiOptions;
  47. $this->swaggerUiContext = $swaggerUiContext;
  48. $this->formats = $formats;
  49. $this->oauthClientId = $oauthClientId;
  50. $this->oauthClientSecret = $oauthClientSecret;
  51. $this->oauthPkce = $oauthPkce;
  52. if (null === $this->twig) {
  53. throw new \RuntimeException('The documentation cannot be displayed since the Twig bundle is not installed. Try running "composer require symfony/twig-bundle".');
  54. }
  55. }
  56. public function __invoke(Request $request)
  57. {
  58. $openApi = $this->openApiFactory->__invoke(['base_url' => $request->getBaseUrl() ?: '/']);
  59. $swaggerContext = [
  60. 'formats' => $this->formats,
  61. 'title' => $openApi->getInfo()->getTitle(),
  62. 'description' => $openApi->getInfo()->getDescription(),
  63. 'showWebby' => $this->swaggerUiContext->isWebbyShown(),
  64. 'swaggerUiEnabled' => $this->swaggerUiContext->isSwaggerUiEnabled(),
  65. 'reDocEnabled' => $this->swaggerUiContext->isRedocEnabled(),
  66. // FIXME: typo graphql => graphQl
  67. 'graphqlEnabled' => $this->swaggerUiContext->isGraphQlEnabled(),
  68. 'graphiQlEnabled' => $this->swaggerUiContext->isGraphiQlEnabled(),
  69. 'graphQlPlaygroundEnabled' => $this->swaggerUiContext->isGraphQlPlaygroundEnabled(),
  70. 'assetPackage' => $this->swaggerUiContext->getAssetPackage(),
  71. ];
  72. $swaggerData = [
  73. 'url' => $this->urlGenerator->generate('api_doc', ['format' => 'json']),
  74. 'spec' => $this->normalizer->normalize($openApi, 'json', []),
  75. 'oauth' => [
  76. 'enabled' => $this->openApiOptions->getOAuthEnabled(),
  77. 'type' => $this->openApiOptions->getOAuthType(),
  78. 'flow' => $this->openApiOptions->getOAuthFlow(),
  79. 'tokenUrl' => $this->openApiOptions->getOAuthTokenUrl(),
  80. 'authorizationUrl' => $this->openApiOptions->getOAuthAuthorizationUrl(),
  81. 'scopes' => $this->openApiOptions->getOAuthScopes(),
  82. 'clientId' => $this->oauthClientId,
  83. 'clientSecret' => $this->oauthClientSecret,
  84. 'pkce' => $this->oauthPkce,
  85. ],
  86. 'extraConfiguration' => $this->swaggerUiContext->getExtraConfiguration(),
  87. ];
  88. if ($request->isMethodSafe() && null !== $resourceClass = $request->attributes->get('_api_resource_class')) {
  89. $swaggerData['id'] = $request->attributes->get('id');
  90. $swaggerData['queryParameters'] = $request->query->all();
  91. $metadata = $this->resourceMetadataFactory->create($resourceClass);
  92. if ($metadata instanceof ResourceMetadata) {
  93. $swaggerData['shortName'] = $metadata->getShortName();
  94. if (null !== $collectionOperationName = $request->attributes->get('_api_collection_operation_name')) {
  95. $swaggerData['operationId'] = sprintf('%s%sCollection', $collectionOperationName, ucfirst($swaggerData['shortName']));
  96. } elseif (null !== $itemOperationName = $request->attributes->get('_api_item_operation_name')) {
  97. $swaggerData['operationId'] = sprintf('%s%sItem', $itemOperationName, ucfirst($swaggerData['shortName']));
  98. } elseif (null !== $subresourceOperationContext = $request->attributes->get('_api_subresource_context')) {
  99. $swaggerData['operationId'] = $subresourceOperationContext['operationId'];
  100. }
  101. } else {
  102. $swaggerData['shortName'] = $metadata[0]->getShortName();
  103. $swaggerData['operationId'] = $request->attributes->get('_api_operation_name');
  104. }
  105. [$swaggerData['path'], $swaggerData['method']] = $this->getPathAndMethod($swaggerData);
  106. }
  107. return new Response($this->twig->render('@ApiPlatform/SwaggerUi/index.html.twig', $swaggerContext + ['swagger_data' => $swaggerData]));
  108. }
  109. private function getPathAndMethod(array $swaggerData): array
  110. {
  111. foreach ($swaggerData['spec']['paths'] as $path => $operations) {
  112. foreach ($operations as $method => $operation) {
  113. if (($operation['operationId'] ?? null) === $swaggerData['operationId']) {
  114. return [$path, $method];
  115. }
  116. }
  117. }
  118. throw new RuntimeException(sprintf('The operation "%s" cannot be found in the Swagger specification.', $swaggerData['operationId']));
  119. }
  120. }
  121. class_alias(SwaggerUiAction::class, \ApiPlatform\Core\Bridge\Symfony\Bundle\SwaggerUi\SwaggerUiAction::class);