-
Notifications
You must be signed in to change notification settings - Fork 90
Add a middleware dispatch #32
Add a middleware dispatch #32
Conversation
$return = $middleware(Psr7Request::fromZend($request), Psr7Response::fromZend($response)); | ||
return $this->complete(Psr7Response::toZend($return), $e); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would argue we should separate this logic into a different listener, which we would then add as a default listener in Zend\Mvc\Application
; it would execute at higher priority than this one (or same priority, but register prior to this one). If no middleware route match parameter is found, it would simply return, which would cause the original dispatch listener to take over.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd write the end logic as this:
if (! $return instanceof \Psr\Http\Message\ResponseInterface) {
$e->setResult($return);
return $return;
}
$response = Psr7Response::toZend($return);
$e->setResult($response);
return $response;
which might fix the testing issue you're seeing.
@weierophinney I created a MiddlewareListener with the logic suggested but I'm having issue to trigger this events. The unit tests are failing, the DispatchListener is executed before the MiddlewareListener even if I used a priority of 1000 here. |
*/ | ||
public function attach(EventManagerInterface $events) | ||
{ | ||
$this->listeners[] = $events->attach(MvcEvent::EVENT_DISPATCH, [$this, 'onDispatch'], 1000); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We'll likely take this down to 1 once we solve the issue.
@weierophinney I fixed the unit test issue. The test runs with two errors on |
You're using dev-develop of the ModuleManager, which is currently setup to use dev-develop of the servicemanager — which is the source of that issue. You can ignore it for now. |
} | ||
} | ||
|
||
if (! $return instanceof \Psr\Http\Message\ResponseInterface) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
move to use statement, I created PR to your branch for it ezimuel#1
[WIP] Add a middleware dispatch
NOTE: This PR is a WIP, please don't merge it.
This PR adds the middleware support to MVC. We want to be able to dispatch a "middleware" parameter as callable or service, using the signature:
To do: