Skip to content

Instantly share code, notes, and snippets.

@lsmith77
Created January 1, 2011 13:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lsmith77/761731 to your computer and use it in GitHub Desktop.
Save lsmith77/761731 to your computer and use it in GitHub Desktop.
In development the CSS file is generated on the fly, while the file is stored permanently in the production build script.
<?php
// RewriteRule ^css/main.css$ dev/less/getcss.php?cssfile=main [L]
header("Cache-Control: no-cache");
header('Content-Type: text/css');
if (isset($_GET['cssfile'])) {
$cache = generatecss($_GET['cssfile']);
if (isset($cache['compiled']) && is_array($cache)) {
$data = $cache['compiled'];
} else {
$data = $cache;
}
} else {
$data = "// cssfile GET parameter missing";
}
echo $data;
function generatecss($cssfile)
{
$lessCompiler = __DIR__.'/../../src/vendor/lessphp/lessc.inc.php';
if (!include $lessCompiler) {
return "// less class definition not found: ".$lessCompiler;
}
if (!preg_match('/^[a-z_]+$/i', $cssfile)) {
return "// cssfile parameter illegal: $cssfile";
}
$input = __DIR__."/../../app/$cssfile/config/$cssfile.less";
if (!file_exists($input)) {
return "// cssfile input file missing: $input";
}
try {
$cacheDir = __DIR__."/../../app/$cssfile/cache/dev";
$cacheFile =$cacheDir."/cache_$cssfile.less";
if (file_exists($cacheFile)) {
$input = unserialize(file_get_contents($cacheFile));
$time = $input['updated'];
} else {
$time = 0;
}
$cache = lessc::cexecute($input);
if ($time < $cache['updated']) {
if (!file_exists($cacheDir)) {
mkdir($cacheDir, 0770, true);
}
file_put_contents($cacheFile, serialize($cache));
}
} catch (exception $e) {
return "// While compiling $input, encountered: ".$e->getMessage();
}
return $cache;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment