diff --git a/Command/DumpCommand.php b/Command/DumpCommand.php index af5450c..6e93b2c 100644 --- a/Command/DumpCommand.php +++ b/Command/DumpCommand.php @@ -63,6 +63,10 @@ class DumpCommand extends ContainerAwareCommand $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) { $formatter->setEnableSandbox(false); } diff --git a/Controller/ApiDocController.php b/Controller/ApiDocController.php index 5e134aa..a856b59 100644 --- a/Controller/ApiDocController.php +++ b/Controller/ApiDocController.php @@ -23,9 +23,16 @@ class ApiDocController extends Controller public function indexAction(Request $request, $view = ApiDoc::DEFAULT_VIEW) { $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, $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')); } diff --git a/Formatter/AbstractFormatter.php b/Formatter/AbstractFormatter.php index 18cecdc..f26da59 100644 --- a/Formatter/AbstractFormatter.php +++ b/Formatter/AbstractFormatter.php @@ -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,28 @@ 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 +96,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(