Server accepts all dependencies as arguments to either constructor or respond

This commit is contained in:
PJ Dietz 2015-05-14 19:43:08 -04:00
parent 3f5e2321d9
commit 3d4a263beb
1 changed files with 32 additions and 7 deletions

View File

@ -21,9 +21,18 @@ class Server implements DispatchStackInterface
/** @var mixed[] List array of middleware */ /** @var mixed[] List array of middleware */
private $stack; private $stack;
public function __construct() /**
* Create a new router.
*
* @param DispatcherInterface $dispatcher Dispatches middleware. If no
* object is passed, the Server will create a
* WellRESTed\Dispatching\Dispatcher
*/
public function __construct(DispatcherInterface $dispatcher = null)
{ {
if ($dispatcher === null) {
$this->dispatcher = $this->getDispatcher(); $this->dispatcher = $this->getDispatcher();
}
$this->stack = []; $this->stack = [];
} }
@ -80,16 +89,32 @@ class Server implements DispatchStackInterface
* *
* This method reads a server request, dispatches the request through the * This method reads a server request, dispatches the request through the
* server's stack of middleware, and outputs the response. * server's stack of middleware, and outputs the response.
*
* @param ServerRequestInterface $request Request provided by the client
* @param ResponseInterface $response Initial starting place response to
* propogate to middleware.
* @param TransmitterInterface $transmitter Instance to outputing the
* final response to the client.
*/ */
public function respond() public function respond(
{ ServerRequestInterface $request = null,
ResponseInterface $response = null,
TransmitterInterface $transmitter = null
) {
if ($request === null) {
$request = $this->getRequest(); $request = $this->getRequest();
}
if ($response === null) {
$response = $this->getResponse(); $response = $this->getResponse();
}
if ($transmitter === null) {
$transmitter = $this->getTransmitter();
}
$next = function ($request, $response) { $next = function ($request, $response) {
return $response; return $response;
}; };
$response = $this->dispatch($request, $response, $next); $response = $this->dispatch($request, $response, $next);
$transmitter = $this->getTransmitter();
$transmitter->transmit($request, $response); $transmitter->transmit($request, $response);
} }