Optimized for static routes
This commit is contained in:
parent
a384a65346
commit
b0c1330a26
|
|
@ -14,6 +14,7 @@ use pjdietz\WellRESTed\Exceptions\HttpExceptions\HttpException;
|
|||
use pjdietz\WellRESTed\Interfaces\HandlerInterface;
|
||||
use pjdietz\WellRESTed\Interfaces\RequestInterface;
|
||||
use pjdietz\WellRESTed\Interfaces\ResponseInterface;
|
||||
use pjdietz\WellRESTed\Routes\StaticRoute;
|
||||
|
||||
/**
|
||||
* Router
|
||||
|
|
@ -43,31 +44,38 @@ class Router implements HandlerInterface
|
|||
*/
|
||||
public function getResponse(RequestInterface $request, array $args = null)
|
||||
{
|
||||
foreach ($this->routes as $route) {
|
||||
/** @var HandlerInterface $route */
|
||||
try {
|
||||
$response = $route->getResponse($request, $args);
|
||||
} catch (HttpException $e) {
|
||||
$response = new Response();
|
||||
$response->setStatusCode($e->getCode());
|
||||
$response->setBody($e->getMessage());
|
||||
}
|
||||
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);
|
||||
$path = $request->getPath();
|
||||
if (array_key_exists($path, $this->routes)) {
|
||||
$handler = new $this->routes[$path]();
|
||||
$response = $handler->getResponse($request, $args);
|
||||
} else {
|
||||
foreach ($this->routes as $route) {
|
||||
/** @var HandlerInterface $route */
|
||||
try {
|
||||
$response = $route->getResponse($request, $args);
|
||||
} catch (HttpException $e) {
|
||||
$response = new Response();
|
||||
$response->setStatusCode($e->getCode());
|
||||
$response->setBody($e->getMessage());
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
|
|
@ -78,7 +86,14 @@ class Router implements HandlerInterface
|
|||
*/
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -64,4 +64,23 @@ class StaticRoute extends BaseRoute
|
|||
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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue