Skip to content

Commit 4a80825

Browse files
authored
Merge pull request #308 from clue-labs/final-next
Do not pass $next handler to final request handler
2 parents f885737 + 465168f commit 4a80825

3 files changed

Lines changed: 52 additions & 4 deletions

File tree

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -850,9 +850,13 @@ $server = new Server(array(
850850
));
851851
```
852852

853+
> Note how the middleware request handler and the final request handler have a
854+
very simple (and similar) interface. The only difference is that the final
855+
request handler does not receive a `$next` handler.
856+
853857
Similarly, you can use the result of the `$next` middleware request handler
854858
function to modify the outgoing response.
855-
Note that as per the above documentation, the `$next` method may return a
859+
Note that as per the above documentation, the `$next` function may return a
856860
`ResponseInterface` directly or one wrapped in a promise for deferred
857861
resolution.
858862
In order to simplify handling both paths, you can simply wrap this in a
@@ -1002,7 +1006,7 @@ Usage:
10021006
$server = new StreamingServer(array(
10031007
new LimitConcurrentRequestsMiddleware(100), // 100 concurrent buffering handlers
10041008
new RequestBodyBufferMiddleware(16 * 1024 * 1024), // 16 MiB
1005-
function (ServerRequestInterface $request, callable $next) {
1009+
function (ServerRequestInterface $request) {
10061010
// The body from $request->getBody() is now fully available without the need to stream it
10071011
return new Response(200);
10081012
},

src/Io/MiddlewareRunner.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function __construct(array $middleware)
2929
/**
3030
* @param ServerRequestInterface $request
3131
* @return ResponseInterface|PromiseInterface<ResponseInterface>
32-
* @throws Exception
32+
* @throws \Exception
3333
*/
3434
public function __invoke(ServerRequestInterface $request)
3535
{
@@ -43,11 +43,18 @@ public function __invoke(ServerRequestInterface $request)
4343
/** @internal */
4444
public function call(ServerRequestInterface $request, $position)
4545
{
46+
// final request handler will be invoked without a next handler
47+
if (!isset($this->middleware[$position + 1])) {
48+
$handler = $this->middleware[$position];
49+
return $handler($request);
50+
}
51+
4652
$that = $this;
4753
$next = function (ServerRequestInterface $request) use ($that, $position) {
4854
return $that->call($request, $position + 1);
4955
};
5056

57+
// invoke middleware request handler with next handler
5158
$handler = $this->middleware[$position];
5259
return $handler($request, $next);
5360
}

tests/Io/MiddlewareRunnerTest.php

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,43 @@ public function testEmptyMiddlewareStackThrowsException()
3131
$middlewareStack($request);
3232
}
3333

34+
public function testMiddlewareHandlerReceivesTwoArguments()
35+
{
36+
$args = null;
37+
$middleware = new MiddlewareRunner(array(
38+
function (ServerRequestInterface $request, $next) use (&$args) {
39+
$args = func_num_args();
40+
return $next($request);
41+
},
42+
function (ServerRequestInterface $request) {
43+
return null;
44+
}
45+
));
46+
47+
$request = new ServerRequest('GET', 'http://example.com/');
48+
49+
$middleware($request);
50+
51+
$this->assertEquals(2, $args);
52+
}
53+
54+
public function testFinalHandlerReceivesOneArgument()
55+
{
56+
$args = null;
57+
$middleware = new MiddlewareRunner(array(
58+
function (ServerRequestInterface $request) use (&$args) {
59+
$args = func_num_args();
60+
return null;
61+
}
62+
));
63+
64+
$request = new ServerRequest('GET', 'http://example.com/');
65+
66+
$middleware($request);
67+
68+
$this->assertEquals(1, $args);
69+
}
70+
3471
/**
3572
* @expectedException RuntimeException
3673
* @expectedExceptionMessage hello
@@ -224,7 +261,7 @@ function (ServerRequestInterface $request, $next) use (&$receivedRequests) {
224261
$receivedRequests[] = 'middleware2: ' . $request->getUri();
225262
return $next($request);
226263
},
227-
function (ServerRequestInterface $request, $next) use (&$receivedRequests) {
264+
function (ServerRequestInterface $request) use (&$receivedRequests) {
228265
$receivedRequests[] = 'middleware3: ' . $request->getUri();
229266
return new \React\Promise\Promise(function () { });
230267
}

0 commit comments

Comments
 (0)