<?php
/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace ApiPlatform\Documentation\Action;
use ApiPlatform\Documentation\Documentation;
use ApiPlatform\Documentation\DocumentationInterface;
use ApiPlatform\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;
use ApiPlatform\OpenApi\Factory\OpenApiFactoryInterface;
use Symfony\Component\HttpFoundation\Request;
/**
* Generates the API documentation.
*
* @author Amrouche Hamza <hamza.simperfit@gmail.com>
*/
final class DocumentationAction
{
public function __construct(private readonly ResourceNameCollectionFactoryInterface $resourceNameCollectionFactory, private readonly string $title = '', private readonly string $description = '', private readonly string $version = '', private readonly ?OpenApiFactoryInterface $openApiFactory = null)
{
}
public function __invoke(Request $request = null): DocumentationInterface
{
if (null !== $request) {
$context = ['base_url' => $request->getBaseUrl()];
if ($request->query->getBoolean('api_gateway')) {
$context['api_gateway'] = true;
}
$request->attributes->set('_api_normalization_context', $request->attributes->get('_api_normalization_context', []) + $context);
}
if ('json' === $request?->getRequestFormat() && null !== $this->openApiFactory) {
return $this->openApiFactory->__invoke($context ?? []);
}
return new Documentation($this->resourceNameCollectionFactory->create(), $this->title, $this->description, $this->version);
}
}