2021-06-27 08:41:33 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the NelmioApiDocBundle package.
|
|
|
|
*
|
|
|
|
* (c) Nelmio
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Nelmio\ApiDocBundle\Render;
|
|
|
|
|
|
|
|
use InvalidArgumentException;
|
|
|
|
use OpenApi\Annotations\OpenApi;
|
2021-07-29 12:11:10 +02:00
|
|
|
use OpenApi\Annotations\Server;
|
2021-06-27 08:41:33 +02:00
|
|
|
use Psr\Container\ContainerInterface;
|
2021-07-29 12:11:10 +02:00
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
2021-06-27 08:41:33 +02:00
|
|
|
|
|
|
|
class RenderOpenApi
|
|
|
|
{
|
|
|
|
public const HTML = 'html';
|
|
|
|
public const JSON = 'json';
|
2021-07-29 11:57:00 +02:00
|
|
|
public const YAML = 'yaml';
|
2021-06-27 08:41:33 +02:00
|
|
|
|
|
|
|
/** @var ContainerInterface */
|
|
|
|
private $generatorLocator;
|
|
|
|
|
|
|
|
/** @var array<string, OpenApiRenderer> */
|
|
|
|
private $openApiRenderers = [];
|
|
|
|
|
|
|
|
public function __construct(ContainerInterface $generatorLocator, OpenApiRenderer ...$openApiRenderers)
|
|
|
|
{
|
|
|
|
$this->generatorLocator = $generatorLocator;
|
|
|
|
foreach ($openApiRenderers as $openApiRenderer) {
|
2021-09-22 23:43:32 +02:00
|
|
|
if (null === $openApiRenderer) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-06-27 08:41:33 +02:00
|
|
|
$this->openApiRenderers[$openApiRenderer->getFormat()] = $openApiRenderer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-27 09:24:35 +02:00
|
|
|
public function getAvailableFormats(): array
|
|
|
|
{
|
|
|
|
return array_keys($this->openApiRenderers);
|
|
|
|
}
|
|
|
|
|
2021-07-29 12:11:10 +02:00
|
|
|
public function renderFromRequest(Request $request, string $format, $area, array $extraOptions = [])
|
|
|
|
{
|
|
|
|
$options = [];
|
|
|
|
if ('' !== $request->getBaseUrl()) {
|
|
|
|
$options += [
|
|
|
|
'server_url' => $request->getSchemeAndHttpHost().$request->getBaseUrl(),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
$options += $extraOptions;
|
|
|
|
|
|
|
|
return $this->render($format, $area, $options);
|
|
|
|
}
|
|
|
|
|
2021-06-27 08:41:33 +02:00
|
|
|
/**
|
|
|
|
* @throws InvalidArgumentException If the area to dump is not valid
|
|
|
|
*/
|
|
|
|
public function render(string $format, string $area, array $options = []): string
|
|
|
|
{
|
|
|
|
if (!$this->generatorLocator->has($area)) {
|
|
|
|
throw new InvalidArgumentException(sprintf('Area "%s" is not supported.', $area));
|
|
|
|
} elseif (!array_key_exists($format, $this->openApiRenderers)) {
|
|
|
|
throw new InvalidArgumentException(sprintf('Format "%s" is not supported.', $format));
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var OpenApi $spec */
|
|
|
|
$spec = $this->generatorLocator->get($area)->generate();
|
|
|
|
|
2021-07-29 12:11:10 +02:00
|
|
|
if (array_key_exists('server_url', $options) && $options['server_url']) {
|
|
|
|
$spec->servers = [new Server(['url' => $options['server_url']])];
|
|
|
|
}
|
|
|
|
|
2021-06-27 08:41:33 +02:00
|
|
|
return $this->openApiRenderers[$format]->render($spec, $options);
|
|
|
|
}
|
|
|
|
}
|