jalle19/php-yui-compressor

YUI 压缩器的现代 PHP 封装

1.0.1 2014-06-03 12:02 UTC

This package is auto-updated.

Last update: 2024-09-13 00:07:08 UTC


README

YUI 压缩器的现代 PHP 封装。

安装

运行 composer install(如果您还没有安装 Composer,请先安装)。您还需要安装 Java 并将其添加到您的路径中(在基于 Debian/Ubuntu 的系统上,您可以使用 sudo apt-get install default-jre 来安装)

用法

以下代码片段假设您已包含 Composer 生成的自动加载器。

<?php

$yui = new \YUI\Compressor();

// Read the uncompressed contents
$css = file_get_contents('styles.css');
$script = file_get_contents('script.js');

// Compress the CSS
$yui->setType(\YUI\Compressor::TYPE_CSS);
$optimizedCss = $yui->compress($css);

// Compress the JavaScript
$yui->setType(\YUI\Compressor::TYPE_JS);
$optimizedScript = $yui->compress($script);
<?php

// javaPath defauls to "java" which means it has to be in the path. If that's 
// not the case we can override it like this.
$yui = new \YUI\Compressor(array(
	'javaPath'=>'/usr/bin/java',
	'line-break'=>80,
	'disable-optimizations'=>true,
));

// Read the uncompressed contents
$css = file_get_contents('styles.css');
$script = file_get_contents('script.js');

// The "disable-optimizations" option is JavaScript-specific so it won't apply 
// here...
$yui->setType(\YUI\Compressor::TYPE_CSS);
$optimizedCss = $yui->compress($css);

// ...but here it will
$yui->setType(\YUI\Compressor::TYPE_JS);
$optimizedScript = $yui->compress($script);

鸣谢

灵感来源于 gpbmike 的作品(《https://github.com/gpbmike/PHP-YUI-Compressor》)