Skip to content

Commit

Permalink
Merge pull request #62 from henriktudborg/master
Browse files Browse the repository at this point in the history
call $callback via call_user_func
  • Loading branch information
chriso committed Jan 7, 2013
2 parents 98804c2 + 375d18c commit 6f15be8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
6 changes: 3 additions & 3 deletions klein.php
Expand Up @@ -152,7 +152,7 @@ function dispatch($uri = null, $req_method = null, array $params = null, $captur
// Easily handle 404's
} elseif ($_route === '404' && !$matched && count($methods_matched) <= 0) {
try {
$callback($request, $response, $app, $matched, $methods_matched);
call_user_func($callback, $request, $response, $app, $matched, $methods_matched);
} catch (Exception $e) {
$response->error($e);
}
Expand All @@ -161,7 +161,7 @@ function dispatch($uri = null, $req_method = null, array $params = null, $captur
// Easily handle 405's
} elseif ($_route === '405' && !$matched && count($methods_matched) > 0) {
try {
$callback($request, $response, $app, $matched, $methods_matched);
call_user_func($callback, $request, $response, $app, $matched, $methods_matched);
} catch (Exception $e) {
$response->error($e);
}
Expand Down Expand Up @@ -224,7 +224,7 @@ function dispatch($uri = null, $req_method = null, array $params = null, $captur
$_REQUEST = array_merge($_REQUEST, $params);
}
try {
$callback($request, $response, $app, $matched, $methods_matched);
call_user_func($callback, $request, $response, $app, $matched, $methods_matched);
} catch (Exception $e) {
$response->error($e);
}
Expand Down
21 changes: 21 additions & 0 deletions tests/RoutesTest.php
Expand Up @@ -2,6 +2,12 @@

require_once dirname(__FILE__) . '/setup.php';

class TestClass {
static function GET($r, $r, $a) {
echo 'ok';
}
}

class RoutesTest extends PHPUnit_Framework_TestCase {
protected function setUp() {
global $__routes;
Expand All @@ -21,6 +27,21 @@ public function testBasic() {
dispatch( '/' );
}

public function testCallable() {
$this->expectOutputString( 'okok' );
respond( '/', array('TestClass', 'GET'));
respond( '/', 'TestClass::GET');
dispatch( '/' );
}

public function testAppReference() {
$this->expectOutputString( 'ab' );
respond( '/', function($r, $r ,$a){ $a->state = 'a'; });
respond( '/', function($r, $r ,$a){ $a->state .= 'b'; });
respond( '/', function($r, $r ,$a){ print $a->state; });
dispatch( '/' );
}

public function testCatchallImplicit() {
$this->expectOutputString( 'b' );

Expand Down

0 comments on commit 6f15be8

Please sign in to comment.