From 2dbdc702ac73d6949a6b1fefd18faa162bdd8ba3 Mon Sep 17 00:00:00 2001 From: Ilyas Salikhov Date: Fri, 24 Oct 2014 14:49:04 +0400 Subject: [PATCH] Check the version in methods response --- Command/DumpCommand.php | 4 ++++ Controller/ApiDocController.php | 10 ++++++++- Formatter/AbstractFormatter.php | 39 +++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/Command/DumpCommand.php b/Command/DumpCommand.php index 296c1b6..9aef91c 100644 --- a/Command/DumpCommand.php +++ b/Command/DumpCommand.php @@ -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); } diff --git a/Controller/ApiDocController.php b/Controller/ApiDocController.php index 101902a..b4e37b5 100644 --- a/Controller/ApiDocController.php +++ b/Controller/ApiDocController.php @@ -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')); } diff --git a/Formatter/AbstractFormatter.php b/Formatter/AbstractFormatter.php index b7c0ad3..7615cad 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,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(