From 6215a5d64fd05dc47f6424a96b41d6085cfc7868 Mon Sep 17 00:00:00 2001 From: Michael Moussa Date: Thu, 10 Oct 2013 00:15:46 -0700 Subject: [PATCH 1/2] Allowing for configurable default API version when none is specified. --- config/module.config.php | 2 ++ src/ZF/Versioning/PrototypeRouteListener.php | 7 ++++++- .../Versioning/PrototypeRouteListenerTest.php | 19 ++++++++++++++----- 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/config/module.config.php b/config/module.config.php index 7b82337..cc0a6fa 100644 --- a/config/module.config.php +++ b/config/module.config.php @@ -15,6 +15,8 @@ // Example: // '#^application/vendor\.(?Pmwop)\.v(?P\d+)\.(?Pstatus|user)$#', ), + // Default version number to use if none is provided by the API consumer. Default: 1 + 'default-version' => 1, 'uri' => array( // Array of routes that should prepend the "zf-versioning" route // (i.e., "/v:version"). Any route in this array will be chained to diff --git a/src/ZF/Versioning/PrototypeRouteListener.php b/src/ZF/Versioning/PrototypeRouteListener.php index 80c8901..59b6ef5 100644 --- a/src/ZF/Versioning/PrototypeRouteListener.php +++ b/src/ZF/Versioning/PrototypeRouteListener.php @@ -22,7 +22,7 @@ class PrototypeRouteListener extends AbstractListenerAggregate protected $versionRoutePrefix = '[/v:version]'; /** - * Constraints to introducde in versioned routes + * Constraints to introduce in versioned routes * * @var array */ @@ -78,6 +78,11 @@ public function onMergeConfig(ModuleEvent $e) return; } + // Override default version of 1 with user-specified config value, if available. + if (isset($config['zf-versioning']['default-version'])) { + $this->versionRouteOptions['defaults']['version'] = $config['zf-versioning']['default-version']; + } + // Pre-process route list to strip out duplicates (often a result of // specifying nested routes) $routes = $config['zf-versioning']['uri']; diff --git a/test/ZFTest/Versioning/PrototypeRouteListenerTest.php b/test/ZFTest/Versioning/PrototypeRouteListenerTest.php index 19e8b33..b2231aa 100644 --- a/test/ZFTest/Versioning/PrototypeRouteListenerTest.php +++ b/test/ZFTest/Versioning/PrototypeRouteListenerTest.php @@ -79,8 +79,8 @@ public function testEmptyConfigurationDoesNotInjectPrototypes(array $routes) public function routesForWhichToVerifyPrototype() { return array( - 'status' => array(array('status')), - 'user' => array(array('user')), + 'status' => array(array('status'), 1), + 'user' => array(array('user'), 2), 'both' => array(array('status', 'user')), ); } @@ -88,9 +88,18 @@ public function routesForWhichToVerifyPrototype() /** * @dataProvider routesForWhichToVerifyPrototype */ - public function testPrototypeAddedToRoutesProvidedToListener(array $routes) + public function testPrototypeAddedToRoutesProvidedToListener(array $routes, $apiVersion = null) { - $this->config['zf-versioning'] = array('uri' => $routes); + $this->config['zf-versioning'] = array( + 'uri' => $routes + ); + + if (!empty($apiVersion)) { + $this->config['zf-versioning']['default-version'] = $apiVersion; + } else { + $apiVersion = 1; + } + $this->configListener->setMergedConfig($this->config); $listener = new PrototypeRouteListener(); $listener->onMergeConfig($this->event); @@ -115,7 +124,7 @@ public function testPrototypeAddedToRoutesProvidedToListener(array $routes) $this->assertArrayHasKey('defaults', $options); $this->assertArrayHasKey('version', $options['defaults']); - $this->assertEquals(1, $options['defaults']['version']); + $this->assertEquals($apiVersion, $options['defaults']['version']); } } } From 13c23024506d0dd5b180420737e60bf5550b8296 Mon Sep 17 00:00:00 2001 From: Michael Moussa Date: Thu, 10 Oct 2013 00:16:34 -0700 Subject: [PATCH 2/2] Updated README with default version configuration instruction. --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 5f66747..449237b 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,8 @@ Enables: - Prefixing defined routes with an optional `[/v:version]` segment, specifying a constraint of digits only for the version parameter, and defining a default - version of 1. + version of 1. Default can be overridden by modifying `[zf-versioning][default-version]` + in `module.config.php`. - Matching a default mediatype regular expression of `application/vnd.{api name}.v{version}(.{resource})?+json` in both Accept and Content-Type headers. - Injecting any discovered version parameters into the route matches.