2017-06-26 10:34:42 +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\Controller;
|
|
|
|
|
2020-05-28 13:19:11 +02:00
|
|
|
use OpenApi\Annotations\OpenApi;
|
|
|
|
use OpenApi\Annotations\Server;
|
2018-01-05 13:08:02 +01:00
|
|
|
use Psr\Container\ContainerInterface;
|
2017-06-26 10:34:42 +02:00
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
2017-07-05 15:41:53 +02:00
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
2018-01-05 13:08:02 +01:00
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
2017-06-26 10:34:42 +02:00
|
|
|
|
|
|
|
final class DocumentationController
|
|
|
|
{
|
2018-01-05 13:08:02 +01:00
|
|
|
private $generatorLocator;
|
2017-06-26 10:34:42 +02:00
|
|
|
|
2020-05-31 18:18:29 +02:00
|
|
|
public function __construct(ContainerInterface $generatorLocator)
|
2017-06-26 10:34:42 +02:00
|
|
|
{
|
2018-01-05 13:08:02 +01:00
|
|
|
$this->generatorLocator = $generatorLocator;
|
2017-06-26 10:34:42 +02:00
|
|
|
}
|
|
|
|
|
2018-01-05 13:08:02 +01:00
|
|
|
public function __invoke(Request $request, $area = 'default')
|
2017-06-26 10:34:42 +02:00
|
|
|
{
|
2018-01-05 13:08:02 +01:00
|
|
|
if (!$this->generatorLocator->has($area)) {
|
2019-04-12 09:35:49 +02:00
|
|
|
throw new BadRequestHttpException(sprintf('Area "%s" is not supported as it isn\'t defined in config.', $area));
|
2018-01-05 13:08:02 +01:00
|
|
|
}
|
|
|
|
|
2020-05-28 13:19:11 +02:00
|
|
|
/** @var OpenApi $spec */
|
|
|
|
$spec = $this->generatorLocator->get($area)->generate();
|
2019-04-10 20:55:04 +02:00
|
|
|
|
2017-07-05 15:41:53 +02:00
|
|
|
if ('' !== $request->getBaseUrl()) {
|
2020-05-28 13:19:11 +02:00
|
|
|
$spec->servers = [new Server(['url' => $request->getSchemeAndHttpHost().$request->getBaseUrl()])];
|
2019-04-10 20:55:04 +02:00
|
|
|
}
|
|
|
|
|
2017-07-05 15:41:53 +02:00
|
|
|
return new JsonResponse($spec);
|
2017-06-26 10:34:42 +02:00
|
|
|
}
|
|
|
|
}
|