On the same time I'm automatically pretty printing the response if the request contains header X-Pretty: 1
but this will also be ignored as the content gets rewritten in this case.
class Module
{
public function onBootstrap(MvcEvent $e)
{
$eventManager = $e->getApplication()->getEventManager();
$eventManager->attach(MvcEvent::EVENT_FINISH, [$this, 'onFinish'], -1000);
}
public function onFinish(MvcEvent $event)
{
$response = $event->getResponse();
$request = $event->getRequest();
if ($request instanceof HttpRequest && $response instanceof HttpResponse && !($response instanceof HttpStreamResponse)) {
$rqHeaders = $request->getHeaders();
$rsHeaders = $response->getHeaders();
$rsContent = $response->getContent();
// pretty print JSON response
if ($rqHeaders->has('X-Pretty')
&& $rqHeaders->get('X-Pretty')->getFieldValue() === '1'
&& is_string($rsContent) && $rsContent
&& $rsHeaders->has('Content-Type')
&& preg_match('/^application\\/(.*\\-)?json/', $rsHeaders->get('Content-Type')->getFieldValue())
) {
$rsContentDecode = json_decode($rsContent);
if ($rsContentDecode !== null) {
$prettyOptions = JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE;
$rsContent = json_encode($rsContentDecode, $prettyOptions);
$response->setContent($rsContent);
if ($rsHeaders->has('Content-Length')) {
$rsHeaders->removeHeader($rsHeaders->get('Content-Length'));
}
$rsHeaders->addHeaderLine('Content-Length', strlen($rsContent));
}
}
// Send Content-Length header if possible
if (is_string($rsContent) && !$rsHeaders->has('Content-Length')) {
$rsHeaders->addHeaderLine('Content-Length', strlen($rsContent));
}
}
}
}