Middleware to minify the Html
, CSS
and Javascript
content using wyrihaximus/compress and the following compressors by default:
- PHP >= 7.2
- A PSR-7 http library
- A PSR-15 middleware dispatcher
This package is installable and autoloadable via Composer as middlewares/minifier.
composer require middlewares/minifier
Dispatcher::run([
Middlewares\Minifier::html(),
Middlewares\Minifier::css(),
Middlewares\Minifier::js(),
]);
This middleware minify the code of http responses using any compressor implementing the WyriHaximus\Compress\CompressorInterface
. The code format is detected from the Content-Type
header, so make sure your responses contains this header (you may want to use middlewares/negotiation for that).
use WyriHaximus\HtmlCompress\Factory;
$compressor = Factory::construct();
$mimeType = 'text/html';
$minifier = new Middlewares\Minifier($compressor, $mimeType);
Optionally, you can provide a Psr\Http\Message\StreamFactoryInterface
as third argument to create the response body. If it's not defined, Middleware\Utils\Factory will be used to detect it automatically.
$streamFactory = new MyOwnStreamFactory();
$minifier = new Middlewares\Minifier($compressor, $mimeType, $streamFactory);
Three static functions are provided to create instances of this middleware with common configuration for html, css and js responses:
$htmlMinifier = Middlewares\Minifier::html();
$cssMinifier = Middlewares\Minifier::css();
$jsMinifier = Middlewares\Minifier::js();
Please see CHANGELOG for more information about recent changes and CONTRIBUTING for contributing details.
The MIT License (MIT). Please see LICENSE for more information.