Update comments and documentation for Dispatcher and related classes

This commit is contained in:
PJ Dietz 2018-06-22 12:17:30 -04:00
parent 5e9e7f154b
commit b82ebf6d95
5 changed files with 64 additions and 32 deletions

View File

@ -54,8 +54,11 @@ class DispatchStack implements DispatchStackInterface
* @param callable $next * @param callable $next
* @return ResponseInterface * @return ResponseInterface
*/ */
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, $next) public function __invoke(
{ ServerRequestInterface $request,
ResponseInterface $response,
$next
) {
$dispatcher = $this->dispatcher; $dispatcher = $this->dispatcher;
// This flag will be set to true when the last middleware calls $next. // This flag will be set to true when the last middleware calls $next.
@ -70,9 +73,9 @@ class DispatchStack implements DispatchStackInterface
// Create a chain of callables. // Create a chain of callables.
// //
// Each callable wil take $request and $response parameters, and will // Each callable will take $request and $response parameters, and will
// contain a dispatcher, the associated middleware, and a $next // contain a dispatcher, the associated middleware, and a $next function
// that is the links to the next middleware in the chain. // that serves as the link to the next middleware in the chain.
foreach (array_reverse($this->stack) as $middleware) { foreach (array_reverse($this->stack) as $middleware) {
$chain = function ($request, $response) use ($dispatcher, $middleware, $chain) { $chain = function ($request, $response) use ($dispatcher, $middleware, $chain) {
return $dispatcher->dispatch($middleware, $request, $response, $chain); return $dispatcher->dispatch($middleware, $request, $response, $chain);

View File

@ -17,7 +17,7 @@ interface DispatchStackInterface extends MiddlewareInterface
* This method MUST preserve the order in which middleware are added. * This method MUST preserve the order in which middleware are added.
* *
* @param mixed $middleware Middleware to dispatch in sequence * @param mixed $middleware Middleware to dispatch in sequence
* @return self * @return static
*/ */
public function add($middleware); public function add($middleware);
@ -48,5 +48,9 @@ interface DispatchStackInterface extends MiddlewareInterface
* @param callable $next * @param callable $next
* @return ResponseInterface * @return ResponseInterface
*/ */
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, $next); public function __invoke(
ServerRequestInterface $request,
ResponseInterface $response,
$next
);
} }

View File

@ -7,9 +7,31 @@ use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface; use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface; use Psr\Http\Server\RequestHandlerInterface;
/**
* Dispatches handlers and middleware
*/
class Dispatcher implements DispatcherInterface class Dispatcher implements DispatcherInterface
{ {
/** /**
* Dispatch a handler or middleware and return the response.
*
* Dispatcher can dispatch any of the following:
* - An instance implementing one of these interfaces:
* - Psr\Http\Server\RequestHandlerInterface
* - Psr\Http\Server\MiddlewareInterface
* - WellRESTed\MiddlewareInterface
* - Psr\Http\Message\ResponseInterface
* - A string containing the fully qualified class name of a class
* implementing one of the interfaces listed above.
* - A callable that returns an instance implementing one of the
* interfaces listed above.
* - A callable with a signature matching the signature of
* WellRESTed\MiddlewareInterface::__invoke
* - An array containing any of the items in this list.
*
* When Dispatcher receives a $dispatchable that is not of a type it
* can dispatch, it throws a DispatchException.
*
* @param mixed $dispatchable * @param mixed $dispatchable
* @param ServerRequestInterface $request * @param ServerRequestInterface $request
* @param ResponseInterface $response * @param ResponseInterface $response
@ -41,7 +63,7 @@ class Dispatcher implements DispatcherInterface
} elseif ($dispatchable instanceof ResponseInterface) { } elseif ($dispatchable instanceof ResponseInterface) {
return $dispatchable; return $dispatchable;
} else { } else {
throw new DispatchException("Unable to dispatch middleware."); throw new DispatchException('Unable to dispatch middleware.');
} }
} }

View File

@ -6,38 +6,47 @@ use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ServerRequestInterface;
/** /**
* Dispatches middleware * Dispatches handlers and middleware
*/ */
interface DispatcherInterface interface DispatcherInterface
{ {
/** /**
* Dispatch middleware and return the response. * Dispatch a handler or middleware and return the response.
* *
* This method MUST pass $request, $response, and $next to the middleware * Dispatchables (middleware and handlers) comes in a number of varieties
* to be dispatched. * (e.g., instance, string, callable). DispatcherInterface interface unpacks
* * the dispatchable and dispatches it.
* $middleware comes in a number of varieties (e.g., instance, string,
* callable). DispatcherInterface interface exist to unpack the middleware
* and dispatch it.
* *
* Implementations MUST be able to dispatch the following: * Implementations MUST be able to dispatch the following:
* - An instance implementing MiddlewareInterface * - An instance implementing one of these interfaces:
* - Psr\Http\Server\RequestHandlerInterface
* - Psr\Http\Server\MiddlewareInterface
* - WellRESTed\MiddlewareInterface
* - Psr\Http\Message\ResponseInterface
* - A string containing the fully qualified class name of a class * - A string containing the fully qualified class name of a class
* implementing MiddlewareInterface * implementing one of the interfaces listed above.
* - A callable that returns an instance implementing MiddlewareInterface * - A callable that returns an instance implementing one of the
* - A callable with a signature matching MiddlewareInterface::__invoke * interfaces listed above.
* - A callable with a signature matching the signature of
* WellRESTed\MiddlewareInterface::__invoke
* - An array containing any of the items in this list.
* *
* Implementation MAY dispatch other types of middleware. * Implementation MAY dispatch other types of middleware.
* *
* When an implementation receives a $middleware that is not of a type it can * When an implementation receives a $dispatchable that is not of a type it
* dispatch, it MUST throw a DispatchException. * can dispatch, it MUST throw a DispatchException.
* *
* @param mixed $middleware * @param mixed $dispatchable
* @param ServerRequestInterface $request * @param ServerRequestInterface $request
* @param ResponseInterface $response * @param ResponseInterface $response
* @param callable $next * @param callable $next
* @return ResponseInterface * @return ResponseInterface
* @throws DispatchException Unable to dispatch $middleware * @throws DispatchException Unable to dispatch $middleware
*/ */
public function dispatch($middleware, ServerRequestInterface $request, ResponseInterface $response, $next); public function dispatch(
$dispatchable,
ServerRequestInterface $request,
ResponseInterface $response,
$next
);
} }

View File

@ -25,7 +25,7 @@ class TransmitterTest extends TestCase
$this->request = (new ServerRequest()) $this->request = (new ServerRequest())
->withMethod("HEAD"); ->withMethod("HEAD");
$this->body = $this->prophesize('\Psr\Http\Message\StreamInterface'); $this->body = $this->prophesize(StreamInterface::class);
$this->body->isReadable()->willReturn(false); $this->body->isReadable()->willReturn(false);
$this->body->getSize()->willReturn(1024); $this->body->getSize()->willReturn(1024);
/** @var StreamInterface $stream */ /** @var StreamInterface $stream */
@ -36,12 +36,6 @@ class TransmitterTest extends TestCase
->withBody($stream); ->withBody($stream);
} }
public function testCreatesInstance()
{
$transmitter = new Transmitter();
$this->assertNotNull($transmitter);
}
public function testSendStatusCodeWithReasonPhrase() public function testSendStatusCodeWithReasonPhrase()
{ {
$transmitter = new Transmitter(); $transmitter = new Transmitter();