Created
May 16, 2011 09:50
-
-
Save BlackScorp/974165 to your computer and use it in GitHub Desktop.
CSS Minifier class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Scorp Framework | |
* @copyright Coypright 2010, BlackScorp | |
* @package | |
* @subpackage ScorpFramework. | |
* @license MIT License (http://www.opensource.org/licenses/mit-license.php) | |
*/ | |
/** | |
* Klasse <b>Minifier</b> | |
* @package | |
* @subpackage ScorpFramework. | |
*/ | |
class Minifier { | |
private $files; | |
private $outputFile; | |
public function addFile($name) { | |
$fileArray = explode('.', basename($name)); | |
$fileType = $fileArray[count($fileArray) - 1]; | |
$this->files[$fileType][$fileArray[0]] = $name; | |
} | |
private function minifieCSSFiles() { | |
$result = ""; | |
$path = ""; | |
foreach ($this->files['css'] as $file) { | |
if (file_exists($file)) { | |
$path = dirname($file); | |
$result .= file_get_contents($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); | |
} | |
} | |
$result = (preg_replace('~\/\*[^*]*\*+([^/*][^*]*\*+)*\/~m', '', $result)); | |
$result = preg_replace("/\r|\n/s", "", $result); | |
$checkSum = md5($result); | |
if (!file_exists($path . '/checksum.txt')) { | |
file_put_contents($path . '/checksum.txt', $checkSum); | |
file_put_contents($path . '/style.css', $result); | |
} else { | |
$oldCheckSum = file_get_contents($path . '/checksum.txt'); | |
if ($oldCheckSum != $checkSum) { | |
file_put_contents($path . '/checksum.txt', $checkSum); | |
file_put_contents($path . '/style.css', $result); | |
} | |
} | |
$this->outputFile['css'] = $path . '/style.css?' . time(); | |
} | |
public function getFile($type) { | |
$this->minifieCSSFiles(); | |
return $this->outputFile[$type]; | |
} | |
} | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html xmlns="http://www.w3.org/1999/xhtml"> | |
<head> | |
<?php | |
require_once 'Minifier.class.php'; | |
$min = new Minifier(); | |
$min->addFile('templates/bluelayout/css/test1.css'); | |
$min->addFile('templates/bluelayout/css/test2.css'); | |
$min->addFile('templates/bluelayout/css/test3.css'); | |
echo $min->getFile('css'); //output: templates/bluelayout/css/style.css?time() | |
?> | |
<title>Minfie CSS</title> | |
</head> | |
<body> | |
<!-- viele Divs --> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment