assetic是由 Winter CMS 团队维护的 PHP 资产管理框架。
<?php
use assetic Asset AssetCollection ;
use assetic Asset FileAsset ;
use assetic Asset GlobAsset ;
$ js = new AssetCollection ( array (
new GlobAsset ( ' /path/to/js/* ' ),
new FileAsset ( ' /path/to/another.js ' ),
));
// the code is merged when the asset is dumped
echo $ js -> dump ();
assetic资产是具有可过滤内容、可以加载和转储的东西。资产还包括元数据,其中一些元数据可以操作,一些元数据是不可变的。
财产 | 附件 | 突变体 |
---|---|---|
内容 | 获取内容 | 设置内容 |
时间 | 获取最后修改时间 | 不适用 |
源根 | 获取源根 | 不适用 |
源路径 | 获取源路径 | 不适用 |
目标路径 | 获取目标路径 | 设置目标路径 |
“目标路径”属性表示资产(或资产集合)应转储到的位置。
可以应用过滤器来操纵资产。
<?php
use assetic Asset AssetCollection ;
use assetic Asset FileAsset ;
use assetic Asset GlobAsset ;
use assetic Filter LessFilter ;
use assetic Filter UglifyCssFilter ;
$ css = new AssetCollection ( array (
new FileAsset ( ' /path/to/src/styles.less ' , array ( new LessFilter ())),
new GlobAsset ( ' /path/to/css/* ' ),
), array (
new UglifyCssFilter ( ' /path/to/uglifycss ' ),
));
// this will echo CSS compiled by LESS and compressed by uglifycss
echo $ css -> dump ();
如果您对其进行迭代,应用于集合的过滤器将级联到每个资产叶。
<?php
foreach ( $ css as $ leaf ) {
// each leaf is compressed by uglifycss
echo $ leaf -> dump ();
}
核心在assetic Filter
命名空间中提供以下过滤器:
CoffeeScriptFilter
:将 CoffeeScript 编译为 JavascriptCssImportFilter
:内联导入的样式表CSSMinFilter
:缩小 CSSCssRewriteFilter
:移动到新 URL 时修复 CSS 资源中的相对 URLGoogleClosureCompilerApiFilter
:使用 Google Closure Compiler API 编译 JavascriptHandlebarsFilter
:将 Handlebars 模板编译为 JavascriptJavaScriptMinifierFilter
:缩小 JavascriptJpegoptimFilter
:优化您的 JPEGJpegtranFilter
:优化您的 JPEGLessFilter
:将 LESS 解析为 CSS(使用 less.js 和 node.js)LessphpFilter
:将 LESS 解析为 CSS(使用 lessphp)OptiPngFilter
:优化您的 PNGPackerFilter
:使用 Dean Edwards 的 Packer 压缩 JavascriptPhpCssEmbedFilter
:将图像数据嵌入样式表中ReactJsxFilter
:将 React JSX 编译为 JavaScriptScssphpFilter
:将 SCSS 解析为 CSSSeparatorFilter
:在资产之间插入分隔符以防止合并失败StylesheetMinifyFilter
:压缩样式表 CSS 文件StylusFilter
:将 STYL 解析为 CSSTailwindCssFilter
:使用 Tailwind CSS 独立 CLI 实用程序构建 Tailwind CSS 样式表TypeScriptFilter
:将 TypeScript 解析为 JavascriptUglifyCssFilter
:缩小 CSSUglifyJs2Filter
:缩小 JavascriptUglifyJs3Filter
:缩小 Javascript 提供资产管理器来组织资产。
<?php
use assetic AssetManager ;
use assetic Asset FileAsset ;
use assetic Asset GlobAsset ;
$ am = new AssetManager ();
$ am -> set ( ' jquery ' , new FileAsset ( ' /path/to/jquery.js ' ));
$ am -> set ( ' base_css ' , new GlobAsset ( ' /path/to/css/* ' ));
资产管理器还可用于引用资产以避免重复。
<?php
use assetic Asset AssetCollection ;
use assetic Asset AssetReference ;
use assetic Asset FileAsset ;
$ am -> set ( ' my_plugin ' , new AssetCollection ( array (
new AssetReference ( $ am , ' jquery ' ),
new FileAsset ( ' /path/to/jquery.plugin.js ' ),
)));
还提供了过滤器管理器来组织过滤器。
<?php
use assetic FilterManager ;
use assetic Filter ScssFilter ;
use assetic Filter CssMinFilter ;
$ fm = new FilterManager ();
$ fm -> set ( ' sass ' , new ScssFilter ( ' /path/to/parser/scss ' ));
$ fm -> set ( ' cssmin ' , new CssMinFilter ());
如果您不想手动创建所有这些对象,则可以使用资产工厂,它将为您完成大部分工作。
<?php
use assetic Factory AssetFactory ;
$ factory = new AssetFactory ( ' /path/to/asset/directory/ ' );
$ factory -> setAssetManager ( $ am );
$ factory -> setFilterManager ( $ fm );
$ factory -> setDebug ( true );
$ css = $ factory -> createAsset ( array (
' @reset ' , // load the asset manager's "reset" asset
' css/src/*.scss ' , // load every scss files from "/path/to/asset/directory/css/src/"
), array (
' scss ' , // filter through the filter manager's "scss" filter
' ?cssmin ' , // don't use this filter in debug mode
));
echo $ css -> dump ();
AssetFactory
使用根目录构建,该根目录用作相对资源路径的基目录。
在过滤器名称前加上问号前缀(如cssmin
所示),将导致工厂处于调试模式时忽略该过滤器。
您还可以在工厂上注册 Workers,并且它创建的所有资产将在返回之前传递给 Worker 的process()
方法。请参阅下面的缓存清除示例。
您可以将 AssetManager 保存的所有资产转储到目录中的文件中。这可能位于网络服务器的文档根目录下方,因此可以静态提供文件。
<?php
use assetic AssetWriter ;
$ writer = new AssetWriter ( ' /path/to/web ' );
$ writer -> writeManagerAssets ( $ am );
这将利用资产的目标路径。
如果您如上所述从静态文件提供资产,则可以使用 CacheBustingWorker 重写资产的目标路径。它将在文件扩展名之前插入一个标识符,该标识符对于特定版本的资产是唯一的。
该标识符基于资产的修改时间,并且如果应用的过滤器支持它,还将考虑依赖的资产。
<?php
use assetic Factory AssetFactory ;
use assetic Factory Worker CacheBustingWorker ;
$ factory = new AssetFactory ( ' /path/to/asset/directory/ ' );
$ factory -> setAssetManager ( $ am );
$ factory -> setFilterManager ( $ fm );
$ factory -> setDebug ( true );
$ factory -> addWorker ( new CacheBustingWorker ());
$ css = $ factory -> createAsset ( array (
' @reset ' , // load the asset manager's "reset" asset
' css/src/*.scss ' , // load every scss files from "/path/to/asset/directory/css/src/"
), array (
' scss ' , // filter through the filter manager's "scss" filter
' ?yui_css ' , // don't use this filter in debug mode
));
echo $ css -> dump ();
提供了简单的缓存机制以避免不必要的工作。
<?php
use assetic Asset AssetCache ;
use assetic Asset FileAsset ;
use assetic Cache FilesystemCache ;
use assetic Filter JavaScriptMinifierFilter ;
$ jsMinifier = new JavaScriptMinifierFilter ();
$ js = new AssetCache (
new FileAsset ( ' /path/to/some.js ' , array ( $ jsMinifier )),
new FilesystemCache ( ' /path/to/cache ' )
);
// the JavaScriptMinifierFilter compressor will only run on the first call
$ js -> dump ();
$ js -> dump ();
$ js -> dump ();
要使用assetic Twig 扩展,您必须将其注册到您的 Twig 环境:
<?php
$ twig -> addExtension ( new assetic Extension ( $ factory ));
一旦到位,该扩展就会公开一个样式表和一个 javascript 标签,其语法与资产工厂使用的语法类似:
{% stylesheets '/path/to/sass/main.sass' filter = 'sass,?yui_css' output = 'css/all.css' %}
< link href = " {{ asset_url }} " type = " text/css " rel = " stylesheet " />
{% endstylesheets %}
此示例将在页面上呈现一个link
元素,其中包含可以在其中找到已过滤资产的 URL。
当扩展处于调试模式时,同一标签将呈现多个link
元素,一个链接元素对应css/src/*.sass
glob 引用的每个资源。指定的过滤器仍将应用,除非使用?
前缀。
也可以通过在标签上设置debug
属性来触发此行为:
{% stylesheets 'css/*' debug = true %} ... {% stylesheets %}
这些资产需要写入 Web 目录,以便这些 URL 不会返回 404 错误。
<?php
use assetic AssetWriter ;
use assetic Extension Twig TwigFormulaLoader ;
use assetic Extension Twig TwigResource ;
use assetic Factory LazyAssetManager ;
$ am = new LazyAssetManager ( $ factory );
// enable loading assets from twig templates
$ am -> setLoader ( ' twig ' , new TwigFormulaLoader ( $ twig ));
// loop through all your templates
foreach ( $ templates as $ template ) {
$ resource = new TwigResource ( $ twigLoader , $ template );
$ am -> addResource ( $ resource , ' twig ' );
}
$ writer = new AssetWriter ( ' /path/to/web ' );
$ writer -> writeManagerAssets ( $ am );
assetic基于 Python webassets 库(可在 GitHub 上获取)。