Add interfaces for Route and Handler
This commit is contained in:
parent
47ec8f5f96
commit
b28b53aa11
|
|
@ -15,8 +15,15 @@ namespace pjdietz\WellRESTed;
|
||||||
*
|
*
|
||||||
* @property-read Response response The Response to the request
|
* @property-read Response response The Response to the request
|
||||||
*/
|
*/
|
||||||
abstract class Handler
|
abstract class Handler implements HandlerInterface
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* Matches array from the preg_match() call used to find this Handler.
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected $args;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The HTTP request to respond to.
|
* The HTTP request to respond to.
|
||||||
*
|
*
|
||||||
|
|
@ -31,32 +38,6 @@ abstract class Handler
|
||||||
*/
|
*/
|
||||||
protected $response;
|
protected $response;
|
||||||
|
|
||||||
/**
|
|
||||||
* Matches array from the preg_match() call used to find this Handler.
|
|
||||||
*
|
|
||||||
* @var array
|
|
||||||
*/
|
|
||||||
protected $args;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a new Handler for a specific request.
|
|
||||||
*
|
|
||||||
* @param Request $request
|
|
||||||
* @param array $args
|
|
||||||
*/
|
|
||||||
public function __construct($request, $args = null)
|
|
||||||
{
|
|
||||||
$this->request = $request;
|
|
||||||
|
|
||||||
if (is_null($args)) {
|
|
||||||
$args = array();
|
|
||||||
}
|
|
||||||
$this->args = $args;
|
|
||||||
|
|
||||||
$this->response = new Response();
|
|
||||||
$this->buildResponse();
|
|
||||||
}
|
|
||||||
|
|
||||||
// -------------------------------------------------------------------------
|
// -------------------------------------------------------------------------
|
||||||
// Accessors
|
// Accessors
|
||||||
|
|
||||||
|
|
@ -75,6 +56,38 @@ abstract class Handler
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $args
|
||||||
|
*/
|
||||||
|
public function setArguments(array $args)
|
||||||
|
{
|
||||||
|
$this->args = $args;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getArguments()
|
||||||
|
{
|
||||||
|
return $this->args;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param \pjdietz\WellRESTed\Request $request
|
||||||
|
*/
|
||||||
|
public function setRequest($request)
|
||||||
|
{
|
||||||
|
$this->request = $request;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return \pjdietz\WellRESTed\Request
|
||||||
|
*/
|
||||||
|
public function getRequest()
|
||||||
|
{
|
||||||
|
return $this->request;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the instance's Reponse
|
* Return the instance's Reponse
|
||||||
*
|
*
|
||||||
|
|
@ -82,6 +95,8 @@ abstract class Handler
|
||||||
*/
|
*/
|
||||||
public function getResponse()
|
public function getResponse()
|
||||||
{
|
{
|
||||||
|
$this->response = new Response();
|
||||||
|
$this->buildResponse();
|
||||||
return $this->response;
|
return $this->response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,12 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace pjdietz\WellRESTed;
|
||||||
|
|
||||||
|
interface HandlerInterface
|
||||||
|
{
|
||||||
|
public function getRequest();
|
||||||
|
public function setRequest($request);
|
||||||
|
public function getArguments();
|
||||||
|
public function setArguments(array $args);
|
||||||
|
public function getResponse();
|
||||||
|
}
|
||||||
|
|
@ -15,7 +15,7 @@ use \Exception;
|
||||||
/**
|
/**
|
||||||
* A Route connects a URI pattern to a Handler.
|
* A Route connects a URI pattern to a Handler.
|
||||||
*/
|
*/
|
||||||
class Route
|
class Route implements RouteInterface
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Regular expression matching URL friendly characters (i.e., letters,
|
* Regular expression matching URL friendly characters (i.e., letters,
|
||||||
|
|
@ -47,14 +47,14 @@ class Route
|
||||||
*
|
*
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
public $pattern;
|
private $pattern;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Name of the Handler class to use
|
* Name of the Handler class to use
|
||||||
*
|
*
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
public $handler;
|
private $handler;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new Route
|
* Create a new Route
|
||||||
|
|
@ -68,6 +68,38 @@ class Route
|
||||||
$this->handler = $handler;
|
$this->handler = $handler;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $handler
|
||||||
|
*/
|
||||||
|
public function setHandler($handler)
|
||||||
|
{
|
||||||
|
$this->handler = $handler;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getHandler()
|
||||||
|
{
|
||||||
|
return $this->handler;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $pattern
|
||||||
|
*/
|
||||||
|
public function setPattern($pattern)
|
||||||
|
{
|
||||||
|
$this->pattern = $pattern;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getPattern()
|
||||||
|
{
|
||||||
|
return $this->pattern;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new Route using a URI template to generate the pattern.
|
* Create a new Route using a URI template to generate the pattern.
|
||||||
*
|
*
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,37 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace pjdietz\WellRESTed;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Interface for a route to relate a pattern for matching a URI to a handler class.
|
||||||
|
*/
|
||||||
|
interface RouteInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Return the regex pattern used to match the URI
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getPattern();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Provide a regex pattern that matches the URI
|
||||||
|
*
|
||||||
|
* @para string $pattern
|
||||||
|
*/
|
||||||
|
public function setPattern($pattern);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the name of the class the route will dispatch.
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getHandler();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Provide the classname to instantiate to handle the route.
|
||||||
|
*
|
||||||
|
* @param string $className
|
||||||
|
*/
|
||||||
|
public function setHandler($className);
|
||||||
|
}
|
||||||
|
|
@ -36,9 +36,9 @@ class Router
|
||||||
/**
|
/**
|
||||||
* Append a new Route instance to the Router's route table.
|
* Append a new Route instance to the Router's route table.
|
||||||
*
|
*
|
||||||
* @param Route $route
|
* @param RouteInterface $route
|
||||||
*/
|
*/
|
||||||
public function addRoute(Route $route)
|
public function addRoute(RouteInterface $route)
|
||||||
{
|
{
|
||||||
$this->routes[] = $route;
|
$this->routes[] = $route;
|
||||||
}
|
}
|
||||||
|
|
@ -58,10 +58,15 @@ class Router
|
||||||
$path = $request->path;
|
$path = $request->path;
|
||||||
|
|
||||||
foreach ($this->routes as $route) {
|
foreach ($this->routes as $route) {
|
||||||
if (preg_match($route->pattern, $path, $matches)) {
|
/** @var RouteInterface $route */
|
||||||
if (is_subclass_of($route->handler, '\pjdietz\WellRESTed\Handler')) {
|
if (preg_match($route->getPattern(), $path, $matches)) {
|
||||||
$handler = new $route->handler($request, $matches);
|
$handlerClassName = $route->getHandler();
|
||||||
return $handler->response;
|
if (is_subclass_of($handlerClassName, '\pjdietz\WellRESTed\HandlerInterface')) {
|
||||||
|
/** @var HandlerInterface $handler */
|
||||||
|
$handler = new $handlerClassName();
|
||||||
|
$handler->setRequest($request);
|
||||||
|
$handler->setArguments($matches);
|
||||||
|
return $handler->getResponse();
|
||||||
} else {
|
} else {
|
||||||
return $this->getNoRouteResponse($request);
|
return $this->getNoRouteResponse($request);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue