Skip to content

Commit

Permalink
transfer $router to hooks, and store current params in $router->params.
Browse files Browse the repository at this point in the history
  • Loading branch information
lloydzhou committed Apr 14, 2016
1 parent 6acfc3c commit fe25fc5
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 19 deletions.
12 changes: 6 additions & 6 deletions example.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ public static function hello_again($name){
->error(406, function($message){
die($message);
})
->hook('auth', function($params){
if ('lloyd' == $params['name'])
return $params;
$params['router']->error(401, 'Forbiden');
->hook('auth', function($router){
if ('lloyd' == $router->params['name'])
return true;
$router->error(401, 'Forbiden');
})
->hook('after', function($result, $router){
if ($result) {
Expand All @@ -50,9 +50,9 @@ public static function hello_again($name){
else print json_encode($result);
}
})
->hook('before', function($params){
->hook('before', function($router){
//$params['name'] = 'lloydzhou';
return $params;
return true;
})
->get('/', function(){
echo "Hello world !!!";
Expand Down
8 changes: 4 additions & 4 deletions router.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
class Router {
protected $prefix = '';
public $prefix_hook = array();
protected $prefix_hook = array();
protected $_tree = array();
protected $_events = array();
protected $_ctypes = array('A' => 'alnum', 'a' => 'alpha', 'd' => 'digit', 'x' => 'xdigit', 'l' => 'lower', 'u' => 'upper');
Expand Down Expand Up @@ -82,9 +82,9 @@ public function execute($params=array(), $method=null, $path=null){
$input = ((isset($_SERVER['HTTP_CONTENT_TYPE']) && 'application/json' == $_SERVER['HTTP_CONTENT_TYPE'])
|| (isset($_SERVER['CONTENT_TYPE']) && 'application/json' == $_SERVER['CONTENT_TYPE']))
? (array)json_decode(file_get_contents('php://input'), true) : array();
$params = array_merge($params, $_SERVER, $_REQUEST, $input, $_FILES, $_COOKIE, isset($_SESSION)?$_SESSION:array(), array('router'=>$this));
$this->params = array_merge($params, $_SERVER, $_REQUEST, $input, $_FILES, $_COOKIE, isset($_SESSION)?$_SESSION:array(), array('router'=>$this));
foreach(array_merge(array('before'), $hook) as $i=>$h){
if (!($params = $this->hook($h, $params))) return $this->error(406, "Failed to execute hook: $h");
if (false === $this->hook($h, $this)) return $this->error(406, "Failed to execute hook: $h");
}
/**
* auto get the variable list based on the callback handler parameter list.
Expand All @@ -95,7 +95,7 @@ public function execute($params=array(), $method=null, $path=null){
$args = $ref->getParameters();
array_walk($args, function(&$p, $i, $params){
$p = isset($params[$p->getName()]) ? $params[$p->getName()] : ($p->isOptional() ? $p->getDefaultValue() : null);
}, $params);
}, $this->params);
/* execute the callback handler and pass the result into "after" hook handler.*/
return $this->hook('after', call_user_func_array($cb, $args), $this);
}
Expand Down
15 changes: 6 additions & 9 deletions test/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,22 +149,19 @@ public function testParamExecute1Dispatched(){
// test hooks
public function testBeforeHook(){
$r = $this->router();
$r->hook('before', function($params){
$params['ext'] = 'json';
return $params;
$r->hook('before', function($router){
$router->params['ext'] = 'json';
});
$r->get('/hello/:name', function($name, $ext){ return $name. '.'. $ext; });
$response = $r->execute(array('ext'=>'js'), 'GET', '/hello/lloyd');
$this->assertEquals('lloyd.json',$response);
}
public function testCustomerHookGroup(){
$r = $this->router();
$r->hook('add', function($params){
$params['result'] = $params['arg1'] + $params['arg2'];
return $params;
})->hook('del', function($params){
$params['result'] = $params['arg1'] - $params['arg2'];
return $params;
$r->hook('add', function($router){
$router->params['result'] = $router->params['arg1'] + $router->params['arg2'];
})->hook('del', function($router){
$router->params['result'] = $router->params['arg1'] - $router->params['arg2'];
});
// test group width hook
$r->group('/add', 'add')->get('/:arg1:d/:arg2:d', function($arg1, $arg2, $result){ return $result; });
Expand Down

0 comments on commit fe25fc5

Please sign in to comment.