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 Javascript
  • CompassFilter: Compass CSS authoring framework
  • CssEmbedFilter: embeds image data in your stylesheets
  • CssImportFilter: inlines imported stylesheets
  • CssMinFilter: minifies CSS
  • CssRewriteFilter: fixes relative URLs in CSS assets when moving to a new URL
  • DartFilter: compiles Javascript using dart2js
  • EmberPrecompileFilter: precompiles Handlebars templates into Javascript for use in the Ember.js framework
  • GoogleClosure\CompilerApiFilter: compiles Javascript using the Google Closure Compiler API
  • GoogleClosure\CompilerJarFilter: compiles Javascript using the Google Closure Compiler JAR
  • GssFilter: compliles CSS using the Google Closure Stylesheets Compiler
  • HandlebarsFilter: compiles Handlebars templates into Javascript
  • JpegoptimFilter: optimize your JPEGs
  • JpegtranFilter: optimize your JPEGs
  • JSMinFilter: minifies Javascript
  • JSMinPlusFilter: minifies Javascript
  • LessFilter: parses LESS into CSS (using less.js with node.js)
  • LessphpFilter: parses LESS into CSS (using lessphp)
  • OptiPngFilter: optimize your PNGs
  • PackagerFilter: parses Javascript for packager tags
  • PackerFilter: compresses Javascript using Dean Edwards's Packer
  • PhpCssEmbedFilter: embeds image data in your stylesheet
  • PngoutFilter: optimize your PNGs
  • Sass\SassFilter: parses SASS into CSS
  • Sass\ScssFilter: parses SCSS into CSS
  • ScssphpFilter: parses SCSS using scssphp
  • SprocketsFilter: Sprockets Javascript dependency management
  • StylusFilter: parses STYL into CSS
  • TypeScriptFilter: parses TypeScript into Javascript
  • UglifyCssFilter: minifies CSS
  • UglifyJs2Filter: minifies Javascript
  • UglifyJsFilter: minifies Javascript
  • Yui\CssCompressorFilter: compresses CSS using the YUI compressor
  • Yui\JsCompressorFilter: compresses Javascript using the YUI compressor

项目主页:http://www.open-open.com/lib/view/home/1388453755375