PHP的资源管理框架:Assetic
jopen
11年前
Assetic 是一个 PHP 的资源管理框架,用于合并和压缩 CSS/JS 资源。这种方式合并资源可以减少浏览器对资源的请求数、降低资源下载大小、加速网站访问。
Assets
An Assetic asset is something with filterable content that can be loaded and dumped. An asset also includes metadata, some of which can be manipulated and some of which is immutable.
Property | Accessor | Mutator |
---|---|---|
content | getContent | setContent |
mtime | getLastModified | n/a |
source root | getSourceRoot | n/a |
source path | getSourcePath | n/a |
target path | getTargetPath | setTargetPath |
The "target path" property denotes where an asset (or an collection of assets) should be dumped.
Filters
Filters can be applied to manipulate assets.
<?php use Assetic\Asset\AssetCollection; use Assetic\Asset\FileAsset; use Assetic\Asset\GlobAsset; use Assetic\Filter\LessFilter; use Assetic\Filter\Yui; $css = new AssetCollection(array( new FileAsset('/path/to/src/styles.less', array(new LessFilter())), new GlobAsset('/path/to/css/*'), ), array( new Yui\CssCompressorFilter('/path/to/yuicompressor.jar'), )); // this will echo CSS compiled by LESS and compressed by YUI echo $css->dump();
The filters applied to the collection will cascade to each asset leaf if you iterate over it.
<?php foreach ($css as $leaf) { // each leaf is compressed by YUI echo $leaf->dump(); }
The core provides the following filters in the Assetic\Filter
namespace:
CoffeeScriptFilter
: compiles CoffeeScript into JavascriptCompassFilter
: Compass CSS authoring frameworkCssEmbedFilter
: embeds image data in your stylesheetsCssImportFilter
: inlines imported stylesheetsCssMinFilter
: minifies CSSCssRewriteFilter
: fixes relative URLs in CSS assets when moving to a new URLDartFilter
: compiles Javascript using dart2jsEmberPrecompileFilter
: precompiles Handlebars templates into Javascript for use in the Ember.js frameworkGoogleClosure\CompilerApiFilter
: compiles Javascript using the Google Closure Compiler APIGoogleClosure\CompilerJarFilter
: compiles Javascript using the Google Closure Compiler JARGssFilter
: compliles CSS using the Google Closure Stylesheets CompilerHandlebarsFilter
: compiles Handlebars templates into JavascriptJpegoptimFilter
: optimize your JPEGsJpegtranFilter
: optimize your JPEGsJSMinFilter
: minifies JavascriptJSMinPlusFilter
: minifies JavascriptLessFilter
: parses LESS into CSS (using less.js with node.js)LessphpFilter
: parses LESS into CSS (using lessphp)OptiPngFilter
: optimize your PNGsPackagerFilter
: parses Javascript for packager tagsPackerFilter
: compresses Javascript using Dean Edwards's PackerPhpCssEmbedFilter
: embeds image data in your stylesheetPngoutFilter
: optimize your PNGsSass\SassFilter
: parses SASS into CSSSass\ScssFilter
: parses SCSS into CSSScssphpFilter
: parses SCSS using scssphpSprocketsFilter
: Sprockets Javascript dependency managementStylusFilter
: parses STYL into CSSTypeScriptFilter
: parses TypeScript into JavascriptUglifyCssFilter
: minifies CSSUglifyJs2Filter
: minifies JavascriptUglifyJsFilter
: minifies JavascriptYui\CssCompressorFilter
: compresses CSS using the YUI compressorYui\JsCompressorFilter
: compresses Javascript using the YUI compressor