Added ability for routes to return output

This commit is contained in:
Marcin Kurczewski 2014-08-30 12:52:44 +02:00
parent 011d803bd0
commit e81391a29a
3 changed files with 14 additions and 4 deletions

View file

@ -13,13 +13,13 @@ final class Route
$this->regex = $this->getRegex(); $this->regex = $this->getRegex();
} }
public function handle($query) public function handle($query, &$output)
{ {
$query = trim($query, '/'); $query = trim($query, '/');
if (!preg_match($this->regex, $query, $matches)) if (!preg_match($this->regex, $query, $matches))
return false; return false;
$routeArguments = $this->getRouteArguments($matches); $routeArguments = $this->getRouteArguments($matches);
call_user_func_array($this->route, $routeArguments); $output = call_user_func_array($this->route, $routeArguments);
return true; return true;
} }

View file

@ -37,8 +37,10 @@ final class Router
foreach ($this->routes[$method] as $route) foreach ($this->routes[$method] as $route)
{ {
if ($route->handle($request)) if ($route->handle($request, $output))
return; {
return $output;
}
} }
throw new \DomainException('Unhandled request address: ' . $request); throw new \DomainException('Unhandled request address: ' . $request);

View file

@ -97,4 +97,12 @@ final class PostDaoTest extends \PHPUnit_Framework_TestCase
$router->handle('GET', '/tests/test_id'); $router->handle('GET', '/tests/test_id');
$this->assertTrue($testOk); $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);
}
} }