adriansuter/psr7-minify-middleware

PSR-7 Middleware that minifies the response body (HTML Minify).

v0.3 2019-04-30 13:32 UTC

This package is not auto-updated.

Last update: 2024-10-25 14:26:37 UTC


README

Latest Stable Version Build status Coverage Status Total Downloads License

Simple PSR-7 Middleware that minifies the response body. This middleware can be used to minify the html output.

By default, all textarea and pre sections would not be minified (ignored). This can be customized.

Installation

composer require adriansuter/psr7-minify-middleware

Usage

The constructor of this middleware has two parameters:

  • A callback that returns a new object implementing the Psr\Http\Message\StreamInterface in order to be able to minify the content.
  • The html elements (tag names) that should be ignored. This parameter is optional and defaults to the array ['textarea', 'pre'].

In Slim 3:

use AdrianSuter\PSR7\Middleware\Minify;
use Slim\Http\Body;

// Create the application $app
// [...]

$app->add(
    new Minify(
        function () {
            return new Body(fopen('php://temp', 'r+'));
        }
    )
);

In order to customize the html elements to be ignored, simply add a second parameter to the constructor:

$app->add(
    new Minify(
        function () {
            return new Body(fopen('php://temp', 'r+'));
        },
        ['script', 'textarea', 'pre', 'code']
    )
);

Testing

  • Unit tests: $ vendor/bin/phpunit