2014-06-28 00:20:12 +00:00
|
|
|
<?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\Formatter;
|
|
|
|
|
2014-06-17 17:05:00 -07:00
|
|
|
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
|
2014-06-28 00:20:12 +00:00
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Extends SwaggerFormatter which takes into account the request's base URL when generating the documents for direct swagger-ui consumption.
|
|
|
|
*
|
|
|
|
* @author Bezalel Hermoso <bezalelhermoso@gmail.com>
|
|
|
|
*/
|
2014-06-17 17:05:00 -07:00
|
|
|
class RequestAwareSwaggerFormatter implements FormatterInterface
|
2014-06-28 00:20:12 +00:00
|
|
|
{
|
|
|
|
/**
|
2024-10-01 15:54:04 +03:00
|
|
|
* @var Request
|
2014-06-28 00:20:12 +00:00
|
|
|
*/
|
|
|
|
protected $request;
|
|
|
|
|
2014-06-17 17:05:00 -07:00
|
|
|
/**
|
|
|
|
* @var SwaggerFormatter
|
|
|
|
*/
|
|
|
|
protected $formatter;
|
2014-06-28 00:20:12 +00:00
|
|
|
|
2014-06-17 17:05:00 -07:00
|
|
|
public function __construct(Request $request, SwaggerFormatter $formatter)
|
2014-06-28 00:20:12 +00:00
|
|
|
{
|
|
|
|
$this->request = $request;
|
2014-06-17 17:05:00 -07:00
|
|
|
$this->formatter = $formatter;
|
2014-06-28 00:20:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-06-17 17:05:00 -07:00
|
|
|
* Format a collection of documentation data.
|
|
|
|
*
|
2024-10-01 15:54:04 +03:00
|
|
|
* @param null $resource
|
|
|
|
*
|
2014-06-17 17:05:00 -07:00
|
|
|
* @internal param $array [ApiDoc] $collection
|
2024-10-01 15:54:04 +03:00
|
|
|
*
|
2014-06-17 17:05:00 -07:00
|
|
|
* @return string|array
|
|
|
|
*/
|
|
|
|
public function format(array $collection, $resource = null)
|
|
|
|
{
|
|
|
|
$result = $this->formatter->format($collection, $resource);
|
|
|
|
|
2024-10-01 15:54:04 +03:00
|
|
|
if (null !== $resource) {
|
2014-06-17 17:05:00 -07:00
|
|
|
$result['basePath'] = $this->request->getBaseUrl() . $result['basePath'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Format documentation data for one route.
|
|
|
|
*
|
|
|
|
* @param ApiDoc $annotation
|
|
|
|
* return string|array
|
2014-06-28 00:20:12 +00:00
|
|
|
*/
|
2014-06-17 17:05:00 -07:00
|
|
|
public function formatOne(ApiDoc $annotation)
|
2014-06-28 00:20:12 +00:00
|
|
|
{
|
2014-06-17 17:05:00 -07:00
|
|
|
return $this->formatter->formatOne($annotation);
|
2014-06-28 00:20:12 +00:00
|
|
|
}
|
2015-03-06 11:19:08 +01:00
|
|
|
}
|