多くの CSS アセットを 1 つに結合し、それらを縮小して、"/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
により、配列にはアセットへのパス文字列だけが含まれていることです。しかし、次のように実際の Assetic Assets を使用する必要があります。
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()
ますか?