vendor/api-platform/core/src/Documentation/Action/DocumentationAction.php line 30

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\Documentation\Action;
  12. use ApiPlatform\Core\Api\FormatsProviderInterface;
  13. use ApiPlatform\Core\OpenApi\Factory\OpenApiFactoryInterface as LegacyOpenApiFactoryInterface;
  14. use ApiPlatform\Documentation\Documentation;
  15. use ApiPlatform\Documentation\DocumentationInterface;
  16. use ApiPlatform\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;
  17. use ApiPlatform\OpenApi\Factory\OpenApiFactoryInterface;
  18. use ApiPlatform\Util\RequestAttributesExtractor;
  19. use Symfony\Component\HttpFoundation\Request;
  20. /**
  21.  * Generates the API documentation.
  22.  *
  23.  * @author Amrouche Hamza <hamza.simperfit@gmail.com>
  24.  */
  25. final class DocumentationAction
  26. {
  27.     private $resourceNameCollectionFactory;
  28.     private $title;
  29.     private $description;
  30.     private $version;
  31.     private $formats;
  32.     private $formatsProvider;
  33.     private $swaggerVersions;
  34.     private $openApiFactory;
  35.     /**
  36.      * @param int[]                                                 $swaggerVersions
  37.      * @param mixed|array|FormatsProviderInterface                  $formatsProvider
  38.      * @param LegacyOpenApiFactoryInterface|OpenApiFactoryInterface $openApiFactory
  39.      */
  40.     public function __construct(ResourceNameCollectionFactoryInterface $resourceNameCollectionFactorystring $title ''string $description ''string $version ''$formatsProvider null, array $swaggerVersions = [23], $openApiFactory null)
  41.     {
  42.         $this->resourceNameCollectionFactory $resourceNameCollectionFactory;
  43.         $this->title $title;
  44.         $this->description $description;
  45.         $this->version $version;
  46.         $this->swaggerVersions $swaggerVersions;
  47.         $this->openApiFactory $openApiFactory;
  48.         if (null === $openApiFactory) {
  49.             @trigger_error(sprintf('Not passing an instance of "%s" as 7th parameter of the constructor of "%s" is deprecated since API Platform 2.6'OpenApiFactoryInterface::class, __CLASS__), \E_USER_DEPRECATED);
  50.         }
  51.         if (null === $formatsProvider) {
  52.             return;
  53.         }
  54.         @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'FormatsProviderInterface::class, __CLASS__), \E_USER_DEPRECATED);
  55.         if (\is_array($formatsProvider)) {
  56.             $this->formats $formatsProvider;
  57.             return;
  58.         }
  59.         $this->formatsProvider $formatsProvider;
  60.     }
  61.     public function __invoke(Request $request null): DocumentationInterface
  62.     {
  63.         if (null !== $request) {
  64.             $context = ['base_url' => $request->getBaseUrl(), 'spec_version' => $request->query->getInt('spec_version'$this->swaggerVersions[0] ?? 3)];
  65.             if ($request->query->getBoolean('api_gateway')) {
  66.                 $context['api_gateway'] = true;
  67.             }
  68.             $request->attributes->set('_api_normalization_context'$request->attributes->get('_api_normalization_context', []) + $context);
  69.             $attributes RequestAttributesExtractor::extractAttributes($request);
  70.         }
  71.         // BC check to be removed in 3.0
  72.         if (null !== $this->formatsProvider) {
  73.             $this->formats $this->formatsProvider->getFormatsFromAttributes($attributes ?? []);
  74.         }
  75.         if ('json' === $request->getRequestFormat() && null !== $this->openApiFactory && === ($context['spec_version'] ?? null)) {
  76.             return $this->openApiFactory->__invoke($context);
  77.         }
  78.         return new Documentation($this->resourceNameCollectionFactory->create(), $this->title$this->description$this->version$this->formats);
  79.     }
  80. }
  81. class_alias(DocumentationAction::class, \ApiPlatform\Core\Documentation\Action\DocumentationAction::class);