mirror of
https://github.com/retailcrm/NelmioApiDocBundle.git
synced 2025-02-02 15:51:48 +03:00
Check the version in methods response
This commit is contained in:
parent
c6db0af5d4
commit
2dbdc702ac
@ -54,6 +54,10 @@ class DumpCommand extends ContainerAwareCommand
|
||||
$formatter = $this->getContainer()->get(sprintf('nelmio_api_doc.formatter.%s_formatter', $format));
|
||||
}
|
||||
|
||||
if ($input->hasOption('api-version')) {
|
||||
$formatter->setVersion($input->getOption('api-version'));
|
||||
}
|
||||
|
||||
if ($input->getOption('no-sandbox') && 'html' === $format) {
|
||||
$formatter->setEnableSandbox(false);
|
||||
}
|
||||
|
@ -22,10 +22,18 @@ class ApiDocController extends Controller
|
||||
public function indexAction(Request $request)
|
||||
{
|
||||
$extractor = $this->get('nelmio_api_doc.extractor.api_doc_extractor');
|
||||
$formatter = $this->get('nelmio_api_doc.formatter.html_formatter');
|
||||
$apiVersion = $request->query->get('_version', null);
|
||||
$extractedDoc = $apiVersion ? $extractor->allForVersion($apiVersion) : $extractor->all();
|
||||
|
||||
$htmlContent = $this->get('nelmio_api_doc.formatter.html_formatter')->format($extractedDoc);
|
||||
if ($apiVersion) {
|
||||
$formatter->setVersion($apiVersion);
|
||||
$extractedDoc = $extractor->allForVersion($apiVersion);
|
||||
} else {
|
||||
$extractedDoc = $extractor->all();
|
||||
}
|
||||
|
||||
$htmlContent = $formatter->format($extractedDoc);
|
||||
|
||||
return new Response($htmlContent, 200, array('Content-Type' => 'text/html'));
|
||||
}
|
||||
|
@ -16,6 +16,13 @@ use Nelmio\ApiDocBundle\DataTypes;
|
||||
|
||||
abstract class AbstractFormatter implements FormatterInterface
|
||||
{
|
||||
protected $version;
|
||||
|
||||
public function setVersion($version)
|
||||
{
|
||||
$this->version = $version;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
@ -52,6 +59,31 @@ abstract class AbstractFormatter implements FormatterInterface
|
||||
*/
|
||||
abstract protected function render(array $collection);
|
||||
|
||||
/**
|
||||
* Check that the versions range includes current version
|
||||
*
|
||||
* @access protected
|
||||
* @param string $fromVersion (default: null)
|
||||
* @param string $toVersion (default: null)
|
||||
* @return boolean
|
||||
*/
|
||||
protected function rangeIncludesVersion($fromVersion = null, $toVersion = null)
|
||||
{
|
||||
if (!$fromVersion && !$toVersion) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($fromVersion && version_compare($fromVersion, $this->version, '>')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($toVersion && version_compare($toVersion, $this->version, '<')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compresses nested parameters into a flat by changing the parameter
|
||||
* names to strings which contain the nested property names, for example:
|
||||
@ -67,6 +99,13 @@ abstract class AbstractFormatter implements FormatterInterface
|
||||
{
|
||||
$newParams = array();
|
||||
foreach ($data as $name => $info) {
|
||||
if ($this->version && !$this->rangeIncludesVersion(
|
||||
isset($info['sinceVersion']) ? $info['sinceVersion'] : null,
|
||||
isset($info['untilVersion']) ? $info['untilVersion'] : null
|
||||
)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$newName = $this->getNewName($name, $info, $parentName);
|
||||
|
||||
$newParams[$newName] = array(
|
||||
|
Loading…
x
Reference in New Issue
Block a user