105 lines
2.5 KiB
PHP
105 lines
2.5 KiB
PHP
<?php
|
|
|
|
/**
|
|
* pjdietz\WellRESTed\Router
|
|
*
|
|
* @author PJ Dietz <pj@pjdietz.com>
|
|
* @copyright Copyright 2014 by PJ Dietz
|
|
* @license MIT
|
|
*/
|
|
|
|
namespace pjdietz\WellRESTed;
|
|
|
|
use pjdietz\WellRESTed\Interfaces\HandlerInterface;
|
|
use pjdietz\WellRESTed\Interfaces\RequestInterface;
|
|
use pjdietz\WellRESTed\Interfaces\ResponseInterface;
|
|
|
|
/**
|
|
* Router
|
|
*
|
|
* A Router uses a table of Routes to find the appropriate Handler for a request.
|
|
*/
|
|
class Router implements HandlerInterface
|
|
{
|
|
/** @var array Array of Route objects */
|
|
private $routes;
|
|
|
|
/** Create a new Router. */
|
|
public function __construct()
|
|
{
|
|
$this->routes = array();
|
|
}
|
|
|
|
/**
|
|
* Return the response built by the handler based on the request
|
|
*
|
|
* @param RequestInterface $request
|
|
* @param array|null $args
|
|
* @return ResponseInterface
|
|
*/
|
|
public function getResponse(RequestInterface $request, array $args = null)
|
|
{
|
|
foreach ($this->routes as $route) {
|
|
/** @var HandlerInterface $route */
|
|
$responce = $route->getResponse($request, $args);
|
|
if ($responce) {
|
|
return $responce;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Append a new route to the route route table.
|
|
*
|
|
* @param HandlerInterface $route
|
|
*/
|
|
public function addRoute(HandlerInterface $route)
|
|
{
|
|
$this->routes[] = $route;
|
|
}
|
|
|
|
/**
|
|
* Append a series of routes.
|
|
*
|
|
* @param array $routes List array of HandlerInterface instances
|
|
*/
|
|
public function addRoutes(array $routes)
|
|
{
|
|
foreach ($routes as $route) {
|
|
if ($route instanceof HandlerInterface) {
|
|
$this->addRoute($route);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Dispatch the singleton Request through the router and output the response.
|
|
*
|
|
* Respond with a 404 Not Found if no route provides a response.
|
|
*/
|
|
public function respond()
|
|
{
|
|
$request = Request::getRequest();
|
|
$response = $this->getResponse($request);
|
|
if (!$response) {
|
|
$response = $this->getNoRouteResponse($request);
|
|
}
|
|
$response->respond();
|
|
}
|
|
|
|
/**
|
|
* Prepare a resonse indicating a 404 Not Found error
|
|
*
|
|
* @param RequestInterface $request
|
|
* @return ResponseInterface
|
|
*/
|
|
protected function getNoRouteResponse(RequestInterface $request)
|
|
{
|
|
$response = new Response(404);
|
|
$response->setBody('No resource at ' . $request->getPath());
|
|
return $response;
|
|
}
|
|
|
|
}
|