Check the version in methods response

This commit is contained in:
Vitaliy Chesnokov 2019-04-24 13:12:35 +03:00
parent c6fa1e893e
commit cd87930d29
No known key found for this signature in database
GPG Key ID: FD23DF1B48ECC3EB
3 changed files with 49 additions and 2 deletions

View File

@ -63,6 +63,10 @@ class DumpCommand extends ContainerAwareCommand
$this->getContainer()->get('translator')->setLocale($input->getOption('locale')); $this->getContainer()->get('translator')->setLocale($input->getOption('locale'));
} }
if ($input->hasOption('api-version')) {
$formatter->setVersion($input->getOption('api-version'));
}
if ($input->getOption('no-sandbox') && 'html' === $format) { if ($input->getOption('no-sandbox') && 'html' === $format) {
$formatter->setEnableSandbox(false); $formatter->setEnableSandbox(false);
} }

View File

@ -23,9 +23,16 @@ class ApiDocController extends Controller
public function indexAction(Request $request, $view = ApiDoc::DEFAULT_VIEW) public function indexAction(Request $request, $view = ApiDoc::DEFAULT_VIEW)
{ {
$extractor = $this->get('nelmio_api_doc.extractor.api_doc_extractor'); $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); $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); if ($apiVersion) {
$formatter->setVersion($apiVersion);
$extractedDoc = $extractor->allForVersion($apiVersion, $view);
} else {
$extractedDoc = $extractor->all($view);
}
$htmlContent = $formatter->format($extractedDoc);
return new Response($htmlContent, 200, array('Content-Type' => 'text/html')); return new Response($htmlContent, 200, array('Content-Type' => 'text/html'));
} }

View File

@ -16,6 +16,13 @@ use Nelmio\ApiDocBundle\DataTypes;
abstract class AbstractFormatter implements FormatterInterface abstract class AbstractFormatter implements FormatterInterface
{ {
protected $version;
public function setVersion($version)
{
$this->version = $version;
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
@ -52,6 +59,28 @@ abstract class AbstractFormatter implements FormatterInterface
*/ */
abstract protected function render(array $collection); 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 * Compresses nested parameters into a flat by changing the parameter
* names to strings which contain the nested property names, for example: * names to strings which contain the nested property names, for example:
@ -67,6 +96,13 @@ abstract class AbstractFormatter implements FormatterInterface
{ {
$newParams = array(); $newParams = array();
foreach ($data as $name => $info) { 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); $newName = $this->getNewName($name, $info, $parentName);
$newParams[$newName] = array( $newParams[$newName] = array(