Add convenience methods to Router

Code cleanup and inspection
This commit is contained in:
PJ Dietz 2014-06-28 20:12:55 -04:00
parent f60b010f11
commit 12b971dfe6
5 changed files with 26 additions and 69 deletions

View File

@ -1,19 +0,0 @@
<?php
/**
* pjdietz\WellRESTed\Interfaces\HandlerInterface
*
* @author PJ Dietz <pj@pjdietz.com>
* @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
{
}

View File

@ -1,44 +0,0 @@
<?php
/**
* pjdietz\WellRESTed\Interfaces\RouteTargetInterface
*
* @author PJ Dietz <pj@pjdietz.com>
* @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);
}

View File

@ -11,7 +11,6 @@
namespace pjdietz\WellRESTed;
use pjdietz\WellRESTed\Exceptions\CurlException;
use pjdietz\WellRESTed\Interfaces\RequestInterface;
use pjdietz\WellRESTed\Interfaces\RoutableInterface;
/**

View File

@ -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
*

View File

@ -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 {