├── .gitignore ├── LICENSE ├── README.md ├── composer.json └── src └── Slim └── Middleware └── Minify.php /.gitignore: -------------------------------------------------------------------------------- 1 | /.idea -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Christian Klisch 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | slim-minify 2 | =========== 3 | 4 | Slim middleware to minify HTML output generated by the slim PHP framework. It removes whitespaces, empty lines, tabs 5 | beetween html-tags and comments to reduce traffic. This script is a summary of stackoverflow answers. 6 | 7 | ## Requirements 8 | 9 | * PHP Version >= 7.2 10 | * Slim Version >= 4.0 11 | 12 | ## Usage 13 | 14 | Copy the file Minify.php to 'Slim/Middleware/'. Register minify via $app->add(): 15 | 16 | 17 | or use the composer: 18 | ``` 19 | "require": { 20 | "christianklisch/slim-minify": "0.7.0" 21 | } 22 | ``` 23 | 24 | in 'src/middleware.php': 25 | ```php 26 | $app->add(new \Slim\Middleware\Minify() ); 27 | ``` 28 | 29 | to deactivate minified output: 30 | ```php 31 | $app->add(new \Slim\Middleware\Minify(false); 32 | ``` 33 | 34 | ## Contributors 35 | 36 | * Christian Klisch https://www.christian-klisch.de 37 | 38 | 39 | ## Copyright and license 40 | 41 | Copyright 2014 released under [MIT](LICENSE) license. 42 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "christianklisch/slim-minify", 3 | "description": "Minify html output in slim framework", 4 | "keywords": ["php","minify","html"], 5 | "homepage": "https://github.com/christianklisch/slim-minify", 6 | "authors": [{ 7 | "name": "Christian Klisch", 8 | "homepage": "http://www.christian-klisch.de/" 9 | } 10 | ], 11 | "license": "MIT", 12 | "require": { 13 | "php": ">=7.2.0", 14 | "slim/slim": ">=4.0.0" 15 | }, 16 | "autoload": { 17 | "psr-4": { "Slim\\Middleware\\": "src/Slim/Middleware/" } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/Slim/Middleware/Minify.php: -------------------------------------------------------------------------------- 1 | shouldMinify = !empty($shouldMinify); 44 | } 45 | 46 | /** 47 | * minify html content 48 | * 49 | * @param string $html 50 | * @return string 51 | */ 52 | private function minifyHTML($html) 53 | { 54 | $search = ['/(?:(?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:(?[^\S ]+/s', '/[^\S ]+\/']; 55 | $replace = [' ', ' ', '>', '<', '\\1', '']; 56 | 57 | return preg_replace($search, $replace, $html); 58 | } 59 | 60 | /** 61 | * @param Request $request 62 | * @param RequestHandler $handler 63 | * @return Response 64 | */ 65 | public function __invoke(Request $request, RequestHandler $handler): Response 66 | { 67 | $response = $handler->handle($request); 68 | 69 | if (!$this->shouldMinify) { 70 | return $response; 71 | } 72 | 73 | $oldBody = $response->getBody(); 74 | 75 | $minifiedBodyContent = $this->minifyHTML((string)$oldBody); 76 | 77 | 78 | $newBody = new \Slim\Psr7\Stream(fopen('php://temp', 'r+')); 79 | 80 | //write the minified html content to the new \Slim\Http\Body instance 81 | $newBody->write($minifiedBodyContent); 82 | 83 | return $response->withBody($newBody); 84 | } 85 | } --------------------------------------------------------------------------------