NelmioApiDocBundle/Controller/ApiDocController.php
2019-04-25 21:50:28 +03:00

48 lines
1.7 KiB
PHP

<?php
/*
* 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.
*/
namespace Nelmio\ApiDocBundle\Controller;
use Nelmio\ApiDocBundle\Formatter\RequestAwareSwaggerFormatter;
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class ApiDocController extends Controller
{
public function indexAction(Request $request, $view = ApiDoc::DEFAULT_VIEW)
{
$extractor = $this->get('nelmio_api_doc.extractor.api_doc_extractor');
$apiVersion = $request->query->get('_version', null);
$extractedDoc = $apiVersion ? $extractor->allForVersion($apiVersion, $view) : $extractor->all($view);
$htmlContent = $this->get('nelmio_api_doc.formatter.html_formatter')->format($extractedDoc);
return new Response($htmlContent, 200, array('Content-Type' => 'text/html'));
}
public function swaggerAction(Request $request, $resource = null)
{
$docs = $this->get('nelmio_api_doc.extractor.api_doc_extractor')->all();
$formatter = new RequestAwareSwaggerFormatter($request, $this->get('nelmio_api_doc.formatter.swagger_formatter'));
$spec = $formatter->format($docs, $resource ? '/' . $resource : null);
if ($resource !== null && count($spec['apis']) === 0) {
throw $this->createNotFoundException(sprintf('Cannot find resource "%s"', $resource));
}
return new JsonResponse($spec);
}
}