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

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