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.      * @param mixed      $formats
  64.      * @param mixed      $oauthEnabled
  65.      * @param mixed      $oauthClientId
  66.      * @param mixed      $oauthClientSecret
  67.      * @param mixed      $oauthType
  68.      * @param mixed      $oauthFlow
  69.      * @param mixed      $oauthTokenUrl
  70.      * @param mixed      $oauthAuthorizationUrl
  71.      * @param mixed      $oauthScopes
  72.      */
  73.     public function __construct(ResourceNameCollectionFactoryInterface $resourceNameCollectionFactoryResourceMetadataFactoryInterface $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)
  74.     {
  75.         $this->resourceNameCollectionFactory $resourceNameCollectionFactory;
  76.         $this->resourceMetadataFactory $resourceMetadataFactory;
  77.         $this->normalizer $normalizer;
  78.         $this->twig $twig;
  79.         $this->urlGenerator $urlGenerator;
  80.         $this->title $title;
  81.         $this->showWebby $showWebby;
  82.         $this->description $description;
  83.         $this->version $version;
  84.         $this->oauthEnabled $oauthEnabled;
  85.         $this->oauthClientId $oauthClientId;
  86.         $this->oauthClientSecret $oauthClientSecret;
  87.         $this->oauthType $oauthType;
  88.         $this->oauthFlow $oauthFlow;
  89.         $this->oauthTokenUrl $oauthTokenUrl;
  90.         $this->oauthAuthorizationUrl $oauthAuthorizationUrl;
  91.         $this->oauthScopes $oauthScopes;
  92.         $this->swaggerUiEnabled $swaggerUiEnabled;
  93.         $this->reDocEnabled $reDocEnabled;
  94.         $this->graphqlEnabled $graphqlEnabled;
  95.         $this->graphiQlEnabled $graphiQlEnabled;
  96.         $this->graphQlPlaygroundEnabled $graphQlPlaygroundEnabled;
  97.         $this->swaggerVersions $swaggerVersions;
  98.         $this->swaggerUiAction $swaggerUiAction;
  99.         $this->assetPackage $assetPackage;
  100.         if (null === $this->twig) {
  101.             throw new \RuntimeException('The documentation cannot be displayed since the Twig bundle is not installed. Try running "composer require symfony/twig-bundle".');
  102.         }
  103.         if (null === $this->swaggerUiAction) {
  104.             @trigger_error(sprintf('The use of "%s" is deprecated since API Platform 2.6, use "%s" instead.'__CLASS__OpenApiSwaggerUiAction::class), \E_USER_DEPRECATED);
  105.         }
  106.         if (\is_array($formats)) {
  107.             $this->formats $formats;
  108.             return;
  109.         }
  110.         @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);
  111.         $this->formatsProvider $formats;
  112.     }
  113.     public function __invoke(Request $request)
  114.     {
  115.         if ($this->swaggerUiAction) {
  116.             return $this->swaggerUiAction->__invoke($request);
  117.         }
  118.         $attributes RequestAttributesExtractor::extractAttributes($request);
  119.         // BC check to be removed in 3.0
  120.         if (null === $this->formatsProvider) {
  121.             $formats $attributes $this
  122.                 ->resourceMetadataFactory
  123.                 ->create($attributes['resource_class'])
  124.                 ->getOperationAttribute($attributes'output_formats', [], true) : $this->formats;
  125.         } else {
  126.             $formats $this->formatsProvider->getFormatsFromAttributes($attributes);
  127.         }
  128.         $documentation = new Documentation($this->resourceNameCollectionFactory->create(), $this->title$this->description$this->version);
  129.         return new Response($this->twig->render('@ApiPlatform/SwaggerUi/index.html.twig'$this->getContext($request$documentation) + ['formats' => $formats]));
  130.     }
  131.     /**
  132.      * Gets the base Twig context.
  133.      */
  134.     private function getContext(Request $requestDocumentation $documentation): array
  135.     {
  136.         $context = [
  137.             'title' => $this->title,
  138.             'description' => $this->description,
  139.             'showWebby' => $this->showWebby,
  140.             'swaggerUiEnabled' => $this->swaggerUiEnabled,
  141.             'reDocEnabled' => $this->reDocEnabled,
  142.             'graphqlEnabled' => $this->graphqlEnabled,
  143.             'graphiQlEnabled' => $this->graphiQlEnabled,
  144.             'graphQlPlaygroundEnabled' => $this->graphQlPlaygroundEnabled,
  145.             'assetPackage' => $this->assetPackage,
  146.         ];
  147.         $swaggerContext = ['spec_version' => $request->query->getInt('spec_version'$this->swaggerVersions[0] ?? 2)];
  148.         if ('' !== $baseUrl $request->getBaseUrl()) {
  149.             $swaggerContext['base_url'] = $baseUrl;
  150.         }
  151.         $swaggerData = [
  152.             'url' => $this->urlGenerator->generate('api_doc', ['format' => 'json']),
  153.             'spec' => $this->normalizer->normalize($documentation'json'$swaggerContext),
  154.         ];
  155.         $swaggerData['oauth'] = [
  156.             'enabled' => $this->oauthEnabled,
  157.             'clientId' => $this->oauthClientId,
  158.             'clientSecret' => $this->oauthClientSecret,
  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->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. }