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

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.      * @param mixed      $formats
  69.      * @param mixed      $oauthEnabled
  70.      * @param mixed      $oauthClientId
  71.      * @param mixed      $oauthClientSecret
  72.      * @param mixed      $oauthType
  73.      * @param mixed      $oauthFlow
  74.      * @param mixed      $oauthTokenUrl
  75.      * @param mixed      $oauthAuthorizationUrl
  76.      * @param mixed      $oauthScopes
  77.      * @param mixed      $resourceMetadataFactory
  78.      */
  79.     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)
  80.     {
  81.         $this->resourceNameCollectionFactory $resourceNameCollectionFactory;
  82.         $this->resourceMetadataFactory $resourceMetadataFactory;
  83.         $this->normalizer $normalizer;
  84.         $this->twig $twig;
  85.         $this->urlGenerator $urlGenerator;
  86.         $this->title $title;
  87.         $this->showWebby $showWebby;
  88.         $this->description $description;
  89.         $this->version $version;
  90.         $this->oauthEnabled $oauthEnabled;
  91.         $this->oauthClientId $oauthClientId;
  92.         $this->oauthClientSecret $oauthClientSecret;
  93.         $this->oauthType $oauthType;
  94.         $this->oauthFlow $oauthFlow;
  95.         $this->oauthTokenUrl $oauthTokenUrl;
  96.         $this->oauthAuthorizationUrl $oauthAuthorizationUrl;
  97.         $this->oauthScopes $oauthScopes;
  98.         $this->swaggerUiEnabled $swaggerUiEnabled;
  99.         $this->reDocEnabled $reDocEnabled;
  100.         $this->graphqlEnabled $graphqlEnabled;
  101.         $this->graphiQlEnabled $graphiQlEnabled;
  102.         $this->graphQlPlaygroundEnabled $graphQlPlaygroundEnabled;
  103.         $this->swaggerVersions $swaggerVersions;
  104.         $this->swaggerUiAction $swaggerUiAction;
  105.         $this->swaggerUiExtraConfiguration $swaggerUiExtraConfiguration;
  106.         $this->assetPackage $assetPackage;
  107.         $this->oauthPkce $oauthPkce;
  108.         if (null === $this->twig) {
  109.             throw new \RuntimeException('The documentation cannot be displayed since the Twig bundle is not installed. Try running "composer require symfony/twig-bundle".');
  110.         }
  111.         if (null === $this->swaggerUiAction) {
  112.             @trigger_error(sprintf('The use of "%s" is deprecated since API Platform 2.6, use "%s" instead.'__CLASS__OpenApiSwaggerUiAction::class), \E_USER_DEPRECATED);
  113.         }
  114.         if (\is_array($formats)) {
  115.             $this->formats $formats;
  116.             return;
  117.         }
  118.         @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);
  119.         $this->formatsProvider $formats;
  120.     }
  121.     public function __invoke(Request $request)
  122.     {
  123.         if ($this->swaggerUiAction) {
  124.             return $this->swaggerUiAction->__invoke($request);
  125.         }
  126.         $attributes RequestAttributesExtractor::extractAttributes($request);
  127.         // BC check to be removed in 3.0
  128.         if (null === $this->formatsProvider) {
  129.             $formats $attributes $this
  130.                 ->resourceMetadataFactory
  131.                 ->create($attributes['resource_class'])
  132.                 ->getOperationAttribute($attributes'output_formats', [], true) : $this->formats;
  133.         } else {
  134.             $formats $this->formatsProvider->getFormatsFromAttributes($attributes);
  135.         }
  136.         $documentation = new Documentation($this->resourceNameCollectionFactory->create(), $this->title$this->description$this->version);
  137.         return new Response($this->twig->render('@ApiPlatform/SwaggerUi/index.html.twig'$this->getContext($request$documentation) + ['formats' => $formats]));
  138.     }
  139.     /**
  140.      * Gets the base Twig context.
  141.      */
  142.     private function getContext(Request $requestDocumentation $documentation): array
  143.     {
  144.         $context = [
  145.             'title' => $this->title,
  146.             'description' => $this->description,
  147.             'showWebby' => $this->showWebby,
  148.             'swaggerUiEnabled' => $this->swaggerUiEnabled,
  149.             'reDocEnabled' => $this->reDocEnabled,
  150.             'graphqlEnabled' => $this->graphqlEnabled,
  151.             'graphiQlEnabled' => $this->graphiQlEnabled,
  152.             'graphQlPlaygroundEnabled' => $this->graphQlPlaygroundEnabled,
  153.             'assetPackage' => $this->assetPackage,
  154.         ];
  155.         $swaggerContext = ['spec_version' => $request->query->getInt('spec_version'$this->swaggerVersions[0] ?? 2)];
  156.         if ('' !== $baseUrl $request->getBaseUrl()) {
  157.             $swaggerContext['base_url'] = $baseUrl;
  158.         }
  159.         $swaggerData = [
  160.             'url' => $this->urlGenerator->generate('api_doc', ['format' => 'json']),
  161.             'spec' => $this->normalizer->normalize($documentation'json'$swaggerContext),
  162.             'extraConfiguration' => $this->swaggerUiExtraConfiguration,
  163.         ];
  164.         $swaggerData['oauth'] = [
  165.             'enabled' => $this->oauthEnabled,
  166.             'clientId' => $this->oauthClientId,
  167.             'clientSecret' => $this->oauthClientSecret,
  168.             'pkce' => $this->oauthPkce,
  169.             'type' => $this->oauthType,
  170.             'flow' => $this->oauthFlow,
  171.             'tokenUrl' => $this->oauthTokenUrl,
  172.             'authorizationUrl' => $this->oauthAuthorizationUrl,
  173.             'scopes' => $this->oauthScopes,
  174.         ];
  175.         if ($request->isMethodSafe() && null !== $resourceClass $request->attributes->get('_api_resource_class')) {
  176.             $swaggerData['id'] = $request->attributes->get('id');
  177.             $swaggerData['queryParameters'] = $request->query->all();
  178.             $metadata $this->resourceMetadataFactory->create($resourceClass);
  179.             $swaggerData['shortName'] = $metadata instanceof ResourceMetadata $metadata->getShortName() : $metadata[0]->getShortName();
  180.             if (null !== $collectionOperationName $request->attributes->get('_api_collection_operation_name')) {
  181.                 $swaggerData['operationId'] = sprintf('%s%sCollection'$collectionOperationNameucfirst($swaggerData['shortName']));
  182.             } elseif (null !== $itemOperationName $request->attributes->get('_api_item_operation_name')) {
  183.                 $swaggerData['operationId'] = sprintf('%s%sItem'$itemOperationNameucfirst($swaggerData['shortName']));
  184.             } elseif (null !== $subresourceOperationContext $request->attributes->get('_api_subresource_context')) {
  185.                 $swaggerData['operationId'] = $subresourceOperationContext['operationId'];
  186.             }
  187.             [$swaggerData['path'], $swaggerData['method']] = $this->getPathAndMethod($swaggerData);
  188.         }
  189.         return $context + ['swagger_data' => $swaggerData];
  190.     }
  191.     private function getPathAndMethod(array $swaggerData): array
  192.     {
  193.         foreach ($swaggerData['spec']['paths'] as $path => $operations) {
  194.             foreach ($operations as $method => $operation) {
  195.                 if ($operation['operationId'] === $swaggerData['operationId']) {
  196.                     return [$path$method];
  197.                 }
  198.             }
  199.         }
  200.         throw new RuntimeException(sprintf('The operation "%s" cannot be found in the Swagger specification.'$swaggerData['operationId']));
  201.     }
  202. }