Skip to content
This repository has been archived by the owner on Jan 21, 2020. It is now read-only.

Commit

Permalink
Merge pull request #1 from michaelmoussa/configurable-default-version
Browse files Browse the repository at this point in the history
Allowing for configurable default version
  • Loading branch information
weierophinney committed Nov 15, 2013
2 parents ce17da7 + 13c2302 commit 2ee65c9
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
// Example:
// '#^application/vendor\.(?P<vendor>mwop)\.v(?P<version>\d+)\.(?P<resource>status|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
Expand Down
7 changes: 6 additions & 1 deletion src/ZF/Versioning/PrototypeRouteListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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'];
Expand Down
19 changes: 14 additions & 5 deletions test/ZFTest/Versioning/PrototypeRouteListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,27 @@ 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')),
);
}

/**
* @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);
Expand All @@ -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']);
}
}
}

0 comments on commit 2ee65c9

Please sign in to comment.