Compressing text - gzip?

Hi @twigz What have you tried so far?

To compress the response it would be possible to use a middleware like this:

// Gzip compression middleware
$app->add(function (Request $request, Response $response, $next) {
    if ($request->hasHeader('Accept-Encoding') &&
        stristr($request->getHeaderLine('Accept-Encoding'), 'gzip') === false
    ) {
        // Browser doesn't accept gzip compression
        return $next($request, $response);
    }

    /** @var Response $response */
    $response = $next($request, $response);

    if ($response->hasHeader('Content-Encoding')) {
        return $next($request, $response);
    }

    // Compress response data
    $deflateContext = deflate_init(ZLIB_ENCODING_GZIP);
    $compressed = deflate_add($deflateContext, (string)$response->getBody(), \ZLIB_FINISH);

    $stream = fopen('php://memory', 'r+');
    fwrite($stream, $compressed);
    rewind($stream);

    return $response
        ->withHeader('Content-Encoding', 'gzip')
        ->withHeader('Content-Length', strlen($compressed))
        ->withBody(new \Slim\Http\Stream($stream));
});