From 12b971dfe6aa367dbe0f99228647e64d57a51b18 Mon Sep 17 00:00:00 2001 From: PJ Dietz Date: Sat, 28 Jun 2014 20:12:55 -0400 Subject: [PATCH] Add convenience methods to Router Code cleanup and inspection --- .../Interfaces/HandlerInterface.php | 19 -------- .../Interfaces/RouteTargetInterface.php | 44 ------------------- src/pjdietz/WellRESTed/Request.php | 1 - src/pjdietz/WellRESTed/Router.php | 23 +++++++++- src/pjdietz/WellRESTed/Routes/BaseRoute.php | 8 ++-- 5 files changed, 26 insertions(+), 69 deletions(-) delete mode 100644 src/pjdietz/WellRESTed/Interfaces/HandlerInterface.php delete mode 100644 src/pjdietz/WellRESTed/Interfaces/RouteTargetInterface.php diff --git a/src/pjdietz/WellRESTed/Interfaces/HandlerInterface.php b/src/pjdietz/WellRESTed/Interfaces/HandlerInterface.php deleted file mode 100644 index 4bd580d..0000000 --- a/src/pjdietz/WellRESTed/Interfaces/HandlerInterface.php +++ /dev/null @@ -1,19 +0,0 @@ - - * @copyright Copyright 2013 by PJ Dietz - * @license MIT - */ - -namespace pjdietz\WellRESTed\Interfaces; - -/** - * Interface for a creating a response in reaction to a request or arguments. - * @package pjdietz\WellRESTed - */ -interface HandlerInterface extends RouteTargetInterface -{ -} diff --git a/src/pjdietz/WellRESTed/Interfaces/RouteTargetInterface.php b/src/pjdietz/WellRESTed/Interfaces/RouteTargetInterface.php deleted file mode 100644 index 0499ce6..0000000 --- a/src/pjdietz/WellRESTed/Interfaces/RouteTargetInterface.php +++ /dev/null @@ -1,44 +0,0 @@ - - * @copyright Copyright 2013 by PJ Dietz - * @license MIT - */ - -namespace pjdietz\WellRESTed\Interfaces; - -/** - * The RouteTargetInterface provides a mechanism for obtaining a response given a request. - * @package pjdietz\WellRESTed - */ -interface RouteTargetInterface -{ - /** @return array Associative array used to obtain a response */ - public function getArguments(); - - /** @param array $args Associative array used to obtain a response */ - public function setArguments(array $args); - - /** @return RoutableInterface Request used to obtain a response */ - public function getRequest(); - - /** @param RoutableInterface $request Request used to obtain a response */ - public function setRequest(RoutableInterface $request); - - /** @return RouterInterface Reference to the router used to dispatch this handler */ - public function getRouter(); - - /** @param RouterInterface $router Request used to obtain a response */ - public function setRouter(RouterInterface $router); - - /** - * Return the response for the given request. - * - * @param RoutableInterface $request - * @return ResponseInterface - */ - public function getResponse(RoutableInterface $request = null); -} diff --git a/src/pjdietz/WellRESTed/Request.php b/src/pjdietz/WellRESTed/Request.php index de410a4..b5f3bb5 100644 --- a/src/pjdietz/WellRESTed/Request.php +++ b/src/pjdietz/WellRESTed/Request.php @@ -11,7 +11,6 @@ namespace pjdietz\WellRESTed; use pjdietz\WellRESTed\Exceptions\CurlException; -use pjdietz\WellRESTed\Interfaces\RequestInterface; use pjdietz\WellRESTed\Interfaces\RoutableInterface; /** diff --git a/src/pjdietz/WellRESTed/Router.php b/src/pjdietz/WellRESTed/Router.php index 401424d..b638fd5 100644 --- a/src/pjdietz/WellRESTed/Router.php +++ b/src/pjdietz/WellRESTed/Router.php @@ -37,7 +37,7 @@ class Router implements DispatcherInterface * @param null $args * @return ResponseInterface */ - public function getResponse(RoutableInterface $request = null, $args = null) + public function getResponse(RoutableInterface $request, $args = null) { // Use the singleton if the caller did not pass a request. if (is_null($request)) { @@ -56,7 +56,7 @@ class Router implements DispatcherInterface } /** - * Append a new Route instance to the Router's route table. + * Append a new route to the route route table. * * @param DispatcherInterface $route */ @@ -65,6 +65,25 @@ class Router implements DispatcherInterface $this->routes[] = $route; } + /** + * Append a series of routes. + * + * @param array $routes List array of DispatcherInterface instances + */ + public function addRoutes(array $routes) + { + foreach ($routes as $route) { + if ($route instanceof DispatcherInterface) { + $this->addRoute($route); + } + } + } + + public function respond() { + $response = $this->getResponse(Request::getRequest()); + $response->respond(); + } + /** * Prepare a resonse indicating a 404 Not Found error * diff --git a/src/pjdietz/WellRESTed/Routes/BaseRoute.php b/src/pjdietz/WellRESTed/Routes/BaseRoute.php index 4c49bdb..39578bd 100644 --- a/src/pjdietz/WellRESTed/Routes/BaseRoute.php +++ b/src/pjdietz/WellRESTed/Routes/BaseRoute.php @@ -3,8 +3,6 @@ namespace pjdietz\WellRESTed\Routes; use pjdietz\WellRESTed\Interfaces\DispatcherInterface; -use pjdietz\WellRESTed\Interfaces\RoutableInterface; -use pjdietz\WellRESTed\Interfaces\RouteTargetInterface; /** * Base class for Routes. @@ -26,10 +24,14 @@ abstract class BaseRoute implements DispatcherInterface $this->targetClassName = $targetClassName; } + /** + * @return DispatcherInterface + * @throws \UnexpectedValueException + */ protected function getTarget() { if (is_subclass_of($this->targetClassName, self::DISPATCHER_INTERFACE)) { - /** @var RouteTargetInterface $target */ + /** @var DispatcherInterface $target */ $target = new $this->targetClassName(); return $target; } else {