php - Assetic is generating multiple files with same content -
i have class uses assetic generate css files disk. i'll jump right code.
in layout header, i'm doing this:
$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();
my "assetify" class runs through assetic. i'll paste hope relevant portions dump()
function:
// asset factory allows not have hard work ourselves. $factory = new assetfactory($this->assetdirectory, $this->debug); $factory->setdefaultoutput('/generated/*.css'); // filter manager allows organize filters asset handling. // 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); // asset manager allows keep our assets organized. $am = new assetmanager(); $factory->setassetmanager($am); // cache-busting worker prefixes every css amounts version number. $factory->addworker(new cachebustingworker()); $assetcollection = array(); foreach ($assetgroups $assetgroup) { foreach ($assetgroup $media => $items) { $filecollection = array(); foreach ($items $item) { // add asset asset collection. $filecollection[] = new fileasset($item); } $assetcollection[] = new assetcollection($filecollection); } } $assetcollection = new assetcollection($assetcollection); $am->set('base_css', $assetcollection); // generate required assets. prefixing filter name question mark // cause filter omitted in debug mode. $asset = $factory->createasset( array('@base_css'), array('?yui_css') ); // configure internal file system cache don't regenerate file on every load. $cache = new assetcache( $asset, new filesystemcache($this->outputdirectory) ); // , generate static versions of files on disk. $writer = new assetwriter($this->assetdirectory); $writer->writeasset($cache);
this generates 2 different files, 87229eb-f47a352.css
, a37c1589762f39aee5bd24e9405dbdf9
. contents of files same. 87229eb-f47a352.css
file seems generated every single time, , other file not regenerated unless contents of files change (this like). if comment out $writer->writeasset($cache)
, no files written disk.
what obvious configuration missing? appreciate help, thank you.
i able replicate code , got same results.
i trying same results think require ended writing own code cache , serve static files.
it's not complete means working. has following features:
- you can choose cache files different pages if specify $filename
- you can choose create versions of released files or delete previous versions
- a cached file generated target folder if changes have made source file
- you need put code in class or function , return url serve.
hope helps :)
<?php use assetic\factory\assetfactory; use assetic\assetmanager; use assetic\filtermanager; use assetic\asset\assetcollection; use assetic\asset\fileasset; use assetic\filter\jsminfilter; // javascript collection $js_collection[] = new fileasset(script_path . 'jquery.js'); $js_collection[] = new fileasset(script_path . 'production.js'); if (file_exists(script_path . $page_info['name'] . '.js')) { $js_collection[] = new fileasset(script_path . $page_info['name'] . '.js'); } // css collection $css_collection[] = new fileasset(style_path . 'theme.css'); if (file_exists(style_path . $page_info['name'] . '.css')) { $css_collection[] = new fileasset(style_path . $page_info['name'] . '.css'); } // filter manager allows organize filters asset handling. $fm = new filtermanager(); $fm->set('js', new jsminfilter()); $js = new assetcollection ( $js_collection ); $js->settargetpath(script_path . 'static'); $css = new assetcollection ( $css_collection ); $css->settargetpath(style_path . 'static'); $am = new assetmanager(); $am->set('js', $js); $am->set('css', $css); //** do: put below in class , return static file names **// // options $seperator = '-'; $filename = $page_info['name']; $versions = false; // list of collection names $collections = $am->getnames(); // each collection foreach ($collections $collection_name) { // collection object $collection = $am->get($collection_name); // ensure file types identical $last_ext = false; foreach ($collection $leaf) { $ext = strtolower(pathinfo($leaf->getsourcepath(), pathinfo_extension)); if (!$last_ext || $ext == $last_ext) { $last_ext = $ext; } else { throw new \runtimeexception('file type mismatch.'); } } // highest last-modified value of assets in current collection $modified_time = $collection->getlastmodified(); // target path $path = $collection->gettargetpath(); // target path must set if (!$path) { throw new \runtimeexception('target path not specified.'); } // build filename check $file = ($filename) ? $filename . $seperator . $modified_time . '.' . $ext : $modified_time . '.' . $ext; $cached_file = $path . '/' . $file; // file doesn't exist need minify, dump , save new cached file if (!file_exists($cached_file)) { // create output dir if doesnt exist if (!is_dir($path) && false === @mkdir($path, 0777, true)) { throw new \runtimeexception('unable create directory ' . $path); } // apply filters if ($fm->has($collection_name)) { $collection->ensurefilter($fm->get($collection_name)); } // if not versioned, delete previous version of file if (!$versions) { if ($filename) { foreach (glob($path . '/' . $filename . $seperator . '*.' . $ext) $searchfile) { @unlink($searchfile); } } else { foreach (glob($path . '/*.' . $ext) $searchfile) { @unlink($searchfile); } } } // put contents in file if (false === @file_put_contents($cached_file, $collection->dump())) { throw new \runtimeexception('unable write file ' . $cached_file); } } // return cached file echo 'output: ' . $cached_file . '<br>'; } exit; ?>
Comments
Post a Comment