vendor/api-platform/core/src/Bridge/Symfony/Bundle/Action/SwaggerUiAction.php line 36

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\Core\Bridge\Symfony\Bundle\Action;
  12. use ApiPlatform\Core\Api\FormatsProviderInterface;
  13. use ApiPlatform\Core\Bridge\Symfony\Bundle\SwaggerUi\SwaggerUiAction as OpenApiSwaggerUiAction;
  14. use ApiPlatform\Core\Documentation\Documentation;
  15. use ApiPlatform\Core\Exception\RuntimeException;
  16. use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
  17. use ApiPlatform\Core\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;
  18. use ApiPlatform\Core\Util\RequestAttributesExtractor;
  19. use Symfony\Component\HttpFoundation\Request;
  20. use Symfony\Component\HttpFoundation\Response;
  21. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  22. use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
  23. use Twig\Environment as TwigEnvironment;
  24. /**
  25.  * Displays the documentation.
  26.  *
  27.  * @deprecated please refer to ApiPlatform\Core\Bridge\Symfony\Bundle\SwaggerUi\SwaggerUiAction for further changes
  28.  *
  29.  * @author Kévin Dunglas <dunglas@gmail.com>
  30.  */
  31. final class SwaggerUiAction
  32. {
  33.     private $resourceNameCollectionFactory;
  34.     private $resourceMetadataFactory;
  35.     private $normalizer;
  36.     private $twig;
  37.     private $urlGenerator;
  38.     private $title;
  39.     private $description;
  40.     private $version;
  41.     private $showWebby;
  42.     private $formats;
  43.     private $oauthEnabled;
  44.     private $oauthClientId;
  45.     private $oauthClientSecret;
  46.     private $oauthType;
  47.     private $oauthFlow;
  48.     private $oauthTokenUrl;
  49.     private $oauthAuthorizationUrl;
  50.     private $oauthScopes;
  51.     private $formatsProvider;
  52.     private $swaggerUiEnabled;
  53.     private $reDocEnabled;
  54.     private $graphqlEnabled;
  55.     private $graphiQlEnabled;
  56.     private $graphQlPlaygroundEnabled;
  57.     private $swaggerVersions;
  58.     private $swaggerUiAction;
  59.     private $assetPackage;
  60.     /**
  61.      * @param int[]      $swaggerVersions
  62.      * @param mixed|null $assetPackage
  63.      */
  64.     public function __construct(ResourceNameCollectionFactoryInterface $resourceNameCollectionFactoryResourceMetadataFactoryInterface $resourceMetadataFactoryNormalizerInterface $normalizerTwigEnvironment $twigUrlGeneratorInterface $urlGeneratorstring $title ''string $description ''string $version ''$formats = [], $oauthEnabled false$oauthClientId ''$oauthClientSecret ''$oauthType ''$oauthFlow ''$oauthTokenUrl ''$oauthAuthorizationUrl ''$oauthScopes = [], bool $showWebby truebool $swaggerUiEnabled falsebool $reDocEnabled falsebool $graphqlEnabled falsebool $graphiQlEnabled falsebool $graphQlPlaygroundEnabled false, array $swaggerVersions = [23], OpenApiSwaggerUiAction $swaggerUiAction null$assetPackage null)
  65.     {
  66.         $this->resourceNameCollectionFactory $resourceNameCollectionFactory;
  67.         $this->resourceMetadataFactory $resourceMetadataFactory;
  68.         $this->normalizer $normalizer;
  69.         $this->twig $twig;
  70.         $this->urlGenerator $urlGenerator;
  71.         $this->title $title;
  72.         $this->showWebby $showWebby;
  73.         $this->description $description;
  74.         $this->version $version;
  75.         $this->oauthEnabled $oauthEnabled;
  76.         $this->oauthClientId $oauthClientId;
  77.         $this->oauthClientSecret $oauthClientSecret;
  78.         $this->oauthType $oauthType;
  79.         $this->oauthFlow $oauthFlow;
  80.         $this->oauthTokenUrl $oauthTokenUrl;
  81.         $this->oauthAuthorizationUrl $oauthAuthorizationUrl;
  82.         $this->oauthScopes $oauthScopes;
  83.         $this->swaggerUiEnabled $swaggerUiEnabled;
  84.         $this->reDocEnabled $reDocEnabled;
  85.         $this->graphqlEnabled $graphqlEnabled;
  86.         $this->graphiQlEnabled $graphiQlEnabled;
  87.         $this->graphQlPlaygroundEnabled $graphQlPlaygroundEnabled;
  88.         $this->swaggerVersions $swaggerVersions;
  89.         $this->swaggerUiAction $swaggerUiAction;
  90.         $this->assetPackage $assetPackage;
  91.         if (null === $this->swaggerUiAction) {
  92.             @trigger_error(sprintf('The use of "%s" is deprecated since API Platform 2.6, use "%s" instead.'__CLASS__OpenApiSwaggerUiAction::class), \E_USER_DEPRECATED);
  93.         }
  94.         if (\is_array($formats)) {
  95.             $this->formats $formats;
  96.             return;
  97.         }
  98.         @trigger_error(sprintf('Passing an array or an instance of "%s" as 5th parameter of the constructor of "%s" is deprecated since API Platform 2.5, pass an array instead'FormatsProviderInterface::class, __CLASS__), \E_USER_DEPRECATED);
  99.         $this->formatsProvider $formats;
  100.     }
  101.     public function __invoke(Request $request)
  102.     {
  103.         if ($this->swaggerUiAction) {
  104.             return $this->swaggerUiAction->__invoke($request);
  105.         }
  106.         $attributes RequestAttributesExtractor::extractAttributes($request);
  107.         // BC check to be removed in 3.0
  108.         if (null === $this->formatsProvider) {
  109.             $formats $attributes $this
  110.                 ->resourceMetadataFactory
  111.                 ->create($attributes['resource_class'])
  112.                 ->getOperationAttribute($attributes'output_formats', [], true) : $this->formats;
  113.         } else {
  114.             $formats $this->formatsProvider->getFormatsFromAttributes($attributes);
  115.         }
  116.         $documentation = new Documentation($this->resourceNameCollectionFactory->create(), $this->title$this->description$this->version);
  117.         return new Response($this->twig->render('@ApiPlatform/SwaggerUi/index.html.twig'$this->getContext($request$documentation) + ['formats' => $formats]));
  118.     }
  119.     /**
  120.      * Gets the base Twig context.
  121.      */
  122.     private function getContext(Request $requestDocumentation $documentation): array
  123.     {
  124.         $context = [
  125.             'title' => $this->title,
  126.             'description' => $this->description,
  127.             'showWebby' => $this->showWebby,
  128.             'swaggerUiEnabled' => $this->swaggerUiEnabled,
  129.             'reDocEnabled' => $this->reDocEnabled,
  130.             'graphqlEnabled' => $this->graphqlEnabled,
  131.             'graphiQlEnabled' => $this->graphiQlEnabled,
  132.             'graphQlPlaygroundEnabled' => $this->graphQlPlaygroundEnabled,
  133.             'assetPackage' => $this->assetPackage,
  134.         ];
  135.         $swaggerContext = ['spec_version' => $request->query->getInt('spec_version'$this->swaggerVersions[0] ?? 2)];
  136.         if ('' !== $baseUrl $request->getBaseUrl()) {
  137.             $swaggerContext['base_url'] = $baseUrl;
  138.         }
  139.         $swaggerData = [
  140.             'url' => $this->urlGenerator->generate('api_doc', ['format' => 'json']),
  141.             'spec' => $this->normalizer->normalize($documentation'json'$swaggerContext),
  142.         ];
  143.         $swaggerData['oauth'] = [
  144.             'enabled' => $this->oauthEnabled,
  145.             'clientId' => $this->oauthClientId,
  146.             'clientSecret' => $this->oauthClientSecret,
  147.             'type' => $this->oauthType,
  148.             'flow' => $this->oauthFlow,
  149.             'tokenUrl' => $this->oauthTokenUrl,
  150.             'authorizationUrl' => $this->oauthAuthorizationUrl,
  151.             'scopes' => $this->oauthScopes,
  152.         ];
  153.         if ($request->isMethodSafe() && null !== $resourceClass $request->attributes->get('_api_resource_class')) {
  154.             $swaggerData['id'] = $request->attributes->get('id');
  155.             $swaggerData['queryParameters'] = $request->query->all();
  156.             $metadata $this->resourceMetadataFactory->create($resourceClass);
  157.             $swaggerData['shortName'] = $metadata->getShortName();
  158.             if (null !== $collectionOperationName $request->attributes->get('_api_collection_operation_name')) {
  159.                 $swaggerData['operationId'] = sprintf('%s%sCollection'$collectionOperationNameucfirst($swaggerData['shortName']));
  160.             } elseif (null !== $itemOperationName $request->attributes->get('_api_item_operation_name')) {
  161.                 $swaggerData['operationId'] = sprintf('%s%sItem'$itemOperationNameucfirst($swaggerData['shortName']));
  162.             } elseif (null !== $subresourceOperationContext $request->attributes->get('_api_subresource_context')) {
  163.                 $swaggerData['operationId'] = $subresourceOperationContext['operationId'];
  164.             }
  165.             [$swaggerData['path'], $swaggerData['method']] = $this->getPathAndMethod($swaggerData);
  166.         }
  167.         return $context + ['swagger_data' => $swaggerData];
  168.     }
  169.     private function getPathAndMethod(array $swaggerData): array
  170.     {
  171.         foreach ($swaggerData['spec']['paths'] as $path => $operations) {
  172.             foreach ($operations as $method => $operation) {
  173.                 if ($operation['operationId'] === $swaggerData['operationId']) {
  174.                     return [$path$method];
  175.                 }
  176.             }
  177.         }
  178.         throw new RuntimeException(sprintf('The operation "%s" cannot be found in the Swagger specification.'$swaggerData['operationId']));
  179.     }
  180. }