From d84e51ba4739a31f67755935124bd373dc43040c Mon Sep 17 00:00:00 2001 From: Mike Cao Date: Sun, 6 Jul 2014 21:59:21 -0700 Subject: [PATCH] Updated route object passing functionality. Before the route object would automatically be passed to all callbacks. Now you need to explicitly ask for it by passing in true as the third parameter in Flight::route(). --- VERSION | 2 +- flight/Engine.php | 6 +++--- flight/net/Route.php | 17 ++++++++++++++++- flight/net/Router.php | 15 ++++++++------- tests/RouterTest.php | 40 +++++++++++++++++++++++++++++----------- 5 files changed, 57 insertions(+), 23 deletions(-) diff --git a/VERSION b/VERSION index 512a1fa..5ed5faa 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.1.9 +1.1.10 diff --git a/flight/Engine.php b/flight/Engine.php index ebf374f..abb3c56 100644 --- a/flight/Engine.php +++ b/flight/Engine.php @@ -303,7 +303,6 @@ class Engine { // Route the request while ($route = $router->route($request)) { $params = array_values($route->params); - array_push($params, $route); $continue = $this->dispatcher->execute( $route->callback, @@ -391,9 +390,10 @@ class Engine { * * @param string $pattern URL pattern to match * @param callback $callback Callback function + * @param boolean $pass_route Pass the matching route object to the callback */ - public function _route($pattern, $callback) { - $this->router()->map($pattern, $callback); + public function _route($pattern, $callback, $pass_route = false) { + $this->router()->map($pattern, $callback, $pass_route); } /** diff --git a/flight/net/Route.php b/flight/net/Route.php index f8f99ff..4603ed9 100644 --- a/flight/net/Route.php +++ b/flight/net/Route.php @@ -44,17 +44,24 @@ class Route { */ public $splat; + /** + * @var boolean Pass self in callback parameters + */ + public $pass = false; + /** * Constructor. * * @param string $pattern URL pattern * @param mixed $callback Callback function * @param array $methods HTTP methods + * @param boolean $pass Pass self in callback parameters */ - public function __construct($pattern, $callback, $methods) { + public function __construct($pattern, $callback, $methods, $pass) { $this->pattern = $pattern; $this->callback = $callback; $this->methods = $methods; + $this->pass = $pass; } /** @@ -64,7 +71,11 @@ class Route { * @return boolean Match status */ public function matchUrl($url) { + // Wildcard or exact match if ($this->pattern === '*' || $this->pattern === $url) { + if ($this->pass) { + array_push($this->params, $this); + } return true; } @@ -102,6 +113,10 @@ class Route { $this->params[$k] = (array_key_exists($k, $matches)) ? urldecode($matches[$k]) : null; } + if ($this->pass) { + array_push($this->params, $this); + } + $this->regex = $regex; return true; diff --git a/flight/net/Router.php b/flight/net/Router.php index c04b5af..64313fc 100644 --- a/flight/net/Router.php +++ b/flight/net/Router.php @@ -38,7 +38,7 @@ class Router { } /** - * Clears all routes the router. + * Clears all routes in the router. */ public function clear() { $this->routes = array(); @@ -49,18 +49,19 @@ class Router { * * @param string $pattern URL pattern to match * @param callback $callback Callback function + * @param boolean $pass_route Pass the matching route object to the callback */ - public function map($pattern, $callback) { + public function map($pattern, $callback, $pass_route = false) { + $url = $pattern; + $methods = array('*'); + if (strpos($pattern, ' ') !== false) { list($method, $url) = explode(' ', trim($pattern), 2); $methods = explode('|', $method); - - array_push($this->routes, new Route($url, $callback, $methods)); - } - else { - array_push($this->routes, new Route($pattern, $callback, array('*'))); } + + array_push($this->routes, new Route($url, $callback, $methods, $pass_route)); } /** diff --git a/tests/RouterTest.php b/tests/RouterTest.php index 6ddd4c9..2f2f153 100644 --- a/tests/RouterTest.php +++ b/tests/RouterTest.php @@ -31,12 +31,11 @@ class RouterTest extends PHPUnit_Framework_TestCase echo 'OK'; } - // Checks if a route was matched - function check($str = 'OK'){ + // Checks if a route was matched with a given output + function check($str = ''){ $route = $this->router->route($this->request); $params = array_values($route->params); - array_push($params, $route); $this->assertTrue(is_callable($route->callback)); @@ -50,7 +49,7 @@ class RouterTest extends PHPUnit_Framework_TestCase $this->router->map('/', array($this, 'ok')); $this->request->url = '/'; - $this->check(); + $this->check('OK'); } // Simple path @@ -58,7 +57,7 @@ class RouterTest extends PHPUnit_Framework_TestCase $this->router->map('/path', array($this, 'ok')); $this->request->url = '/path'; - $this->check(); + $this->check('OK'); } // POST route @@ -67,7 +66,7 @@ class RouterTest extends PHPUnit_Framework_TestCase $this->request->url = '/'; $this->request->method = 'POST'; - $this->check(); + $this->check('OK'); } // Either GET or POST route @@ -76,7 +75,7 @@ class RouterTest extends PHPUnit_Framework_TestCase $this->request->url = '/'; $this->request->method = 'GET'; - $this->check(); + $this->check('OK'); } // Test regular expression matching @@ -84,7 +83,7 @@ class RouterTest extends PHPUnit_Framework_TestCase $this->router->map('/num/[0-9]+', array($this, 'ok')); $this->request->url = '/num/1234'; - $this->check(); + $this->check('OK'); } // Passing URL parameters @@ -146,6 +145,23 @@ class RouterTest extends PHPUnit_Framework_TestCase $this->router->map('/account/*', array($this, 'ok')); $this->request->url = '/account/123/abc/xyz'; + $this->check('OK'); + } + + // Check if route was passed + function testRoutePassing(){ + $this->router->map('/yes_route', function($route){ + $this->assertTrue(is_object($route)); + }, + true); + $this->request->url = '/yes_route'; + $this->check(); + + $this->router->map('/no_route', function($route = null){ + $this->assertTrue(is_null($route)); + }, + false); + $this->request->url = '/no_route'; $this->check(); } @@ -153,9 +169,11 @@ class RouterTest extends PHPUnit_Framework_TestCase function testSplatWildcard(){ $this->router->map('/account/*', function($route){ echo $route->splat; - }); - $this->request->url = '/account/123/abc/xyz'; + }, + true); + + $this->request->url = '/account/456/def/xyz'; - $this->check('123/abc/xyz'); + $this->check('456/def/xyz'); } }