3
我有一個使用Assetic來生成一些css文件到磁盤的類。我會直接跳到代碼中。Assetic正在生成具有相同內容的多個文件
在我的佈局頭,我在做這樣的事情:
$assetify = new Assetify();
$assetify->setDebug(true);
$assetify->setAssetDirectory(BASE_DIR . '/public/assets');
$assetify->setOutputDirectory(BASE_DIR . '/public/assets/generated');
$assetify
->addStylesheet('/assets/css/bootstrap-2.3.2.css')
->addStylesheet('/assets/css/select2-3.4.3.css')
->addStylesheet('/assets/css/main.css');
echo $assetify->dump();
我 「Assetify」 類通過Assetic運行此。我會貼一下,希望只從dump()
功能的相關部分:
// The Asset Factory allows us to not have to do all the hard work ourselves.
$factory = new AssetFactory($this->assetDirectory, $this->debug);
$factory->setDefaultOutput('/generated/*.css');
// The Filter Manager allows us to organize filters for the asset handling.
// For other filters, see: https://github.com/kriswallsmith/assetic
$fm = new FilterManager();
$fm->set('yui_css', new Yui\CssCompressorFilter('/usr/local/bin/yuicompressor-2.4.7.jar'));
$fm->set('yui_js', new Yui\JsCompressorFilter('/usr/local/bin/yuicompressor-2.4.7.jar'));
$factory->setFilterManager($fm);
// The Asset Manager allows us to keep our assets organized.
$am = new AssetManager();
$factory->setAssetManager($am);
// The cache-busting worker prefixes every css with what amounts to a version number.
$factory->addWorker(new CacheBustingWorker());
$assetCollection = array();
foreach ($assetGroups as $assetGroup) {
foreach ($assetGroup as $media => $items) {
$fileCollection = array();
foreach ($items as $item) {
// Add this asset to the asset collection.
$fileCollection[] = new FileAsset($item);
}
$assetCollection[] = new AssetCollection($fileCollection);
}
}
$assetCollection = new AssetCollection($assetCollection);
$am->set('base_css', $assetCollection);
// Generate the required assets. Prefixing a filter name with a question mark
// will cause that filter to be omitted in debug mode.
$asset = $factory->createAsset(
array('@base_css'),
array('?yui_css')
);
// Configure an internal file system cache so we don't regenerate this file on every load.
$cache = new AssetCache(
$asset,
new FilesystemCache($this->outputDirectory)
);
// And generate static versions of the files on disk.
$writer = new AssetWriter($this->assetDirectory);
$writer->writeAsset($cache);
這會產生兩個不同的文件,87229eb-f47a352.css
和a37c1589762f39aee5bd24e9405dbdf9
。這些文件的內容完全相同。似乎每一次都會生成87229eb-f47a352.css
文件,除非文件內容發生更改(這是我所希望的),否則不會重新生成其他文件。如果我註釋掉$writer->writeAsset($cache)
,則沒有文件寫入磁盤。
我缺少什麼明顯的配置?我很感激幫助,謝謝。
你得到這個工作?我正在嘗試做類似的事情。你的班級看起來非常有用,你能分享嗎? –
我很抱歉,但直到今天我纔看到通知!差不多兩年後。哇靠。非常感謝你的回答,我接受了。我從來沒有真正理解它,並且不得不繼續前進,所以我最終讓我的腳本只是在生成後刪除每個非擴展文件。 – Vic