From dbe416b9b92062fe20c3947b793ef33f1b1b60b3 Mon Sep 17 00:00:00 2001 From: Koen Punt Date: Wed, 16 Apr 2014 11:17:42 +0200 Subject: [PATCH 1/2] 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 */ From b29440e11781247b093ee2644d4c7a9bc304a96f Mon Sep 17 00:00:00 2001 From: Koen Punt Date: Wed, 16 Apr 2014 11:39:57 +0200 Subject: [PATCH 2/2] Allow instance of iterator in addRoutes --- AltoRouter.php | 6 +++-- AltoRouterTest.php | 56 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 2 deletions(-) diff --git a/AltoRouter.php b/AltoRouter.php index 77bba01..aee4fdd 100644 --- a/AltoRouter.php +++ b/AltoRouter.php @@ -38,7 +38,10 @@ class AltoRouter { * @return void * @author Koen Punt */ - public function addRoutes(array $routes){ + public function addRoutes($routes){ + if(!is_array($routes) && !$routes instanceof Traversable) { + throw new \Exception('Routes should be an array or an instance of Traversable'); + } foreach($routes as $route) { call_user_func_array(array($this, 'map'), $route); } @@ -68,7 +71,6 @@ class AltoRouter { * @param string $route The route regex, custom regex must start with an @. You can use multiple pre-set regex filters, like [i:id] * @param mixed $target The target where this route should point to. Can be anything. * @param string $name Optional name of this route. Supply if you want to reverse route this url in your application. - * */ public function map($method, $route, $target, $name = null) { diff --git a/AltoRouterTest.php b/AltoRouterTest.php index af833b0..1594d98 100644 --- a/AltoRouterTest.php +++ b/AltoRouterTest.php @@ -18,6 +18,33 @@ class AltoRouterDebug extends AltoRouter{ } +class SimpleTraversable implements Iterator{ + + protected $_position = 0; + + protected $_data = array( + array('GET', '/foo', 'foo_action', null), + array('POST', '/bar', 'bar_action', 'second_route') + ); + + public function current(){ + return $this->_data[$this->_position]; + } + public function key(){ + return $this->_position; + } + public function next(){ + ++$this->_position; + } + public function rewind(){ + $this->_position = 0; + } + public function valid(){ + return isset($this->_data[$this->_position]); + } + +} + /** * Generated by PHPUnit_SkeletonGenerator 1.2.1 on 2013-07-14 at 17:47:46. */ @@ -65,6 +92,35 @@ class AltoRouterTest extends PHPUnit_Framework_TestCase $this->assertEquals(array($method, $route, $target, 'second_route'), $routes[1]); } + /** + * @covers AltoRouter::addRoutes + */ + public function testAddRoutesAcceptsTraverable() + { + $traversable = new SimpleTraversable(); + $this->router->addRoutes($traversable); + + $traversable->rewind(); + + $first = $traversable->current(); + $traversable->next(); + $second = $traversable->current(); + + $routes = $this->router->getRoutes(); + + $this->assertEquals($first, $routes[0]); + $this->assertEquals($second, $routes[1]); + } + + /** + * @covers AltoRouter::addRoutes + * @expectedException Exception + */ + public function testAddRoutesThrowsExceptionOnInvalidArgument() + { + $this->router->addRoutes(new stdClass); + } + /** * @covers AltoRouter::setBasePath */