I was hopeful to create a system where developers can pick and choose their middleware stack components using the zend-mvc middleware config.
For example, this middleware’s process method:
class JsonWrappingMiddleware implements MiddlewareInterface
{
public function process(ServerRequestInterface $request, DelegateInterface $delegate)
{
/** @var JsonResponse $data */
$jsonDecodeNextResponse = json_decode(
(string) $delegate->process($request)->getBody(),
true
);
$current = ['c' => 'd'];
return new JsonResponse(array_merge($jsonDecodeNextResponse, $current));
}
}
Works well if the next middleware in the stack looks like this:
public function process(ServerRequestInterface $request, DelegateInterface $delegate)
{
return new JsonResponse(['a' => 'b']);
}
However, for this exercise, they’d have to detect that there is indeed a ‘next’ element in the stack. The DelegateInterface only publishes process.
How can I detect that it’s the ‘end of the road’? I’d like to do something like:
public function process(ServerRequestInterface $request, DelegateInterface $delegate)
{
$current = ['c' => 'd'];
/** @var JsonResponse $data */
if ($delegate) {
$current = array_merge(json_decode(
(string)$delegate->process($request)->getBody(),
true
), $current);
}
return new JsonResponse($current);
}
That way I can sandwich a bunch of these, or, simply use one.