From dbe416b9b92062fe20c3947b793ef33f1b1b60b3 Mon Sep 17 00:00:00 2001 From: Koen Punt Date: Wed, 16 Apr 2014 11:17:42 +0200 Subject: [PATCH] Add addRoutes method for bulk adding routes --- .travis.yml | 2 +- AltoRouter.php | 20 +++++++++++++++++--- AltoRouterTest.php | 20 ++++++++++++++++++++ 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index 54cb953..3bf3161 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,4 +4,4 @@ php: - 5.4 - 5.5 -script: phpunit ./ +script: phpunit --coverage-text ./ diff --git a/AltoRouter.php b/AltoRouter.php index 840a694..77bba01 100644 --- a/AltoRouter.php +++ b/AltoRouter.php @@ -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])) { diff --git a/AltoRouterTest.php b/AltoRouterTest.php index 0474f8a..af833b0 100644 --- a/AltoRouterTest.php +++ b/AltoRouterTest.php @@ -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 */