middlewares / minifier
Middleware to minify Html, CSS and Javascript responses
Installs: 27 048
Dependents: 2
Suggesters: 0
Security: 0
Stars: 18
Watchers: 3
Forks: 4
Open Issues: 0
Requires
- php: ^7.2 || ^8.0
- middlewares/utils: ^3.0 || ^4.0
- psr/http-server-middleware: ^1.0
- wyrihaximus/html-compress: ^3.0 || ^4.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.0
- laminas/laminas-diactoros: ^2.3 || ^3.0
- oscarotero/php-cs-fixer-config: ^1.0
- phpstan/phpstan: ^0.12
- phpunit/phpunit: ^8|^9
- squizlabs/php_codesniffer: ^3.0
README
Middleware to minify the Html
, CSS
and Javascript
content using wyrihaximus/compress and the following compressors by default:
Requirements
- PHP >= 7.2
- A PSR-7 http library
- A PSR-15 middleware dispatcher
Installation
This package is installable and autoloadable via Composer as middlewares/minifier.
composer require middlewares/minifier
Example
Dispatcher::run([ Middlewares\Minifier::html(), Middlewares\Minifier::css(), Middlewares\Minifier::js(), ]);
Usage
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);
Helpers
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.