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

Commit 396a650

Browse files
committed
Corrected rebase conflicts - moved middleware pipe dispatch into the controller wrapper
1 parent 2d2f887 commit 396a650

File tree

3 files changed

+33
-19
lines changed

3 files changed

+33
-19
lines changed

src/Controller/MiddlewareController.php

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,19 @@
99

1010
namespace Zend\Mvc\Controller;
1111

12+
use Psr\Http\Message\ResponseInterface;
13+
use Psr\Http\Message\ServerRequestInterface;
1214
use Zend\EventManager\EventManager;
1315
use Zend\Http\Request;
1416
use Zend\Http\Response;
17+
use Zend\Mvc\Exception\ReachedFinalHandlerException;
1518
use Zend\Mvc\Exception\RuntimeException;
1619
use Zend\Mvc\MvcEvent;
1720
use Zend\Psr7Bridge\Psr7Response;
1821
use Zend\Psr7Bridge\Psr7ServerRequest;
1922
use Zend\Router\RouteMatch;
23+
use Zend\Stratigility\Delegate\CallableDelegateDecorator;
24+
use Zend\Stratigility\MiddlewarePipe;
2025

2126
/**
2227
* Note: I'm a terrible person
@@ -27,17 +32,28 @@
2732
final class MiddlewareController extends AbstractController
2833
{
2934
/**
30-
* @var callable
35+
* @var MiddlewarePipe
3136
*/
32-
private $middleware;
37+
private $pipe;
3338

34-
public function __construct(callable $middleware, EventManager $eventManager, MvcEvent $event)
35-
{
36-
$this->eventIdentifier = __CLASS__;
37-
$this->middleware = $middleware;
39+
/**
40+
* @var ResponseInterface
41+
*/
42+
private $responsePrototype;
43+
44+
public function __construct(
45+
MiddlewarePipe $pipe,
46+
ResponseInterface $responsePrototype,
47+
EventManager $eventManager,
48+
MvcEvent $event
49+
) {
50+
$this->eventIdentifier = __CLASS__;
51+
$this->pipe = $pipe;
52+
$this->responsePrototype = $responsePrototype;
3853

3954
$this->setEventManager($eventManager);
4055
$this->setEvent($event);
56+
4157
}
4258

4359
/**
@@ -74,7 +90,12 @@ public function onDispatch(MvcEvent $e)
7490
}
7591
}
7692

77-
$result = \call_user_func($this->middleware, $psr7Request, Psr7Response::fromZend($response));
93+
$result = $this->pipe->process($psr7Request, new CallableDelegateDecorator(
94+
function (ServerRequestInterface $request, ResponseInterface $response) {
95+
throw ReachedFinalHandlerException::create();
96+
},
97+
$this->responsePrototype
98+
));
7899

79100
$e->setResult($result);
80101

src/MiddlewareListener.php

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -83,20 +83,11 @@ public function onDispatch(MvcEvent $event)
8383
$caughtException = null;
8484
try {
8585
$return = (new MiddlewareController(
86-
$middleware,
86+
$pipe,
87+
$psr7ResponsePrototype,
8788
$application->getServiceManager()->get('EventManager'),
8889
$event
8990
))->dispatch($request, $response);
90-
$psr7Request = Psr7Request::fromZend($request)->withAttribute(RouteMatch::class, $routeMatch);
91-
foreach ($routeMatch->getParams() as $key => $value) {
92-
$psr7Request = $psr7Request->withAttribute($key, $value);
93-
}
94-
$return = $pipe->process($psr7Request, new CallableDelegateDecorator(
95-
function (PsrServerRequestInterface $request, PsrResponseInterface $response) {
96-
throw ReachedFinalHandlerException::create();
97-
},
98-
$psr7ResponsePrototype
99-
));
10091
} catch (\Throwable $ex) {
10192
$caughtException = $ex;
10293
} catch (\Exception $ex) { // @TODO clean up once PHP 7 requirement is enforced

test/MiddlewareListenerTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ public function testSuccessfullyDispatchesMiddleware()
8989
$application = $event->getApplication();
9090

9191
$application->getEventManager()->attach(MvcEvent::EVENT_DISPATCH_ERROR, function ($e) {
92-
die(var_dump($e->getParam('exception')->getMessage()));
9392
$this->fail(sprintf('dispatch.error triggered when it should not be: %s', var_export($e->getError(), 1)));
9493
});
9594

@@ -163,6 +162,7 @@ public function testSuccessfullyDispatchesPipeOfCallableAndHttpInteropStyleMiddl
163162
$eventManager = new EventManager();
164163

165164
$serviceManager = $this->prophesize(ContainerInterface::class);
165+
$serviceManager->get('EventManager')->willReturn($eventManager);
166166
$serviceManager->has('firstMiddleware')->willReturn(true);
167167
$serviceManager->get('firstMiddleware')->willReturn(function ($request, $response, $next) {
168168
$this->assertInstanceOf(ServerRequestInterface::class, $request);
@@ -303,6 +303,8 @@ public function testMiddlewareWithNothingPipedReachesFinalHandlerException()
303303
});
304304
$application->getResponse()->willReturn($response);
305305

306+
$serviceManager->get('EventManager')->willReturn($eventManager);
307+
306308
$event = new MvcEvent();
307309
$event->setRequest(new Request());
308310
$event->setResponse($response);

0 commit comments

Comments
 (0)