From e81391a29a23861d2632ca03cbf04a5a82a12113 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Sat, 30 Aug 2014 12:52:44 +0200 Subject: [PATCH] Added ability for routes to return output --- src/Route.php | 4 ++-- src/Router.php | 6 ++++-- tests/RouterTest.php | 8 ++++++++ 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/Route.php b/src/Route.php index e3359cce..dc870ffe 100644 --- a/src/Route.php +++ b/src/Route.php @@ -13,13 +13,13 @@ final class Route $this->regex = $this->getRegex(); } - public function handle($query) + public function handle($query, &$output) { $query = trim($query, '/'); if (!preg_match($this->regex, $query, $matches)) return false; $routeArguments = $this->getRouteArguments($matches); - call_user_func_array($this->route, $routeArguments); + $output = call_user_func_array($this->route, $routeArguments); return true; } diff --git a/src/Router.php b/src/Router.php index a8683702..d1223a18 100644 --- a/src/Router.php +++ b/src/Router.php @@ -37,8 +37,10 @@ final class Router foreach ($this->routes[$method] as $route) { - if ($route->handle($request)) - return; + if ($route->handle($request, $output)) + { + return $output; + } } throw new \DomainException('Unhandled request address: ' . $request); diff --git a/tests/RouterTest.php b/tests/RouterTest.php index 3f4526ca..588cfde7 100644 --- a/tests/RouterTest.php +++ b/tests/RouterTest.php @@ -97,4 +97,12 @@ final class PostDaoTest extends \PHPUnit_Framework_TestCase $router->handle('GET', '/tests/test_id'); $this->assertTrue($testOk); } + + public function testOutputHandling() + { + $router = new \Szurubooru\Router; + $router->get('/test', function() { return 'ok'; }); + $output = $router->handle('GET', '/test'); + $this->assertEquals('ok', $output); + } }