Last Updated: February 25, 2016
·
1.547K
· adykto

CSS = LESS + Optimization + Minification

Libraries needed: LessPHP, CSSTidy, Minify.

define('DEBUG_MODE', true);
$lessFile = './less/style.less';
$lessCache = './cache/style.css.cached';

$lessCached = (file_exists($lessCache)) ? unserialize(file_get_contents($lessCache)) : $lessFile;
$lessParsed = lessc::cexecute($lessCached);

if(!is_array($lessCached) || $lessParsed['updated'] > $lessCached['updated']) {
    file_put_contents($lessCache, serialize($lessParsed));
}

$cssCode = $lessParsed['compiled'];

$css = new csstidy();
$css->set_cfg('preserve_css', false);
$css->set_cfg('optimise_shorthands', 3);
$css->set_cfg('merge_selectors', 1);
$css->set_cfg('discard_invalid_properties', 1);
$css->parse($cssCode);
$cssCode = $css->print->plain();

header('Content-type: text/css');
if(DEBUG_MODE) {
    header("Cache-Control: no-cache");
    header("Pragma: no-cache");
    echo $cssCode;
} else {
    echo CssMin::minify($cssCode);
}