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

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