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().
pull/126/head
Mike Cao 11 years ago
parent ab2aba4e7d
commit d84e51ba47

@ -1 +1 @@
1.1.9
1.1.10

@ -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);
}
/**

@ -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;

@ -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));
}
/**

@ -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');
}
}

Loading…
Cancel
Save