mirror of
https://github.com/dannyvankooten/AltoRouter.git
synced 2025-07-30 21:20:20 +02:00
Add addRoutes method for bulk adding routes
This commit is contained in:
@@ -4,4 +4,4 @@ php:
|
||||
- 5.4
|
||||
- 5.5
|
||||
|
||||
script: phpunit ./
|
||||
script: phpunit --coverage-text ./
|
||||
|
@@ -22,11 +22,25 @@ class AltoRouter {
|
||||
* @param array $matchTypes
|
||||
*/
|
||||
public function __construct( $routes = array(), $basePath = '', $matchTypes = array() ) {
|
||||
$this->addRoutes($routes);
|
||||
$this->setBasePath($basePath);
|
||||
$this->addMatchTypes($matchTypes);
|
||||
}
|
||||
|
||||
foreach( $routes as $route ) {
|
||||
call_user_func_array(array($this,'map'),$route);
|
||||
/**
|
||||
* Add multiple routes at once from array in the following format:
|
||||
*
|
||||
* $routes = array(
|
||||
* array($method, $route, $target, $name)
|
||||
* );
|
||||
*
|
||||
* @param array $routes
|
||||
* @return void
|
||||
* @author Koen Punt
|
||||
*/
|
||||
public function addRoutes(array $routes){
|
||||
foreach($routes as $route) {
|
||||
call_user_func_array(array($this, 'map'), $route);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -226,7 +240,7 @@ class AltoRouter {
|
||||
if (preg_match_all('`(/|\.|)\[([^:\]]*+)(?::([^:\]]*+))?\](\?|)`', $route, $matches, PREG_SET_ORDER)) {
|
||||
|
||||
$matchTypes = $this->matchTypes;
|
||||
foreach ($matches as $match) {
|
||||
foreach($matches as $match) {
|
||||
list($block, $pre, $type, $param, $optional) = $match;
|
||||
|
||||
if (isset($matchTypes[$type])) {
|
||||
|
@@ -45,6 +45,26 @@ class AltoRouterTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers AltoRouter::addRoutes
|
||||
*/
|
||||
public function testAddRoutes()
|
||||
{
|
||||
$method = 'POST';
|
||||
$route = '/[:controller]/[:action]';
|
||||
$target = function(){};
|
||||
|
||||
$this->router->addRoutes(array(
|
||||
array($method, $route, $target),
|
||||
array($method, $route, $target, 'second_route')
|
||||
));
|
||||
|
||||
$routes = $this->router->getRoutes();
|
||||
|
||||
$this->assertEquals(array($method, $route, $target, null), $routes[0]);
|
||||
$this->assertEquals(array($method, $route, $target, 'second_route'), $routes[1]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers AltoRouter::setBasePath
|
||||
*/
|
||||
|
Reference in New Issue
Block a user