2012-04-12 12:50:20 +02:00
|
|
|
<?php
|
|
|
|
|
2012-04-13 11:03:05 +02:00
|
|
|
/*
|
|
|
|
* This file is part of the NelmioApiDocBundle.
|
|
|
|
*
|
|
|
|
* (c) Nelmio <hello@nelm.io>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2012-04-12 18:37:42 +02:00
|
|
|
namespace Nelmio\ApiDocBundle\Controller;
|
2012-04-12 12:50:20 +02:00
|
|
|
|
2024-10-01 15:54:04 +03:00
|
|
|
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
|
2024-07-02 16:24:26 +03:00
|
|
|
use Nelmio\ApiDocBundle\Extractor\ApiDocExtractor;
|
|
|
|
use Nelmio\ApiDocBundle\Formatter\HtmlFormatter;
|
2014-06-17 17:05:00 -07:00
|
|
|
use Nelmio\ApiDocBundle\Formatter\RequestAwareSwaggerFormatter;
|
2024-07-02 16:24:26 +03:00
|
|
|
use Nelmio\ApiDocBundle\Formatter\SwaggerFormatter;
|
2024-10-01 15:54:04 +03:00
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
2014-06-17 17:05:00 -07:00
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
2012-04-12 12:50:20 +02:00
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
|
2023-01-09 15:59:54 +03:00
|
|
|
class ApiDocController extends AbstractController
|
2012-04-12 12:50:20 +02:00
|
|
|
{
|
2024-06-18 00:19:53 +03:00
|
|
|
public function __construct(
|
2024-07-02 16:24:26 +03:00
|
|
|
private readonly ApiDocExtractor $extractor,
|
|
|
|
private readonly HtmlFormatter $htmlFormatter,
|
2024-10-01 15:54:04 +03:00
|
|
|
private readonly SwaggerFormatter $swaggerFormatter,
|
2024-06-18 00:19:53 +03:00
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
|
|
|
public function index(Request $request, $view = ApiDoc::DEFAULT_VIEW)
|
2012-04-12 12:50:20 +02:00
|
|
|
{
|
2019-04-24 12:55:08 +03:00
|
|
|
$apiVersion = $request->query->get('_version', null);
|
2019-04-24 13:12:35 +03:00
|
|
|
|
|
|
|
if ($apiVersion) {
|
2024-07-02 16:24:26 +03:00
|
|
|
$this->htmlFormatter->setVersion($apiVersion);
|
|
|
|
$extractedDoc = $this->extractor->allForVersion($apiVersion, $view);
|
2019-04-24 13:12:35 +03:00
|
|
|
} else {
|
2024-07-02 16:24:26 +03:00
|
|
|
$extractedDoc = $this->extractor->all($view);
|
2019-04-24 13:12:35 +03:00
|
|
|
}
|
2024-10-01 15:54:04 +03:00
|
|
|
$htmlContent = $this->htmlFormatter->format($extractedDoc);
|
2012-04-12 12:50:20 +02:00
|
|
|
|
2024-10-01 15:54:04 +03:00
|
|
|
return new Response($htmlContent, 200, ['Content-Type' => 'text/html']);
|
2012-04-12 12:50:20 +02:00
|
|
|
}
|
2014-06-17 17:05:00 -07:00
|
|
|
|
2024-06-18 00:19:53 +03:00
|
|
|
public function swagger(Request $request, $resource = null)
|
2014-06-17 17:05:00 -07:00
|
|
|
{
|
2024-07-02 16:24:26 +03:00
|
|
|
$docs = $this->extractor->all();
|
|
|
|
$formatter = new RequestAwareSwaggerFormatter($request, $this->swaggerFormatter);
|
2014-06-17 17:05:00 -07:00
|
|
|
|
2014-06-17 17:05:00 -07:00
|
|
|
$spec = $formatter->format($docs, $resource ? '/' . $resource : null);
|
2014-06-28 00:20:12 +00:00
|
|
|
|
2024-10-01 15:54:04 +03:00
|
|
|
if (null !== $resource && 0 === count($spec['apis'])) {
|
2014-06-28 00:20:12 +00:00
|
|
|
throw $this->createNotFoundException(sprintf('Cannot find resource "%s"', $resource));
|
|
|
|
}
|
|
|
|
|
2014-06-17 17:05:00 -07:00
|
|
|
return new JsonResponse($spec);
|
|
|
|
}
|
2012-04-12 12:50:20 +02:00
|
|
|
}
|