Rearrange Route
This commit is contained in:
parent
259dd0baa1
commit
b350693aca
|
|
@ -24,17 +24,28 @@ use pjdietz\WellRESTed\Routes\StaticRoute;
|
||||||
*/
|
*/
|
||||||
class Router implements HandlerInterface
|
class Router implements HandlerInterface
|
||||||
{
|
{
|
||||||
/** @var array Hash array HTTP verb => RouteTable */
|
|
||||||
private $routeTables;
|
|
||||||
|
|
||||||
/** @var array Hash array of status code => error handler */
|
/** @var array Hash array of status code => error handler */
|
||||||
private $errorHandlers;
|
private $errorHandlers;
|
||||||
|
/** @var array Hash array HTTP verb => RouteTable */
|
||||||
|
private $routeTables;
|
||||||
|
|
||||||
/** Create a new Router. */
|
/** Create a new Router. */
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
$this->routeTables = array();
|
|
||||||
$this->errorHandlers = array();
|
$this->errorHandlers = array();
|
||||||
|
$this->routeTables = array();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Append a new route to the route table.
|
||||||
|
*
|
||||||
|
* @param HandlerInterface $route
|
||||||
|
* @param string $method HTTP Method; * for any
|
||||||
|
*/
|
||||||
|
public function addRoute(HandlerInterface $route, $method = "*")
|
||||||
|
{
|
||||||
|
$table = $this->getRouteTable($method);
|
||||||
|
$table->addRoute($route);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -96,7 +107,7 @@ class Router implements HandlerInterface
|
||||||
}
|
}
|
||||||
return $response;
|
return $response;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Prepare a response indicating a 404 Not Found error
|
* Prepare a response indicating a 404 Not Found error
|
||||||
*
|
*
|
||||||
|
|
@ -115,6 +126,14 @@ class Router implements HandlerInterface
|
||||||
return $response;
|
return $response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function getRouteTable($method = "*")
|
||||||
|
{
|
||||||
|
if (!isset($this->routeTables[$method])) {
|
||||||
|
$this->routeTables[$method] = new RouteTable();
|
||||||
|
}
|
||||||
|
return $this->routeTables[$method];
|
||||||
|
}
|
||||||
|
|
||||||
private function getResponseFromRouteTables(RequestInterface $request, array $args = null)
|
private function getResponseFromRouteTables(RequestInterface $request, array $args = null)
|
||||||
{
|
{
|
||||||
$method = $request->getMethod();
|
$method = $request->getMethod();
|
||||||
|
|
@ -170,6 +189,10 @@ class Router implements HandlerInterface
|
||||||
return $response;
|
return $response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
////////////////
|
||||||
|
// Deprecated //
|
||||||
|
////////////////
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @deprecated Use {@see addRoute} instead.
|
* @deprecated Use {@see addRoute} instead.
|
||||||
* @see addRoute
|
* @see addRoute
|
||||||
|
|
@ -180,30 +203,6 @@ class Router implements HandlerInterface
|
||||||
trigger_error("Router::setPrefixRoute is deprecated. Use addRoute", E_USER_DEPRECATED);
|
trigger_error("Router::setPrefixRoute is deprecated. Use addRoute", E_USER_DEPRECATED);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Append a new route to the route table.
|
|
||||||
*
|
|
||||||
* @param HandlerInterface $route
|
|
||||||
* @param string $method HTTP Method; * for any
|
|
||||||
*/
|
|
||||||
public function addRoute(HandlerInterface $route, $method = "*")
|
|
||||||
{
|
|
||||||
$table = $this->getRouteTable($method);
|
|
||||||
$table->addRoute($route);
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////
|
|
||||||
// Deprecated //
|
|
||||||
////////////////
|
|
||||||
|
|
||||||
private function getRouteTable($method = "*")
|
|
||||||
{
|
|
||||||
if (!isset($this->routeTables[$method])) {
|
|
||||||
$this->routeTables[$method] = new RouteTable();
|
|
||||||
}
|
|
||||||
return $this->routeTables[$method];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @deprecated Use {@see addRoute} instead.
|
* @deprecated Use {@see addRoute} instead.
|
||||||
* @see addRoute
|
* @see addRoute
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue