Allow Routers to dispatch additional Routers

- RouterInterface and HandlerInterface now share a parent, RouteTargetInterface.
- A Router may now be used as the target for a Route.
- Route's handler member is not replaced by Router's target member.
- Route::getHandler() and Route::setHandler() are deprecated and alias getTarget() and setTarget()
This commit is contained in:
PJ Dietz 2013-08-16 16:51:09 -04:00
parent dbd4ff96a5
commit 6013198436
8 changed files with 194 additions and 143 deletions

View File

@ -13,81 +13,23 @@ namespace pjdietz\WellRESTed;
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\Interfaces\RouterInterface;
/** /**
* A Handler issues a response for a given resource. * A Handler issues a response for a given resource.
* *
* @property-read ResponseInterface response The Response to the request * @property-read ResponseInterface response The Response to the request
*/ */
abstract class Handler implements HandlerInterface abstract class Handler extends RouteTarget implements HandlerInterface
{ {
/** @var array Matches array from the preg_match() call used to find this Handler */
protected $args;
/** @var RequestInterface The HTTP request to respond to. */
protected $request;
/** @var ResponseInterface The HTTP response to send based on the request. */
protected $response;
/** @var RouterInterface The router that dispatched this handler */
protected $router;
// -------------------------------------------------------------------------
// Accessors
/** /**
* Magic function for properties * @param RequestInterface $request
* * @return ResponseInterface
* @param string $propertyName
* @return mixed
*/ */
public function __get($propertyName) public function getResponse(RequestInterface $request = null)
{ {
$method = 'get' . ucfirst($propertyName); if (!is_null($request)) {
if (method_exists($this, $method)) { $this->request = $request;
return $this->{$method}();
} }
return null;
}
/** @param array $args */
public function setArguments(array $args)
{
$this->args = $args;
}
/** @return array */
public function getArguments()
{
return $this->args;
}
/** @return RequestInterface */
public function getRequest()
{
return $this->request;
}
/** @param RequestInterface $request */
public function setRequest(RequestInterface $request)
{
$this->request = $request;
}
/** @return RouterInterface */
public function getRouter()
{
return $this->router;
}
/** @param RouterInterface $router */
public function setRouter(RouterInterface $router)
{
$this->router = $router;
}
/** @return ResponseInterface */
public function getResponse()
{
$this->response = new Response(); $this->response = new Response();
$this->buildResponse(); $this->buildResponse();
return $this->response; return $this->response;
@ -127,15 +69,6 @@ abstract class Handler implements HandlerInterface
} }
} }
// -------------------------------------------------------------------------
// HTTP Methods
// Each of these methods corresponds to a standard HTTP method. Each method
// has no arguments and returns nothing, but should affect the instance's
// response member.
//
// By default, the methods will provide a 405 Method Not Allowed header.
/** /**
* Method for handling HTTP GET requests. * Method for handling HTTP GET requests.
* *

View File

@ -14,26 +14,6 @@ namespace pjdietz\WellRESTed\Interfaces;
* Interface for a creating a response in reaction to a request or arguments. * Interface for a creating a response in reaction to a request or arguments.
* @package pjdietz\WellRESTed * @package pjdietz\WellRESTed
*/ */
interface HandlerInterface interface HandlerInterface extends 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 RequestInterface Request used to obtain a response */
public function getRequest();
/** @param RequestInterface $request Request used to obtain a response */
public function setRequest(RequestInterface $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 ResponseInterface Response obtained based on the args and request */
public function getResponse();
} }

View File

@ -23,8 +23,8 @@ interface RouteInterface
public function setPattern($pattern); public function setPattern($pattern);
/** @return string Fully qualified name of the class the route will dispatch. */ /** @return string Fully qualified name of the class the route will dispatch. */
public function getHandler(); public function getTarget();
/** @param string $className Fully qualified name of the class the route will dispatch. */ /** @param string $className Fully qualified name of the class the route will dispatch. */
public function setHandler($className); public function setTarget($className);
} }

View File

@ -0,0 +1,44 @@
<?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 RequestInterface Request used to obtain a response */
public function getRequest();
/** @param RequestInterface $request Request used to obtain a response */
public function setRequest(RequestInterface $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 RequestInterface $request
* @return ResponseInterface
*/
public function getResponse(RequestInterface $request = null);
}

View File

