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

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\Documentation\Documentation;
  13. use ApiPlatform\Documentation\DocumentationInterface;
  14. use ApiPlatform\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;
  15. use ApiPlatform\OpenApi\Factory\OpenApiFactoryInterface;
  16. use Symfony\Component\HttpFoundation\Request;
  17. /**
  18.  * Generates the API documentation.
  19.  *
  20.  * @author Amrouche Hamza <hamza.simperfit@gmail.com>
  21.  */
  22. final class DocumentationAction
  23. {
  24.     public function __construct(private readonly ResourceNameCollectionFactoryInterface $resourceNameCollectionFactory, private readonly string $title '', private readonly string $description '', private readonly string $version '', private readonly ?OpenApiFactoryInterface $openApiFactory null)
  25.     {
  26.     }
  27.     public function __invoke(Request $request null): DocumentationInterface
  28.     {
  29.         if (null !== $request) {
  30.             $context = ['base_url' => $request->getBaseUrl()];
  31.             if ($request->query->getBoolean('api_gateway')) {
  32.                 $context['api_gateway'] = true;
  33.             }
  34.             $request->attributes->set('_api_normalization_context'$request->attributes->get('_api_normalization_context', []) + $context);
  35.         }
  36.         if ('json' === $request?->getRequestFormat() && null !== $this->openApiFactory) {
  37.             return $this->openApiFactory->__invoke($context ?? []);
  38.         }
  39.         return new Documentation($this->resourceNameCollectionFactory->create(), $this->title$this->description$this->version);
  40.     }
  41. }