From 3d4a263beb6d72ce372a41a14b044ef3be0c0e25 Mon Sep 17 00:00:00 2001 From: PJ Dietz Date: Thu, 14 May 2015 19:43:08 -0400 Subject: [PATCH] Server accepts all dependencies as arguments to either constructor or respond --- src/Server.php | 39 ++++++++++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/src/Server.php b/src/Server.php index ab7f9f7..db81f9b 100644 --- a/src/Server.php +++ b/src/Server.php @@ -21,9 +21,18 @@ class Server implements DispatchStackInterface /** @var mixed[] List array of middleware */ 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) { - $this->dispatcher = $this->getDispatcher(); + if ($dispatcher === null) { + $this->dispatcher = $this->getDispatcher(); + } $this->stack = []; } @@ -80,16 +89,32 @@ class Server implements DispatchStackInterface * * This method reads a server request, dispatches the request through the * 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() - { - $request = $this->getRequest(); - $response = $this->getResponse(); + public function respond( + ServerRequestInterface $request = null, + ResponseInterface $response = null, + TransmitterInterface $transmitter = null + ) { + if ($request === null) { + $request = $this->getRequest(); + } + if ($response === null) { + $response = $this->getResponse(); + } + if ($transmitter === null) { + $transmitter = $this->getTransmitter(); + } + $next = function ($request, $response) { return $response; }; $response = $this->dispatch($request, $response, $next); - $transmitter = $this->getTransmitter(); $transmitter->transmit($request, $response); }