@ -14,13 +14,6 @@ namespace pjdietz\WellRESTed\Interfaces;
* The RouterInterface provides a mechanism for obtaining a response given a request. * The RouterInterface provides a mechanism for obtaining a response given a request.
* @package pjdietz\WellRESTed * @package pjdietz\WellRESTed
*/ */
interface RouterInterface interface RouterInterface extends RouteTargetInterface
{ {
/** }
* Return the response for the given request.
*
* @param RequestInterface $request
* @return ResponseInterface
*/
public function getResponse(RequestInterface $request = null);
}

View File

@ -44,36 +44,36 @@ class Route implements RouteInterface
*/ */
private $pattern; private $pattern;
/** /**
* Name of the Handler class to use * Name of the RouteTarget class to dispatch when the pattern is matched.
* *
* @var string * @var string
*/ */
private $handler; private $target;
/** /**
* Create a new Route * Create a new Route
* *
* @param $pattern * @param string $pattern Regex string to match against the request path
* @param $handler * @param string $target String name of the RouteTargetInterface to dispatch
*/ */
public function __construct($pattern, $handler) public function __construct($pattern, $target)
{ {
$this->pattern = $pattern; $this->pattern = $pattern;
$this->handler = $handler; $this->target = $target;
} }
/** /**
* Create a new Route using a URI template to generate the pattern. * Create a new Route using a URI template to generate the pattern.
* *
* @param string $uriTemplate * @param string $uriTemplate
* @param string $handler * @param string $target
* @param array $variables * @param array $variables
* @throws WellRESTedException * @throws WellRESTedException
* @return Route * @return Route
*/ */
static public function newFromUriTemplate( static public function newFromUriTemplate(
$uriTemplate, $uriTemplate,
$handler, $target,
$variables = null $variables = null
) { ) {
@ -129,26 +129,7 @@ class Route implements RouteInterface
$pattern = '/^' . $pattern . '$/'; $pattern = '/^' . $pattern . '$/';
$klass = __CLASS__; return new self($pattern, $target);
$route = new $klass($pattern, $handler);
return $route;
}
/**
* @return string
*/
public function getHandler()
{
return $this->handler;
}
/**
* @param string $handler
*/
public function setHandler($handler)
{
$this->handler = $handler;
} }
/** /**
@ -167,4 +148,36 @@ class Route implements RouteInterface
$this->pattern = $pattern; $this->pattern = $pattern;
} }
/** @return string fully qualified name of the RouteTargetInterface to dispatch */
public function getTarget()
{
return $this->target;
}
/** @param string $target fully qualified name of the RouteTargetInterface to dispatch */
public function setTarget($target)
{
$this->target = $target;
}
/**
* @return string
* @deprecated 1.3.0
* @see \pjdietz\WellRESTed\Route::getTarget()
*/
public function getHandler()
{
return $this->getTarget();
}
/**
* @param string $handler
* @deprecated 1.3.0
* @see \pjdietz\WellRESTed\Route::setTarget()
*/
public function setHandler($handler)
{
$this->setTarget($handler);
}
} }

View File

@ -0,0 +1,70 @@
<?php
/**
* pjdietz\WellRESTed\RouteTarget
*
* @author PJ Dietz <pj@pjdietz.com>
* @copyright Copyright 2013 by PJ Dietz
* @license MIT
*/
namespace pjdietz\WellRESTed;
use pjdietz\WellRESTed\Interfaces\RequestInterface;
use pjdietz\WellRESTed\Interfaces\ResponseInterface;
use pjdietz\WellRESTed\Interfaces\RouterInterface;
use pjdietz\WellRESTed\Interfaces\RouteTargetInterface;
/**
* RouteTarget
*
* RouteTarget defines the basic functionality for an instance dispatched when a Route pattern
* is matched.
*/
abstract class RouteTarget implements RouteTargetInterface
{
/** @var array Matches array from the preg_match() call used to find this Handler */
protected $args;
/** @var RequestInterface The HTTP request to respond to. */
protected $request;
/** @var ResponseInterface The HTTP response to send based on the request. */
protected $response;
/** @var RouterInterface The router that dispatched this handler */
protected $router;
/** @param array $args */
public function setArguments(array $args)
{
$this->args = $args;
}
/** @return array */
public function getArguments()
{
return $this->args;
}
/** @return RequestInterface */
public function getRequest()
{
return $this->request;
}
/** @param RequestInterface $request */
public function setRequest(RequestInterface $request)
{
$this->request = $request;
}
/** @return RouterInterface */
public function getRouter()
{
return $this->router;
}
/** @param RouterInterface $router */
public function setRouter(RouterInterface $router)
{
$this->router = $router;
}
}

View File

@ -15,16 +15,17 @@ use pjdietz\WellRESTed\Interfaces\RequestInterface;
use pjdietz\WellRESTed\Interfaces\ResponseInterface; use pjdietz\WellRESTed\Interfaces\ResponseInterface;
use pjdietz\WellRESTed\Interfaces\RouteInterface; use pjdietz\WellRESTed\Interfaces\RouteInterface;
use pjdietz\WellRESTed\Interfaces\RouterInterface; use pjdietz\WellRESTed\Interfaces\RouterInterface;
use pjdietz\WellRESTed\Interfaces\RouteTargetInterface;
/** /**
* Router * Router
* *
* A Router uses a table of Routes to find the appropriate Handler for a request. * A Router uses a table of Routes to find the appropriate Handler for a request.
*/ */
class Router implements RouterInterface class Router extends RouteTarget implements RouterInterface
{ {
/** @var string Fully qualified name for the interface for handlers */ /** @var string Fully qualified name for the interface for handlers */
const HANDLER_INTERFACE = '\\pjdietz\\WellRESTed\\Interfaces\\HandlerInterface'; const ROUTE_TARGET_INTERFACE = '\\pjdietz\\WellRESTed\\Interfaces\\RouteTargetInterface';
/** @var array Array of Route objects */ /** @var array Array of Route objects */
private $routes; private $routes;
@ -61,19 +62,36 @@ class Router implements RouterInterface
foreach ($this->routes as $route) { foreach ($this->routes as $route) {
/** @var RouteInterface $route */ /** @var RouteInterface $route */
if (preg_match($route->getPattern(), $path, $matches)) { if (preg_match($route->getPattern(), $path, $matches)) {
$handlerClassName = $route->getHandler(); $targetClassName = $route->getTarget();
if (is_subclass_of($handlerClassName, self::HANDLER_INTERFACE)) { if (is_subclass_of($targetClassName, self::ROUTE_TARGET_INTERFACE)) {
/** @var HandlerInterface $handler */
$handler = new $handlerClassName(); /** @var RouteTargetInterface $target */
$handler->setRequest($request); $target = new $targetClassName();
$handler->setArguments($matches); $target->setRequest($request);
$handler->setRouter($this);
return $handler->getResponse(); // If this instance already had argument, 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 { } else {
return $this->getInternalServerErrorResponse($request); return $this->getInternalServerErrorResponse($request);
} }
} }
} }
return $this->getNoRouteResponse($request); return $this->getNoRouteResponse($request);
} }