Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit 6bad82f

Browse files
committed
Do not cast request handlers implementing middleware to RequestHandlerMiddleware
Per #645, `MiddlewareContainer::get()` was incorrectly casting middleware that also implemented `RequestHandlerInterface` to `RequestHandlerMiddleware`. This particularly affected `Zend\Stratigility\MiddlewarePipe` instances, as they implement both interfaces; if the last middleware in a pipeline called on the handler, users would then encounter a "pipeline exhausted" error, because it would be invoked as a handler, and thus have no reference to the application's pipeline. This change verifies that the request handler does not also implement `MiddlewareInterface` before casting.
1 parent 4e551cd commit 6bad82f

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

src/MiddlewareContainer.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,11 @@ public function get($service) : MiddlewareInterface
6363
? $this->container->get($service)
6464
: new $service();
6565

66-
$middleware = $middleware instanceof RequestHandlerInterface
67-
? new RequestHandlerMiddleware($middleware)
68-
: $middleware;
66+
if ($middleware instanceof RequestHandlerInterface
67+
&& ! $middleware instanceof MiddlewareInterface
68+
) {
69+
$middleware = new RequestHandlerMiddleware($middleware);
70+
}
6971

7072
if (! $middleware instanceof MiddlewareInterface) {
7173
throw Exception\InvalidMiddlewareException::forMiddlewareService($service, $middleware);

test/MiddlewareContainerTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,20 @@ public function testGetWillDecorateARequestHandlerAsMiddleware()
101101
$this->assertInstanceOf(RequestHandlerMiddleware::class, $middleware);
102102
$this->assertAttributeSame($handler, 'handler', $middleware);
103103
}
104+
105+
/**
106+
* @see https://github.com/zendframework/zend-expressive/issues/645
107+
*/
108+
public function testGetDoesNotCastMiddlewareImplementingRequestHandlerToRequestHandlerMiddleware()
109+
{
110+
$pipeline = $this->prophesize(RequestHandlerInterface::class);
111+
$pipeline->willImplement(MiddlewareInterface::class);
112+
113+
$this->originContainer->has('pipeline')->willReturn(true);
114+
$this->originContainer->get('pipeline')->will([$pipeline, 'reveal']);
115+
116+
$middleware = $this->container->get('pipeline');
117+
118+
$this->assertSame($middleware, $pipeline->reveal());
119+
}
104120
}

0 commit comments

Comments
 (0)