Add type hints to Server; construct defaults for most dependencies

This commit is contained in:
PJ Dietz 2020-08-09 14:51:43 -04:00
parent 98014d8c59
commit 09dd1d7a32
2 changed files with 24 additions and 52 deletions

View File

@ -14,14 +14,14 @@ use WellRESTed\Transmission\TransmitterInterface;
class Server class Server
{ {
/** @var array */ /** @var mixed[] */
protected $attributes; protected $attributes = [];
/** @var DispatcherInterface */ /** @var DispatcherInterface */
private $dispatcher; private $dispatcher;
/** @var string ServerRequestInterface attribute name for matched path variables */ /** @var string|null attribute name for matched path variables */
private $pathVariablesAttributeName; private $pathVariablesAttributeName = null;
/** @var ServerRequestInterface */ /** @var ServerRequestInterface|null */
private $request; private $request = null;
/** @var ResponseInterface */ /** @var ResponseInterface */
private $response; private $response;
/** @var TransmitterInterface */ /** @var TransmitterInterface */
@ -31,6 +31,9 @@ class Server
public function __construct() { public function __construct() {
$this->stack = []; $this->stack = [];
$this->response = new Response();
$this->dispatcher = new Dispatcher();
$this->transmitter = new Transmitter();
} }
/** /**
@ -39,7 +42,7 @@ class Server
* @param mixed $middleware Middleware to dispatch in sequence * @param mixed $middleware Middleware to dispatch in sequence
* @return Server * @return Server
*/ */
public function add($middleware) public function add($middleware): Server
{ {
$this->stack[] = $middleware; $this->stack[] = $middleware;
return $this; return $this;
@ -50,10 +53,10 @@ class Server
* *
* @return Router * @return Router
*/ */
public function createRouter() public function createRouter(): Router
{ {
return new Router( return new Router(
$this->getDispatcher(), $this->dispatcher,
$this->pathVariablesAttributeName $this->pathVariablesAttributeName
); );
} }
@ -64,25 +67,26 @@ class Server
* 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 via a Transmitter. * server's stack of middleware, and outputs the response via a Transmitter.
*/ */
public function respond() public function respond(): void
{ {
$request = $this->getRequest(); $request = $this->getRequest();
foreach ($this->getAttributes() as $name => $value) { foreach ($this->attributes as $name => $value) {
$request = $request->withAttribute($name, $value); $request = $request->withAttribute($name, $value);
} }
$response = $this->getResponse(); $next = function (
ServerRequestInterface $rqst,
$next = function ($rqst, $resp) { ResponseInterface $resp
): ResponseInterface {
return $resp; return $resp;
}; };
$dispatcher = $this->getDispatcher(); $response = $this->response;
$response = $dispatcher->dispatch(
$response = $this->dispatcher->dispatch(
$this->stack, $request, $response, $next); $this->stack, $request, $response, $next);
$transmitter = $this->getTransmitter(); $this->transmitter->transmit($request, $response);
$transmitter->transmit($request, $response);
} }
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
@ -150,43 +154,11 @@ class Server
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
/* Defaults */ /* Defaults */
private function getAttributes() private function getRequest(): ServerRequestInterface
{
if (!$this->attributes) {
$this->attributes = [];
}
return $this->attributes;
}
private function getDispatcher()
{
if (!$this->dispatcher) {
$this->dispatcher = new Dispatcher();
}
return $this->dispatcher;
}
private function getRequest()
{ {
if (!$this->request) { if (!$this->request) {
$this->request = ServerRequest::getServerRequest(); $this->request = ServerRequest::getServerRequest();
} }
return $this->request; return $this->request;
} }
private function getResponse()
{
if (!$this->response) {
$this->response = new Response();
}
return $this->response;
}
private function getTransmitter()
{
if (!$this->transmitter) {
$this->transmitter = new Transmitter();
}
return $this->transmitter;
}
} }

View File

@ -27,7 +27,7 @@ class ServerTest extends TestCase
parent::setUp(); parent::setUp();
$this->transmitter = $this->prophesize(TransmitterInterface::class); $this->transmitter = $this->prophesize(TransmitterInterface::class);
$this->transmitter->transmit(Argument::cetera())->willReturn(); $this->transmitter->transmit(Argument::cetera());
$this->server = new Server(); $this->server = new Server();
$this->server->setTransmitter($this->transmitter->reveal()); $this->server->setTransmitter($this->transmitter->reveal());