From f60b010f1127aaffaf8a78b7c0db0186b13ced0c Mon Sep 17 00:00:00 2001 From: PJ Dietz Date: Sat, 28 Jun 2014 20:03:36 -0400 Subject: [PATCH] Update Router class to match new DispatcherInterface --- src/pjdietz/WellRESTed/Router.php | 64 ++++++------------------------- 1 file changed, 11 insertions(+), 53 deletions(-) diff --git a/src/pjdietz/WellRESTed/Router.php b/src/pjdietz/WellRESTed/Router.php index 5a1c882..401424d 100644 --- a/src/pjdietz/WellRESTed/Router.php +++ b/src/pjdietz/WellRESTed/Router.php @@ -13,14 +13,13 @@ namespace pjdietz\WellRESTed; use pjdietz\WellRESTed\Interfaces\DispatcherInterface; use pjdietz\WellRESTed\Interfaces\ResponseInterface; use pjdietz\WellRESTed\Interfaces\RoutableInterface; -use pjdietz\WellRESTed\Interfaces\RouteTargetInterface; /** * Router * * A Router uses a table of Routes to find the appropriate Handler for a request. */ -class Router extends RouteTarget implements DispatcherInterface +class Router implements DispatcherInterface { /** @var array Array of Route objects */ private $routes; @@ -35,62 +34,21 @@ class Router extends RouteTarget implements DispatcherInterface * Return the response built by the handler based on the request * * @param RoutableInterface $request + * @param null $args * @return ResponseInterface */ - public function getResponse(RoutableInterface $request = null) + public function getResponse(RoutableInterface $request = null, $args = null) { // Use the singleton if the caller did not pass a request. - if (is_null($reqs)) - - // Set the instance's request, if the called passed one. - if (!is_null($request)) { - $this->request = $request; - } - - // If the instance does not have a request, use the singleton. - if (is_null($this->request)) { - $this->request = Request::getRequest(); - } - - // Reference the request and path. - $request = $this->request; - $request->incrementRouteDepth(); - $path = $request->getPath(); - - if ($request->getRouteDepth() >= $this->getMaxDepth()) { - return $this->getInternalServerErrorResponse($request, 'Maximum route recursion reached.'); + if (is_null($request)) { + $request = Request::getRequest(); } foreach ($this->routes as $route) { - /** @var RouteInterface $route */ - if (preg_match($route->getPattern(), $path, $matches)) { - $targetClassName = $route->getTarget(); - if (is_subclass_of($targetClassName, self::ROUTE_TARGET_INTERFACE)) { - - /** @var RouteTargetInterface $target */ - $target = new $targetClassName(); - $target->setRequest($request); - - // If this instance already had arguments, merge the matches with them. - $myArguments = $this->getArguments(); - if (!is_null($myArguments)) { - $matches = array_merge($myArguments, $matches); - } - $target->setArguments($matches); - - // If this instance already had a top-level router, pass it along. - // Otherwise, pass itself as the top-level router. - if (isset($this->router)) { - $target->setRouter($this->router); - } else { - $target->setRouter($this); - } - - return $target->getResponse(); - - } else { - return $this->getInternalServerErrorResponse($request); - } + /** @var DispatcherInterface $route */ + $responce = $route->getResponse($request, $args); + if ($responce) { + return $responce; } } @@ -100,7 +58,7 @@ class Router extends RouteTarget implements DispatcherInterface /** * Append a new Route instance to the Router's route table. * - * @param RouteInterface $route + * @param DispatcherInterface $route */ public function addRoute(DispatcherInterface $route) { @@ -116,7 +74,7 @@ class Router extends RouteTarget implements DispatcherInterface protected function getNoRouteResponse(RoutableInterface $request) { $response = new Response(404); - $response->body = 'No resource at ' . $request->getPath(); + $response->setBody('No resource at ' . $request->getPath()); return $response; }