jrhenderson1988/miniphy
Composer 安装命令:
composer require jrhenderson1988/miniphy
包简介
A HTML/PHP template minifier. Includes support for Laravel's Blade compiler.
README 文档
README
A PHP based HTML minifier that's designed to be simple. It offers built in support for Laravel 5, providing both a service provider and facade. Additionally, it is configurable to allow for compile-time minification of blade template files.
Installation
You can install Miniphy using composer. Just run the following command to get the latest version:
$ composer require jrhenderson1988/miniphy
Or you can add it manually to your composer.json file and run composer update.
{
"require": {
"jrhenderson1988/miniphy": "^1.0"
}
}
If you're using Laravel, don't forget to add the MiniphyServiceProvider to your config/app.php.
// ...
Illuminate\Validation\ValidationServiceProvider::class,
Illuminate\View\ViewServiceProvider::class,
/**
* Package Service Providers...
*/
Miniphy\MiniphyServiceProvider::class,
// ...
If you want to use the Facade, make sure you add it to your aliases in config/app.php.
// ...
'Validator' => Illuminate\Support\Facades\Validator::class,
'View' => Illuminate\Support\Facades\View::class,
'Miniphy' => Miniphy\Facades\Miniphy::class,
],
Usage
It's pretty easy to use Miniphy, in fact it's just a case of creating a Miniphy instance and calling the html method to create a HTML driver to be used for minifying HTML. You can then call the minify method on the resulting driver with some content and it's minified content will be returned:
use Miniphy\Miniphy;
$content = '<html> <body> <p> Your HTML content </p> </body> </html>';
$miniphy = new Miniphy();
$minifiedContent = $miniphy->html()->minify($content);
For convenience, you may also pass your HTML content to the html method and the minified content will be returned. Under the hood this will create a HTML driver and call it's minify method.
use Miniphy\Miniphy;
$content = '<html> <body> <p> Your HTML content </p> </body> </html>';
$minifiedContent = (new Miniphy())->html($content);
Modes
Miniphy's HTML minification allows for 3 different modes: soft, medium and hard.
- Soft (default): This mode will leave a single space between HTML elements when removing whitespace. This is the safest mode to use and will be the least likely to cause problems.
- Medium: This mode will remove all whitespace around block level and undisplayed elements, but preserve a single space around inline elements such as
span,aetc. Bear in mind that it's possible to make typically inline elements behave like block elements using CSS, so this mode may have unwanted side-effects if doing so. - Hard: This is the most aggressive mode and will remove whitespace around all elements.
You can easily set the mode by calling the chainable setHtmlMode method on the Miniphy instance. There is also a htmlMode method that can be used to get or set the HTML mode, when provided with a parameter, this method will return the Miniphy instance for chaining. Without a parameter, this method will return an integer value representing the mode.
use Miniphy\Miniphy;
$miniphy = new Miniphy();
// Soft mode
$miniphy->setHtmlMode(Miniphy::HTML_MODE_SOFT);
// Set medium mode and minify the provided HTML content
$miniphy->setHtmlMode(Miniphy::HTML_MODE_MEDIUM)->html(' HTML CONTENT ');
// Set hard mode and minify the content using the htmlMode method
$miniphy->htmlMode(Miniphy::HTML_MODE_HARD)->html(' HTML CONTENT ');
Laravel
When you've set up the package correctly in Laravel you can use Miniphy very easily, either through using the Facade, dependency injection or you can get an instance of Miniphy from the IoC container. Alternatively, you may enable compile-time Blade optimisation through the settings to automatically minify your blade templates when they're generated:
Facade
$minified = Miniphy::html(' Your HTML content ');
Dependency Injection
<?php
namespace App\Http\Controllers;
use Miniphy\Miniphy;
class MyController extends Controller
{
public function myMethod(Miniphy $miniphy)
{
return $miniphy->html(' Your HTML content ');
}
}
IoC container
$miniphy = app('miniphy');
$minified = $miniphy->html(' Your HTML content ');
Compile-time Blade optimisation
If you want your Blade templates to be minified at compile time you may enable compile-time blade optimisation through the settings.
In config/miniphy.php
<?php
return [
// ...
'blade' => true,
// ...
];
Important Note: Laravel caches the Blade templates after they've been compiled. If you find that your views are not being minified after enabling blade optimisation you may need to run
php artisan view:clear. This will clear the view cache and the templates will be re-compiled. Similarly, if you want to disable Blade optimisation, you will need to run this command to remove the cached, minified views.
TODO
- When reserving PHP tags, allow for short opening tags
- When reserving PHP tags, take into account short echo style tags
<?= $value ?> - Add support for minifying inline CSS.
- Add support for minifying inline Javascript.
jrhenderson1988/miniphy 适用场景与选型建议
jrhenderson1988/miniphy 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.24k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2017 年 03 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 jrhenderson1988/miniphy 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jrhenderson1988/miniphy 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 2.24k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 23
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-03-23