Update Router class to match new DispatcherInterface
This commit is contained in:
parent
8f99b88d09
commit
f60b010f11
|
|
@ -13,14 +13,13 @@ namespace pjdietz\WellRESTed;
|
||||||
use pjdietz\WellRESTed\Interfaces\DispatcherInterface;
|
use pjdietz\WellRESTed\Interfaces\DispatcherInterface;
|
||||||
use pjdietz\WellRESTed\Interfaces\ResponseInterface;
|
use pjdietz\WellRESTed\Interfaces\ResponseInterface;
|
||||||
use pjdietz\WellRESTed\Interfaces\RoutableInterface;
|
use pjdietz\WellRESTed\Interfaces\RoutableInterface;
|
||||||
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 extends RouteTarget implements DispatcherInterface
|
class Router implements DispatcherInterface
|
||||||
{
|
{
|
||||||
/** @var array Array of Route objects */
|
/** @var array Array of Route objects */
|
||||||
private $routes;
|
private $routes;
|
||||||
|
|
@ -35,62 +34,21 @@ class Router extends RouteTarget implements DispatcherInterface
|
||||||
* Return the response built by the handler based on the request
|
* Return the response built by the handler based on the request
|
||||||
*
|
*
|
||||||
* @param RoutableInterface $request
|
* @param RoutableInterface $request
|
||||||
|
* @param null $args
|
||||||
* @return ResponseInterface
|
* @return ResponseInterface
|
||||||
*/
|
*/
|
||||||
public function getResponse(RoutableInterface $request = null)
|
public function getResponse(RoutableInterface $request = null, $args = null)
|
||||||
{
|
{
|
||||||
// Use the singleton if the caller did not pass a request.
|
// Use the singleton if the caller did not pass a request.
|
||||||
if (is_null($reqs))
|
if (is_null($request)) {
|
||||||
|
$request = Request::getRequest();
|
||||||
// Set the instance's request, if the called passed one.
|
|
||||||
if (!is_null($request)) {
|
|
||||||
$this->request = $request;
|
|
||||||
}
|
|
||||||
|
|
||||||
// If the instance does not have a request, use the singleton.
|
|
||||||
if (is_null($this->request)) {
|
|
||||||
$this->request = Request::getRequest();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Reference the request and path.
|
|
||||||
$request = $this->request;
|
|
||||||
$request->incrementRouteDepth();
|
|
||||||
$path = $request->getPath();
|
|
||||||
|
|
||||||
if ($request->getRouteDepth() >= $this->getMaxDepth()) {
|
|
||||||
return $this->getInternalServerErrorResponse($request, 'Maximum route recursion reached.');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($this->routes as $route) {
|
foreach ($this->routes as $route) {
|
||||||
/** @var RouteInterface $route */
|
/** @var DispatcherInterface $route */
|
||||||
if (preg_match($route->getPattern(), $path, $matches)) {
|
$responce = $route->getResponse($request, $args);
|
||||||
$targetClassName = $route->getTarget();
|
if ($responce) {
|
||||||
if (is_subclass_of($targetClassName, self::ROUTE_TARGET_INTERFACE)) {
|
return $responce;
|
||||||
|
|
||||||
/** @var RouteTargetInterface $target */
|
|
||||||
$target = new $targetClassName();
|
|
||||||
$target->setRequest($request);
|
|
||||||
|
|
||||||
// If this instance already had arguments, 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 {
|
|
||||||
return $this->getInternalServerErrorResponse($request);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -100,7 +58,7 @@ class Router extends RouteTarget implements DispatcherInterface
|
||||||
/**
|
/**
|
||||||
* Append a new Route instance to the Router's route table.
|
* Append a new Route instance to the Router's route table.
|
||||||
*
|
*
|
||||||
* @param RouteInterface $route
|
* @param DispatcherInterface $route
|
||||||
*/
|
*/
|
||||||
public function addRoute(DispatcherInterface $route)
|
public function addRoute(DispatcherInterface $route)
|
||||||
{
|
{
|
||||||
|
|
@ -116,7 +74,7 @@ class Router extends RouteTarget implements DispatcherInterface
|
||||||
protected function getNoRouteResponse(RoutableInterface $request)
|
protected function getNoRouteResponse(RoutableInterface $request)
|
||||||
{
|
{
|
||||||
$response = new Response(404);
|
$response = new Response(404);
|
||||||
$response->body = 'No resource at ' . $request->getPath();
|
$response->setBody('No resource at ' . $request->getPath());
|
||||||
return $response;
|
return $response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue