Skip to content

Commit

Permalink
Merge pull request #308 from clue-labs/final-next
Browse files Browse the repository at this point in the history
Do not pass $next handler to final request handler
  • Loading branch information
WyriHaximus committed Mar 13, 2018
2 parents f885737 + 465168f commit 4a80825
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 deletions.
8 changes: 6 additions & 2 deletions README.md
Expand Up @@ -850,9 +850,13 @@ $server = new Server(array(
));
```

> Note how the middleware request handler and the final request handler have a
very simple (and similar) interface. The only difference is that the final
request handler does not receive a `$next` handler.

Similarly, you can use the result of the `$next` middleware request handler
function to modify the outgoing response.
Note that as per the above documentation, the `$next` method may return a
Note that as per the above documentation, the `$next` function may return a
`ResponseInterface` directly or one wrapped in a promise for deferred
resolution.
In order to simplify handling both paths, you can simply wrap this in a
Expand Down Expand Up @@ -1002,7 +1006,7 @@ Usage:
$server = new StreamingServer(array(
new LimitConcurrentRequestsMiddleware(100), // 100 concurrent buffering handlers
new RequestBodyBufferMiddleware(16 * 1024 * 1024), // 16 MiB
function (ServerRequestInterface $request, callable $next) {
function (ServerRequestInterface $request) {
// The body from $request->getBody() is now fully available without the need to stream it
return new Response(200);
},
Expand Down
9 changes: 8 additions & 1 deletion src/Io/MiddlewareRunner.php
Expand Up @@ -29,7 +29,7 @@ public function __construct(array $middleware)
/**
* @param ServerRequestInterface $request
* @return ResponseInterface|PromiseInterface<ResponseInterface>
* @throws Exception
* @throws \Exception
*/
public function __invoke(ServerRequestInterface $request)
{
Expand All @@ -43,11 +43,18 @@ public function __invoke(ServerRequestInterface $request)
/** @internal */
public function call(ServerRequestInterface $request, $position)
{
// final request handler will be invoked without a next handler
if (!isset($this->middleware[$position + 1])) {
$handler = $this->middleware[$position];
return $handler($request);
}

$that = $this;
$next = function (ServerRequestInterface $request) use ($that, $position) {
return $that->call($request, $position + 1);
};

// invoke middleware request handler with next handler
$handler = $this->middleware[$position];
return $handler($request, $next);
}
Expand Down
39 changes: 38 additions & 1 deletion tests/Io/MiddlewareRunnerTest.php
Expand Up @@ -31,6 +31,43 @@ public function testEmptyMiddlewareStackThrowsException()
$middlewareStack($request);
}

public function testMiddlewareHandlerReceivesTwoArguments()
{
$args = null;
$middleware = new MiddlewareRunner(array(
function (ServerRequestInterface $request, $next) use (&$args) {
$args = func_num_args();
return $next($request);
},
function (ServerRequestInterface $request) {
return null;
}
));

$request = new ServerRequest('GET', 'http://example.com/');

$middleware($request);

$this->assertEquals(2, $args);
}

public function testFinalHandlerReceivesOneArgument()
{
$args = null;
$middleware = new MiddlewareRunner(array(
function (ServerRequestInterface $request) use (&$args) {
$args = func_num_args();
return null;
}
));

$request = new ServerRequest('GET', 'http://example.com/');

$middleware($request);

$this->assertEquals(1, $args);
}

/**
* @expectedException RuntimeException
* @expectedExceptionMessage hello
Expand Down Expand Up @@ -224,7 +261,7 @@ function (ServerRequestInterface $request, $next) use (&$receivedRequests) {
$receivedRequests[] = 'middleware2: ' . $request->getUri();
return $next($request);
},
function (ServerRequestInterface $request, $next) use (&$receivedRequests) {
function (ServerRequestInterface $request) use (&$receivedRequests) {
$receivedRequests[] = 'middleware3: ' . $request->getUri();
return new \React\Promise\Promise(function () { });
}
Expand Down

0 comments on commit 4a80825

Please sign in to comment.