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 上取得)。