Optimized for static routes

This commit is contained in:
Phil 2015-01-01 19:44:33 +00:00
parent a384a65346
commit b0c1330a26
2 changed files with 57 additions and 23 deletions

View File

@ -14,6 +14,7 @@ use pjdietz\WellRESTed\Exceptions\HttpExceptions\HttpException;
use pjdietz\WellRESTed\Interfaces\HandlerInterface; use pjdietz\WellRESTed\Interfaces\HandlerInterface;
use pjdietz\WellRESTed\Interfaces\RequestInterface; use pjdietz\WellRESTed\Interfaces\RequestInterface;
use pjdietz\WellRESTed\Interfaces\ResponseInterface; use pjdietz\WellRESTed\Interfaces\ResponseInterface;
use pjdietz\WellRESTed\Routes\StaticRoute;
/** /**
* Router * Router
@ -43,31 +44,38 @@ class Router implements HandlerInterface
*/ */
public function getResponse(RequestInterface $request, array $args = null) public function getResponse(RequestInterface $request, array $args = null)
{ {
foreach ($this->routes as $route) { $path = $request->getPath();
/** @var HandlerInterface $route */ if (array_key_exists($path, $this->routes)) {
try { $handler = new $this->routes[$path]();
$response = $route->getResponse($request, $args); $response = $handler->getResponse($request, $args);
} catch (HttpException $e) { } else {
$response = new Response(); foreach ($this->routes as $route) {
$response->setStatusCode($e->getCode()); /** @var HandlerInterface $route */
$response->setBody($e->getMessage()); try {
} $response = $route->getResponse($request, $args);
if ($response) { } catch (HttpException $e) {
// Check if the router has an error handler for this status code. $response = new Response();
$status = $response->getStatusCode(); $response->setStatusCode($e->getCode());
if (array_key_exists($status, $this->errorHandlers)) { $response->setBody($e->getMessage());
/** @var HandlerInterface $errorHandler */
$errorHandler = new $this->errorHandlers[$status]();
// Pass the response triggering this along to the error handler.
$errorArgs = array("response" => $response);
if ($args) {
$errorArgs = array_merge($args, $errorArgs);
}
return $errorHandler->getResponse($request, $errorArgs);
} }
return $response; if ($response) break;
} }
} }
if ($response) {
// Check if the router has an error handler for this status code.
$status = $response->getStatusCode();
if (array_key_exists($status, $this->errorHandlers)) {
/** @var HandlerInterface $errorHandler */
$errorHandler = new $this->errorHandlers[$status]();
// Pass the response triggering this along to the error handler.
$errorArgs = array("response" => $response);
if ($args) {
$errorArgs = array_merge($args, $errorArgs);
}
return $errorHandler->getResponse($request, $errorArgs);
}
return $response;
}
return null; return null;
} }
@ -78,7 +86,14 @@ class Router implements HandlerInterface
*/ */
public function addRoute(HandlerInterface $route) public function addRoute(HandlerInterface $route)
{ {
$this->routes[] = $route; if ($route instanceof StaticRoute) {
$handler = $route->getHandler();
foreach ($route->getPaths() as $path) {
$this->routes[$path] = $handler;
}
} else {
$this->routes[] = $route;
}
} }
/** /**

View File

@ -64,4 +64,23 @@ class StaticRoute extends BaseRoute
return null; return null;
} }
/**
* Returns the target class this maps to.
*
* @return HandlerInterface
*/
public function getHandler()
{
return $this->getTarget();
}
/**
* Returns the paths this maps to a target handler.
*
* @return array Array of paths.
*/
public function getPaths()
{
return $this->paths;
}
} }