Skip to content

Instantly share code, notes, and snippets.

@adamtester
Created January 15, 2015 11:16
Show Gist options
  • Save adamtester/965811e2e340980a9ae4 to your computer and use it in GitHub Desktop.
Save adamtester/965811e2e340980a9ae4 to your computer and use it in GitHub Desktop.
laravel_compress
App::after(function($request, $response)
{
// Compress output
ini_set("pcre.recursion_limit", "16777");
$buffer = $response->getContent();
$re = '%# Collapse whitespace everywhere but in blacklisted elements.
(?> # Match all whitespans other than single space.
[^\S ]\s* # Either one [\t\r\n\f\v] and zero or more ws,
| \s{2,} # or two or more consecutive-any-whitespace.
) # Note: The remaining regex consumes no text at all...
(?= # Ensure we are not in a blacklist tag.
[^<]*+ # Either zero or more non-"<" {normal*}
(?: # Begin {(special normal*)*} construct
< # or a < starting a non-blacklist tag.
(?!/?(?:textarea|pre|script)\b)
[^<]*+ # more non-"<" {normal*}
)*+ # Finish "unrolling-the-loop"
(?: # Begin alternation group.
< # Either a blacklist start tag.
(?>textarea|pre|script)\b
| \z # or end of file.
) # End alternation group.
) # If we made it here, we are not in a blacklist tag.
%Six';
$new_buffer = preg_replace($re, " ", $buffer);
// We are going to check if processing has working
if ($new_buffer === null) {
$new_buffer = $buffer;
}
$response->setContent($new_buffer);
});
@adamtester
Copy link
Author

Could try this fitztrev/laravel-html-minify#52
When I have time I'll make a L5 version if people want it

@tiran133
Copy link

Middleware for L5 Just add it to your Kernel middleware.
https://gist.github.com/tiran133/83022f001c75f02e8a15

<?php  namespace App\Http\Middleware;

use Closure;
use Illuminate\Contracts\Routing\Middleware;

class MinifyResponseMiddleware implements Middleware{

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \Closure $next
     * @return mixed
     */
    public function handle ($request, Closure $next)
    {

        $response = $next($request);

        // Compress output
        ini_set("pcre.recursion_limit", "16777");
        $buffer = $response->getContent();
        $re = '%# Collapse whitespace everywhere but in blacklisted elements.
        (?>             # Match all whitespans other than single space.
          [^\S ]\s*     # Either one [\t\r\n\f\v] and zero or more ws,
        | \s{2,}        # or two or more consecutive-any-whitespace.
        ) # Note: The remaining regex consumes no text at all...
        (?=             # Ensure we are not in a blacklist tag.
          [^<]*+        # Either zero or more non-"<" {normal*}
          (?:           # Begin {(special normal*)*} construct
            <           # or a < starting a non-blacklist tag.
            (?!/?(?:textarea|pre|script)\b)
            [^<]*+      # more non-"<" {normal*}
          )*+           # Finish "unrolling-the-loop"
          (?:           # Begin alternation group.
            <           # Either a blacklist start tag.
            (?>textarea|pre|script)\b
          | \z          # or end of file.
          )             # End alternation group.
        )  # If we made it here, we are not in a blacklist tag.
        %Six';

        $new_buffer = preg_replace($re, " ", $buffer);

        // We are going to check if processing has working
        if ($new_buffer === null) {
            $new_buffer = $buffer;
        }

        $response->setContent($new_buffer);

        return $response;
    }
}

I use this and I haven't had any problems yet

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment