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