Skip to content

Instantly share code, notes, and snippets.

@samt
Created April 6, 2011 20:22
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 samt/906441 to your computer and use it in GitHub Desktop.
Save samt/906441 to your computer and use it in GitHub Desktop.
CSS Generator
#
# CSS Generator
#
# Scans a directory for all the CSS and compiles them into a single, compressed
# document for easy and quick access from the client side. Also sends headers
# to ensure that the document will not be loaded more than every 2 days.
#
<Files .htaccess>
Order Allow,Deny
Deny from all
</files>
#
# Set some headers to ensure speed
#
<Files stylesheet.css>
Header set Cache-Control "max-age=172800, must-revalidate"
</files>
RewriteEngine On
#
# Rewrite to cssgen.php for generation
#
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} /stylesheet\.css$
RewriteRule . cssgen.php [L]
<?php
//
// CSS Cacher and compressor
//
$path = dirname(__FILE__);
$buffer = '';
foreach(glob("*.css") as $file)
{
$buffer .= file_get_contents($file);
}
$buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);
$buffer = str_replace(array("\t", "\r\n", "\r", "\n", ' ', ' ', ' '), '', $buffer);
$buffer = str_replace(array(': ', ' :', ', ', ' {', ';}'), array(':', ':', ',', '{', '}'), $buffer);
header("content-type: text/css; charset: UTF-8");
echo $buffer;
@file_put_contents($path . '/stylesheet.css', $buffer);
exit;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment