我有几种方法可以将许多 CSS 资产组合为一个,缩小它们并将它们转储(缓存)到生成的文件名,如“/assetic/6bad22c.css”。我通过使用以下内容来实现这一点:
目前我使用 AssetFactory
private static function getCssAssetFactory()
{
$fm = new FilterManager();
$fm->set('less', new Filter\LessphpFilter());
$fm->set('import', new Filter\CssImportFilter());
$fm->set('rewrite', new Filter\CssRewriteFilter());
$fm->set('min', new Filter\CssMinFilter());
$factory = new AssetFactory(self::getAssetBuildPath());
$factory->setFilterManager($fm);
return $factory;
}
并通过创建资产
public static function dumpStylesheets()
{
$asset = self::getCssAssetFactory()->createAsset
( self::$stylesheets
, array
( 'less' // Less CSS Compiler
, 'import' // Solves @imports
, 'rewrite' // Rewrites Base URLs when moving to another URL
, 'min' // Minifies the script
)
, array('output' => 'assetic/*.css')
);
$cache = self::getAssetCache($asset);
self::getAssetWriter()->writeAsset($cache);
return self::basePath().'/'.$asset->getTargetPath();
}
以下是参考的方法:
private static function getAssetWriter()
{
if (is_null(self::$AssetWriter))
{
self::$AssetWriter = new AssetWriter(self::getAssetBuildPath());
}
return self::$AssetWriter;
}
private static function getAssetCache($asset)
{
return new AssetCache
( $asset
, new FilesystemCache(self::getAssetBuildPath().'/cache')
);
}
到目前为止,这里没有魔法。我的问题是,根据定义self::$stylesheets
,数组只包含资产的路径字符串。但我需要像这样使用真正的资产资产:
self::$stylesheets = array
( new Asset\FileAsset('path/to/style.css')
, new Asset\StringAsset('.some-class {text-decoration: none;}');
, new Asset\HttpAsset('http://domain.tld/assets/style.css');
);
但AssetFactory::createAsset()
不接受自己的资产。我需要使用的可能性,StringAsset
因为我必须更改服务器端 CSS / JS 中的一些值。
除了使用还有其他方法可以实现这一点AssetFactory::createAsset()
吗?