Add response preparation hooks to Router

This commit is contained in:
PJ Dietz 2015-05-03 19:38:04 -04:00
parent b523f2e79d
commit 121b8be044
1 changed files with 39 additions and 3 deletions

View File

@ -8,6 +8,8 @@ use WellRESTed\HttpExceptions\HttpException;
use WellRESTed\Message\Response; use WellRESTed\Message\Response;
use WellRESTed\Message\ServerRequest; use WellRESTed\Message\ServerRequest;
use WellRESTed\Message\Stream; use WellRESTed\Message\Stream;
use WellRESTed\Routing\ResponsePrep\ContentLengthPrep;
use WellRESTed\Routing\ResponsePrep\HeadPrep;
use WellRESTed\Routing\Route\RouteFactory; use WellRESTed\Routing\Route\RouteFactory;
use WellRESTed\Routing\Route\RouteFactoryInterface; use WellRESTed\Routing\Route\RouteFactoryInterface;
@ -15,14 +17,22 @@ class Router implements MiddlewareInterface
{ {
/** @var DispatcherInterface */ /** @var DispatcherInterface */
protected $dispatcher; protected $dispatcher;
/** @var array Middleware to dispatch before the router evaluates the route. */
/** @var MiddlewareInterface[] List of middleware to dispatch immediatly before outputting the response */
protected $responsePreparationHooks;
/** @var MiddlewareInterface[] List of middleware to dispatch before the router evaluates the route. */
private $preRouteHooks; private $preRouteHooks;
/** @var array Middleware to dispatch after the router dispatches all other middleware */
/** @var MiddlewareInterface[] List of middleware to dispatch after the router dispatches all other middleware */
private $postRouteHooks; private $postRouteHooks;
/** @var array Hash array of status code => error handler */
/** @var array Hash array of status code => middleware */
private $statusHandlers; private $statusHandlers;
/** @var RouteTable Collection of routes */ /** @var RouteTable Collection of routes */
private $routeTable; private $routeTable;
/** @var RouteFactoryInterface */ /** @var RouteFactoryInterface */
private $routeFactory; private $routeFactory;
@ -30,6 +40,7 @@ class Router implements MiddlewareInterface
public function __construct() public function __construct()
{ {
$this->responsePreparationHooks = $this->getResponsePreparationHooks();
$this->routeFactory = $this->getRouteFactory(); $this->routeFactory = $this->getRouteFactory();
$this->routeTable = $this->getRouteTable(); $this->routeTable = $this->getRouteTable();
$this->statusHandlers = []; $this->statusHandlers = [];
@ -79,6 +90,11 @@ class Router implements MiddlewareInterface
$this->postRouteHooks[] = $middleware; $this->postRouteHooks[] = $middleware;
} }
public function addResponsePreparationHook($middleware)
{
$this->responsePreparationHooks[] = $middleware;
}
public function setStatusHandler($statusCode, $middleware) public function setStatusHandler($statusCode, $middleware)
{ {
$this->statusHandlers[$statusCode] = $middleware; $this->statusHandlers[$statusCode] = $middleware;
@ -100,6 +116,7 @@ class Router implements MiddlewareInterface
$dispatcher->dispatch($middleware, $request, $response); $dispatcher->dispatch($middleware, $request, $response);
} }
$this->disptachPostRouteHooks($request, $response); $this->disptachPostRouteHooks($request, $response);
$this->dispatchResponsePreparationHooks($request, $response);
} }
public function respond() public function respond()
@ -163,6 +180,17 @@ class Router implements MiddlewareInterface
return new Response(); return new Response();
} }
/**
* @return MiddlewareInterface[]
*/
protected function getResponsePreparationHooks()
{
return [
new ContentLengthPrep(),
new HeadPrep()
];
}
/** /**
* @return RouteFactoryInterface * @return RouteFactoryInterface
*/ */
@ -202,4 +230,12 @@ class Router implements MiddlewareInterface
} }
} }
} }
private function dispatchResponsePreparationHooks(ServerRequestInterface $request, ResponseInterface &$response)
{
$dispatcher = $this->getDispatcher();
foreach ($this->responsePreparationHooks as $hook) {
$dispatcher->dispatch($hook, $request, $response);
}
}
} }