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
|
|
|
|
2014-06-17 17:05:00 -07:00
|
|
|
use Nelmio\ApiDocBundle\Formatter\RequestAwareSwaggerFormatter;
|
2012-04-12 12:50:20 +02:00
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
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;
|
|
|
|
|
|
|
|
class ApiDocController extends Controller
|
|
|
|
{
|
2014-11-22 22:27:13 +01:00
|
|
|
public function indexAction($api = "default")
|
2012-04-12 12:50:20 +02:00
|
|
|
{
|
2014-11-22 22:27:13 +01:00
|
|
|
$extractedDoc = $this->get('nelmio_api_doc.extractor.api_doc_extractor')->all($api);
|
2012-04-12 18:44:38 +02:00
|
|
|
$htmlContent = $this->get('nelmio_api_doc.formatter.html_formatter')->format($extractedDoc);
|
2012-04-12 12:50:20 +02:00
|
|
|
|
2012-08-23 15:09:56 +02:00
|
|
|
return new Response($htmlContent, 200, array('Content-Type' => 'text/html'));
|
2012-04-12 12:50:20 +02:00
|
|
|
}
|
2014-06-17 17:05:00 -07:00
|
|
|
|
|
|
|
public function swaggerAction(Request $request, $resource = null)
|
|
|
|
{
|
2014-06-17 17:05:00 -07:00
|
|
|
|
2014-06-17 17:05:00 -07:00
|
|
|
$docs = $this->get('nelmio_api_doc.extractor.api_doc_extractor')->all();
|
2014-06-17 17:05:00 -07:00
|
|
|
$formatter = new RequestAwareSwaggerFormatter($request, $this->get('nelmio_api_doc.formatter.swagger_formatter'));
|
|
|
|
|
2014-06-17 17:05:00 -07:00
|
|
|
$spec = $formatter->format($docs, $resource ? '/' . $resource : null);
|
2014-06-28 00:20:12 +00:00
|
|
|
|
2014-06-17 17:05:00 -07:00
|
|
|
if ($resource !== null && count($spec['apis']) === 0) {
|
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
|
|
|
}
|